PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/chatbot/core/conversation/intialise_conversation.php

https://gitlab.com/tutaalexandr/bot_local
PHP | 690 lines | 465 code | 31 blank | 194 comment | 45 complexity | 4466a6c28aef73312c9947729180d0a1 MD5 | raw file
  1. <?php
  2. /***************************************
  3. * www.program-o.com
  4. * PROGRAM O
  5. * Version: 2.4.8
  6. * FILE: chatbot/core/conversation/intialise_conversation.php
  7. * AUTHOR: Elizabeth Perreau and Dave Morton
  8. * DATE: MAY 17TH 2014
  9. * DETAILS: this file contains the functions intialise
  10. * the conversation
  11. ***************************************/
  12. /**
  13. * function intialise_convoArray()
  14. * A function to intialise the conversation array
  15. * This is the array that is built throught the conversation
  16. * @link http://blog.program-o.com/?p=1242
  17. * @param array $convoArr - the current state of the conversation array
  18. * @return array $convoArr (updated)
  19. **/
  20. function intialise_convoArray($convoArr)
  21. {
  22. if (!isset($convoArr['conversation'])) {
  23. $convoArr['conversation'] = array();
  24. }
  25. //set the initial convoArr values
  26. runDebug(__FILE__, __FUNCTION__, __LINE__, "Intialising conversation", 4);
  27. //load blank topics
  28. $convoArr = load_blank_array_element('topic', "", $convoArr);
  29. //load blank thats
  30. $convoArr = load_blank_array_element('that', "", $convoArr);
  31. //load blank stars
  32. $convoArr = load_blank_array_element('star', "", $convoArr);
  33. //load blank stars
  34. $convoArr = load_blank_array_element('input', "", $convoArr);
  35. //load blank stack
  36. $convoArr = load_blank_stack($convoArr);
  37. //load bot properties
  38. $convoArr = load_default_bot_values($convoArr);
  39. //load the new client defaults
  40. $convoArr = load_new_client_defaults($convoArr);
  41. return $convoArr;
  42. }
  43. /**
  44. * function load_blank_array_element()
  45. * A function to intialise the conversation array values
  46. *
  47. * @link http://blog.program-o.com/?p=1244
  48. * @param string $arrayIndex - the array element we are going to intialise
  49. * @param string $defaultValue - the value which will be used to set the element
  50. * @param array $convoArr - the current state of the conversation array
  51. * @return array $convoArr (updated)
  52. */
  53. function load_blank_array_element($arrayIndex, $defaultValue, $convoArr)
  54. {
  55. global $remember_up_to;
  56. runDebug(__FILE__, __FUNCTION__, __LINE__, "Loading blank $arrayIndex array", 4);
  57. //set in global config file
  58. $remember_up_to = (isset($convoArr['conversation']['remember_up_to'])) ? $convoArr['conversation']['remember_up_to'] : $remember_up_to;
  59. for ($i = 1; $i <= ($remember_up_to + 1); $i++)
  60. {
  61. $convoArr[$arrayIndex][$i] = $defaultValue;
  62. }
  63. return $convoArr;
  64. }
  65. /**
  66. * function load_blank_stack()
  67. * A function to intialise the conversation stack values
  68. *
  69. * @link http://blog.program-o.com/?p=1246
  70. * @param array $convoArr - the current state of the conversation array
  71. * @internal param string $arrayIndex - the array element we are going to intialise
  72. * @internal param string $defaultValue - the value which will be used to set the element
  73. * @return array $convoArr (updated)
  74. */
  75. function load_blank_stack($convoArr)
  76. {
  77. runDebug(__FILE__, __FUNCTION__, __LINE__, "Loading blank stack", 4);
  78. global $stack_value;
  79. //set in global config file
  80. $convoArr['stack']['top'] = $stack_value;
  81. $convoArr['stack']['second'] = $stack_value;
  82. $convoArr['stack']['third'] = $stack_value;
  83. $convoArr['stack']['fourth'] = $stack_value;
  84. $convoArr['stack']['fifth'] = $stack_value;
  85. $convoArr['stack']['sixth'] = $stack_value;
  86. $convoArr['stack']['seventh'] = $stack_value;
  87. $convoArr['stack']['last'] = $stack_value;
  88. return $convoArr;
  89. }
  90. /**
  91. * function load_default_bot_values()
  92. * A function to intialise the chatbot properties
  93. *
  94. * @link http://blog.program-o.com/?p=1248
  95. * @param array $convoArr - the current state of the conversation array
  96. * @return array $convoArr (updated)
  97. */
  98. function load_default_bot_values($convoArr)
  99. {
  100. runDebug(__FILE__, __FUNCTION__, __LINE__, "Loading db bot personality properties", 4);
  101. global $dbConn, $dbn, $bot_id;
  102. //set in global config file
  103. $sql = "SELECT * FROM `$dbn`.`botpersonality` WHERE `bot_id` = '" . $bot_id . "'";
  104. runDebug(__FILE__, __FUNCTION__, __LINE__, "load db bot personality values SQL: $sql", 3);
  105. $result = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__);
  106. foreach ($result as $row)
  107. {
  108. $convoArr['bot_properties'][$row['name']] = $row['value'];
  109. }
  110. return $convoArr;
  111. }
  112. /**
  113. * function write_to_session()
  114. * A function to save the current conversation state to session for the next turn
  115. *
  116. * @link http://blog.program-o.com/?p=1250
  117. * @param array $convoArr - the current state of the conversation array
  118. * @return array $convoArr
  119. */
  120. function write_to_session($convoArr)
  121. {
  122. // TODO: Reduce the convo array to only the barest info necessary before saving
  123. runDebug(__FILE__, __FUNCTION__, __LINE__, "Saving to session", 4);
  124. $_SESSION['programo'] = $convoArr;
  125. return $convoArr;
  126. }
  127. /**
  128. * function read_from_session()
  129. * A function to read the current conversation state from session for this turn
  130. *
  131. * @link http://blog.program-o.com/?p=1252
  132. * @return array $convoArr
  133. */
  134. function read_from_session()
  135. {
  136. runDebug(__FILE__, __FUNCTION__, __LINE__, "Reading from session", 4);
  137. $convoArr = array();
  138. //initialise
  139. if (isset ($_SESSION['programo']))
  140. {
  141. $convoArr = $_SESSION['programo'];
  142. }
  143. return $convoArr;
  144. }
  145. /**
  146. * function add_new_conversation_vars()
  147. * A function add the new values from the user input into the conversation state
  148. *
  149. * @link http://blog.program-o.com/?p=1254
  150. * @param string $say - the user input
  151. * @param array $convoArr - the current state of the conversation array
  152. * @return array $convoArr (updated)
  153. */
  154. function add_new_conversation_vars($say, $convoArr)
  155. {
  156. runDebug(__FILE__, __FUNCTION__, __LINE__, "New conversation vars", 4);
  157. //put what the user has said on the front of the 'user_say' and 'input' subarray with a minimum clean to prevent injection
  158. $convoArr = push_on_front_convoArr("user_say", strip_tags($say), $convoArr);
  159. $convoArr['aiml']['user_raw'] = strip_tags($say);
  160. $convoArr = push_on_front_convoArr('input', $convoArr['aiml']['user_raw'], $convoArr);
  161. return $convoArr;
  162. }
  163. /**
  164. * function add_firstturn_conversation_vars()
  165. * A function add the bot values to the conversation state if this is the first turn
  166. *
  167. * @link http://blog.program-o.com/?p=1256
  168. * @param array $convoArr - the current state of the conversation array
  169. * @return array $convoArr (updated)
  170. */
  171. function add_firstturn_conversation_vars($convoArr)
  172. {
  173. runDebug(__FILE__, __FUNCTION__, __LINE__, "First turn", 4);
  174. if (!isset ($convoArr['bot_properties']))
  175. {
  176. $convoArr = load_default_bot_values($convoArr);
  177. }
  178. return $convoArr;
  179. }
  180. /**
  181. * function push_on_front_convoArr()
  182. * A function to push items on the front of a subarray in convoArr
  183. *
  184. * @link http://blog.program-o.com/?p=1258
  185. * @param string $arrayIndex - the subarray index to push to
  186. * @param string $value - the value to push on teh subarray
  187. * @param array $convoArr - the current state of the conversation array
  188. * @return array $convoArr (updated)
  189. */
  190. function push_on_front_convoArr($arrayIndex, $value, $convoArr)
  191. {
  192. global $rememLimit, $remember_up_to;
  193. runDebug(__FILE__, __FUNCTION__, __LINE__, "Pushing '$value' to the front of the [$arrayIndex] array", 2);
  194. $remember_up_to = (isset($convoArr['conversation']['remember_up_to'])) ? $convoArr['conversation']['remember_up_to'] : $remember_up_to;
  195. //these subarray indexes are 2d
  196. $two_d_arrays = array("that", "that_raw");
  197. $arrayIndex = trim($arrayIndex);
  198. //mini clean
  199. $value = trim($value);
  200. $value = preg_replace('/\s\s+/', ' ', $value);
  201. $value = preg_replace('/\s\./', '.', $value);
  202. //there is a chance the subarray has not been set yet so check and if not set here
  203. if (!isset ($convoArr[$arrayIndex][1]))
  204. {
  205. $convoArr[$arrayIndex] = array();
  206. $convoArr = load_blank_array_element($arrayIndex, "", $convoArr);
  207. }
  208. //if the subarray is itself an array check it here
  209. if (in_array($arrayIndex, $two_d_arrays))
  210. {
  211. $matches = preg_match_all("# ?(([^\.\?!]*)+(?:[\.\?!]|(?:<br ?/?>))*)#ui", $value, $sentences);
  212. $cmatch = 0;
  213. //do another check to make sure the array is not just full of blanks
  214. foreach ($sentences as $temp)
  215. {
  216. foreach ($temp as $chk)
  217. {
  218. if (trim($chk) != "")
  219. {
  220. $cmatch++;
  221. }
  222. }
  223. }
  224. //if there definately is something in the sentence array build the temp sentence array
  225. if (($cmatch > 0) && ($matches !== FALSE))
  226. {
  227. foreach ($sentences[1] as $index => $value)
  228. {
  229. if ($arrayIndex == "that")
  230. {
  231. $t =($value != '') ? clean_that($value, __FILE__, __FUNCTION__, __LINE__) : '';
  232. if ($t != "")
  233. {
  234. $tmp_sentence[] = $t;
  235. }
  236. }
  237. else
  238. {
  239. $tmp_sentence[] = $value;
  240. }
  241. }
  242. //reverse the array and store
  243. $sentences = array();
  244. $sentences = array_reverse($tmp_sentence);
  245. }
  246. else
  247. {
  248. $sentences = array();
  249. if ($arrayIndex == "that")
  250. {
  251. $sentences[0] = clean_that($value, __FILE__, __FUNCTION__, __LINE__);
  252. }
  253. else
  254. {
  255. $sentences[0] = $value;
  256. }
  257. }
  258. //make a space so that [0] is null (in accordance with the AIML array offset)
  259. array_unshift($sentences, NULL);
  260. unset ($sentences[0]);
  261. //push this onto the subarray and then clear [0] element (in accordance with the AIML array offset)
  262. array_unshift($convoArr[$arrayIndex], $sentences);
  263. array_unshift($convoArr[$arrayIndex], null);
  264. unset ($convoArr[$arrayIndex][0]);
  265. }
  266. else
  267. {
  268. array_unshift($convoArr[$arrayIndex], $value);
  269. array_unshift($convoArr[$arrayIndex], NULL);
  270. }
  271. if ((trim($arrayIndex) == 'star') || (trim($arrayIndex) == 'topic'))
  272. {
  273. //keep 5 times as many topics and stars as lines of conversation
  274. $rememLimit_tmp = $rememLimit;
  275. }
  276. else
  277. {
  278. $rememLimit_tmp = $remember_up_to;
  279. }
  280. for ($i = $rememLimit_tmp + 1; $i <= count($convoArr[$arrayIndex]); $i++)
  281. {
  282. if (isset ($convoArr[$arrayIndex][$i]))
  283. {
  284. unset ($convoArr[$arrayIndex][$i]);
  285. }
  286. }
  287. unset ($convoArr[$arrayIndex][0]);
  288. if ($arrayIndex == "topic")
  289. {
  290. push_stack($convoArr, $value);
  291. }
  292. return $convoArr;
  293. }
  294. /**
  295. * function load_bot_config()
  296. * A function to get the bot/convo configuration values out of the database
  297. *
  298. * @param array $convoArr - current state of the conversation
  299. * @return array $convoArr (updated)
  300. */
  301. function load_bot_config($convoArr)
  302. {
  303. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Loading config data for the current bot.', 2);
  304. global $dbConn, $dbn, $format, $pattern, $conversation_lines, $remember_up_to, $debugemail, $debug_level, $debug_mode, $save_state, $error_response;
  305. //get the values from the db
  306. $sql = "SELECT * FROM `$dbn`.`bots` WHERE bot_id = '" . $convoArr['conversation']['bot_id'] . "'";
  307. runDebug(__FILE__, __FUNCTION__, __LINE__, "load bot config SQL: $sql", 3);
  308. $row = db_fetch($sql, null, __FILE__, __FUNCTION__, __LINE__);
  309. if (count($row) > 0)
  310. {
  311. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Loading bot details from the database.', 4);
  312. $convoArr['conversation']['conversation_lines'] = $row['conversation_lines'];
  313. $convoArr['conversation']['remember_up_to'] = $row['remember_up_to'];
  314. $convoArr['conversation']['debugemail'] = $row['debugemail'];
  315. $convoArr['conversation']['debug_level'] = $row['debugshow'];
  316. $convoArr['conversation']['debugmode'] = $row['debugmode'];
  317. $convoArr['conversation']['save_state'] = $row['save_state'];
  318. $convoArr['conversation']['default_aiml_pattern'] = $row['default_aiml_pattern'];
  319. $convoArr['conversation']['bot_parent_id'] = $row['bot_parent_id'];
  320. $error_response = (!empty($row['error_response'])) ? $row['error_response'] : $error_response;
  321. }
  322. else
  323. {
  324. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Unable to load bot details from the database. Loading default values.', 4);
  325. $convoArr['conversation']['conversation_lines'] = $conversation_lines;
  326. $convoArr['conversation']['remember_up_to'] = $remember_up_to;
  327. $convoArr['conversation']['debugemail'] = $debugemail;
  328. $convoArr['conversation']['debug_level'] = $debug_level;
  329. $convoArr['conversation']['debugmode'] = $debug_mode;
  330. $convoArr['conversation']['save_state'] = $save_state;
  331. $convoArr['conversation']['default_aiml_pattern'] = $pattern;
  332. $convoArr['conversation']['bot_parent_id'] = 0;
  333. }
  334. //if return format is not html overide the debug type
  335. if ($convoArr['conversation']['format'] != "html")
  336. {
  337. $convoArr['conversation']['debugmode'] = 1;
  338. }
  339. return $convoArr;
  340. }
  341. /**
  342. * function log_conversation(()
  343. * A function to log the conversation
  344. *
  345. * @link http://blog.program-o.com/?p=1262
  346. * @param array $convoArr - the current state of the conversation array
  347. * @return array $convoArr (updated)
  348. */
  349. function log_conversation($convoArr)
  350. {
  351. //db globals
  352. global $dbConn, $dbn;
  353. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Saving the conversation to the DB.', 2);
  354. //clean and set
  355. $usersay = $convoArr['aiml']['user_raw'];
  356. $usersay = str_replace("'", "\'", $usersay);
  357. $botsay = $convoArr['aiml']['parsed_template'];
  358. $botsay = str_replace("'", "\'", $botsay);
  359. $user_id = $convoArr['conversation']['user_id'];
  360. $convo_id = $convoArr['conversation']['convo_id'];
  361. $bot_id = $convoArr['conversation']['bot_id'];
  362. $sql = "INSERT INTO `$dbn`.`conversation_log` (
  363. `id` ,
  364. `input` ,
  365. `response` ,
  366. `user_id` ,
  367. `convo_id` ,
  368. `bot_id` ,
  369. `timestamp`
  370. )
  371. VALUES (
  372. NULL ,
  373. '$usersay',
  374. '$botsay',
  375. '$user_id',
  376. '$convo_id',
  377. '$bot_id',
  378. CURRENT_TIMESTAMP
  379. )";
  380. runDebug(__FILE__, __FUNCTION__, __LINE__, "Saving conservation SQL: $sql", 3);
  381. $sth = $dbConn->prepare($sql);
  382. $sth->execute();
  383. return $convoArr;
  384. }
  385. /**
  386. * function log_conversation_state(()
  387. * A function to log the conversation state
  388. *
  389. * @link http://blog.program-o.com/?p=1264
  390. * @param array $convoArr - the current state of the conversation array
  391. * @return array $convoArr (updated)
  392. */
  393. function log_conversation_state($convoArr)
  394. {
  395. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Logging the state of the conversation.', 2);
  396. global $dbConn, $dbn, $user_name;
  397. //get undefined defaults from the db
  398. runDebug(__FILE__, __FUNCTION__, __LINE__, "logging state", 4);
  399. runDebug(__FILE__, __FUNCTION__, __LINE__, "user name = $user_name. Stored user name = " . $convoArr['conversation']['user_name'], 4);
  400. $serialise_convo = serialize(reduceConvoArr($convoArr));
  401. $user_id = $convoArr['conversation']['user_id'];
  402. $sql_addon = (!empty ($user_name)) ? "`user_name` = '" . $user_name . "', " : '';
  403. $sql = "UPDATE `$dbn`.`users`
  404. SET
  405. `state` = '$serialise_convo',
  406. `last_update` = NOW(),
  407. $sql_addon
  408. `chatlines` = `chatlines`+1
  409. WHERE `id` = '$user_id' LIMIT 1";
  410. runDebug(__FILE__, __FUNCTION__, __LINE__, "updating conversation state SQL: $sql", 3);
  411. $sth = $dbConn->prepare($sql);
  412. $sth->execute();
  413. return $convoArr;
  414. }
  415. /**
  416. * function get_conversation_state(()
  417. * A function to get the conversation state from the db
  418. *
  419. * @link http://blog.program-o.com/?p=1266
  420. * @param array $convoArr - the current state of the conversation array
  421. * @return array|mixed $convoArr (updated)
  422. */
  423. function get_conversation_state($convoArr)
  424. {
  425. global $dbConn, $dbn,$unknown_user;
  426. runDebug(__FILE__, __FUNCTION__, __LINE__, "getting state", 4);
  427. $user_id = $convoArr['conversation']['user_id'];
  428. $sql = "SELECT * FROM `$dbn`.`users` WHERE `id` = '$user_id' LIMIT 1";
  429. runDebug(__FILE__, __FUNCTION__, __LINE__, "Getting conversation state SQL: $sql", 3);
  430. $row = db_fetch($sql, null, __FILE__, __FUNCTION__, __LINE__);
  431. if (($row) && (count($row) > 0))
  432. {
  433. $convoArr = unserialize($row['state']);
  434. $user_name = (!empty ($row['user_name'])) ? $row['user_name'] : $unknown_user;
  435. $convoArr['conversation']['user_name'] = $user_name;
  436. $convoArr['client_properties']['name'] = $user_name;
  437. }
  438. return $convoArr;
  439. }
  440. /**
  441. * function check_set_bot(()
  442. * A function to check and set the bot id, name and default format for bot
  443. *
  444. * @link http://blog.program-o.com/?p=1269
  445. * @param array $convoArr - the current state of the conversation array
  446. * @return array $convoArr (updated)
  447. */
  448. function check_set_bot($convoArr)
  449. {
  450. global $form_vars, $error_response;
  451. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Checking and/or setting the current bot.', 2);
  452. global $dbConn, $dbn, $bot_id, $error_response, $format,$unknown_user;
  453. //check to see if bot_id has been passed if not load default
  454. if ((isset ($form_vars['bot_id'])) && (trim($form_vars['bot_id']) != ""))
  455. {
  456. $bot_id = trim($form_vars['bot_id']);
  457. }
  458. elseif (isset ($convoArr['conversation']['bot_id']))
  459. {
  460. $bot_id = $convoArr['conversation']['bot_id'];
  461. }
  462. else
  463. {
  464. /** @noinspection PhpSillyAssignmentInspection */
  465. $bot_id = $bot_id;
  466. }
  467. //get the values from the db
  468. $sql = "SELECT * FROM `$dbn`.`bots` WHERE bot_id = '$bot_id' and `bot_active`='1'";
  469. runDebug(__FILE__, __FUNCTION__, __LINE__, "Making sure the bot exists. SQL = $sql", 3);
  470. $row = db_fetch($sql, null, __FILE__, __FUNCTION__, __LINE__);
  471. if (($row) && (count($row) > 0))
  472. {
  473. $bot_name = $row['bot_name'];
  474. $error_response = (!empty($row['error_response'])) ? $row['error_response'] : $error_response;
  475. $unknown_user = $row['unknown_user'];
  476. $convoArr['conversation']['bot_name'] = $bot_name;
  477. $convoArr['conversation']['bot_id'] = $bot_id;
  478. $convoArr['conversation']['format'] = $row['format'];
  479. $convoArr['conversation']['unknown_user'] = $unknown_user;
  480. runDebug(__FILE__, __FUNCTION__, __LINE__, "BOT ID: $bot_id", 2);
  481. }
  482. else
  483. {
  484. $convoArr['conversation']['format'] = $format;
  485. $convoArr['conversation']['bot_id'] = $bot_id;
  486. runDebug(__FILE__, __FUNCTION__, __LINE__, "ERROR - Cannot find bot id: $bot_id", 1);
  487. }
  488. return $convoArr;
  489. }
  490. /**
  491. * function check_set_convo_id(()
  492. * A function to check and set the convo id
  493. *
  494. * @link http://blog.program-o.com/?p=1276
  495. * @param array $convoArr - the current state of the conversation array
  496. * @return array $convoArr (updated)
  497. */
  498. function check_set_convo_id($convoArr)
  499. {
  500. global $form_vars;
  501. //check to see if convo_id has been passed if not load default
  502. if (isset($form_vars['convo_id']))
  503. {
  504. $convo_id = $form_vars['convo_id'];
  505. runDebug(__FILE__, __FUNCTION__, __LINE__, "Obtaining the convo id from form vars. Value: $convo_id", 4);
  506. }
  507. elseif (isset ($convoArr['conversation']['convo_id']))
  508. {
  509. $convo_id = $convoArr['conversation']['convo_id'];
  510. runDebug(__FILE__, __FUNCTION__, __LINE__, "CONVO ID already exists. Value: $convo_id", 2);
  511. }
  512. else
  513. {
  514. $convo_id = session_id();
  515. runDebug(__FILE__, __FUNCTION__, __LINE__, "Cannot find CONVO ID. Using default: $convo_id", 1);
  516. }
  517. $convoArr['conversation']['convo_id'] = $convo_id;
  518. return $convoArr;
  519. }
  520. /**
  521. * function check_set_user(()
  522. * A function to check and set the user's information
  523. *
  524. * @link http://blog.program-o.com/?p=1278
  525. * @param array $convoArr - the current state of the conversation array
  526. * @return array|int $convoArr (updated)
  527. */
  528. function check_set_user($convoArr)
  529. {
  530. global $dbConn, $dbn, $unknown_user;
  531. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Checking and setting the user info, as needed.', 2);
  532. //check to see if user_name has been set if not set as default
  533. $convo_id = (isset ($convoArr['conversation']['convo_id'])) ? $convoArr['conversation']['convo_id'] : session_id();
  534. if (!isset ($convoArr['conversation']['convo_id']))
  535. $convoArr['conversation']['convo_id'] = $convo_id;
  536. $ip = $_SERVER['REMOTE_ADDR'];
  537. $convoArr['client_properties']['ip_address'] = $ip;
  538. $sql = "select `user_name`, `id`, `chatlines` from `$dbn`.`users` where `session_id` = :convo_id limit 1;";
  539. $row = db_fetch($sql, array(':convo_id' => $convo_id), __FILE__, __FUNCTION__, __LINE__);
  540. if ($row === false)
  541. {
  542. $convoArr = intisaliseUser($convoArr);
  543. $user_id = $convoArr['conversation']['user_id'];
  544. $user_name = $unknown_user;
  545. }
  546. else
  547. {
  548. $user_id = $row['id'];
  549. $user_name = (!empty ($row['user_name'])) ? $row['user_name'] : $unknown_user;
  550. }
  551. $chatlines = (!empty ($row['chatlines'])) ? $row['chatlines'] : 0;
  552. $user_name = (!empty ($user_name)) ? $user_name : $unknown_user;
  553. $convoArr['conversation']['user_name'] = $user_name;
  554. $convoArr['conversation']['user_id'] = $user_id;
  555. $convoArr['client_properties']['name'] = $user_name;
  556. $convoArr['conversation']['totallines'] = $chatlines;
  557. return $convoArr;
  558. }
  559. /**
  560. * function check_set_format(()
  561. * A function to check and set the conversation output type
  562. *
  563. * @link http://blog.program-o.com/?p=1281
  564. * @param array $convoArr - the current state of the conversation array
  565. * @return array $convoArr (updated)
  566. */
  567. function check_set_format($convoArr)
  568. {
  569. global $format, $form_vars;
  570. $formatsArr = array('html', 'xml', 'json');
  571. if ((isset ($form_vars['format'])) && (trim($form_vars['format']) != ""))
  572. {
  573. $desired_format = strtolower(trim($form_vars['format']));
  574. }
  575. else
  576. {
  577. $desired_format = $format;
  578. }
  579. if (!in_array($format, $formatsArr))
  580. {
  581. $convoArr['conversation']['format'] = $format; // default format
  582. $convoArr['debug']['intialisation_error'] = "Incompatible return type: $format";
  583. runDebug(__FILE__, __FUNCTION__, __LINE__, "ERROR - bad return type: $format", 1);
  584. }
  585. else
  586. {
  587. //at this point we can overwrite the conversation format.
  588. $convoArr['conversation']['format'] = $desired_format;
  589. runDebug(__FILE__, __FUNCTION__, __LINE__, "Using format: $format", 4);
  590. }
  591. return $convoArr;
  592. }
  593. /**
  594. * function load_that(()
  595. * A function to load the previous bot responses into the convoArr['that'] array
  596. *
  597. * @link http://blog.program-o.com/?p=1283
  598. * @param array $convoArr - the current state of the conversation array
  599. * @return array $convoArr (updated)
  600. */
  601. function load_that($convoArr)
  602. {
  603. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Loading the THAT array.', 2);
  604. global $dbConn, $dbn, $remember_up_to, $bot_id;
  605. $remember_up_to = (!empty ($convoArr['conversation']['remember_up_to'])) ? $convoArr['conversation']['remember_up_to'] : $remember_up_to;
  606. $user_id = $convoArr['conversation']['user_id'];
  607. $bot_id = (!empty($convoArr['conversation']['bot_id'])) ? $convoArr['conversation']['bot_id'] : $bot_id;
  608. $limit = $remember_up_to;
  609. $sql = "select `input`, `response` from `$dbn`.`conversation_log` where `user_id` = $user_id and `bot_id` = $bot_id order by `id` desc limit $limit;"; // desc
  610. runDebug(__FILE__, __FUNCTION__, __LINE__, "Getting conversation log entries for the current user. SQL:\n$sql", 3);
  611. $result = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__);
  612. if ($result)
  613. {
  614. $tmpThatRows = array();
  615. $tmpInputRows = array();
  616. $tmpThat = array();
  617. $tmpInput = array();
  618. $puncuation = array(',', '?', ';', '!');
  619. foreach ($result as $row)
  620. {
  621. $tmpThatRows[] = $row['response'];
  622. $tmpInputRows[] = $row['input'];
  623. }
  624. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Inputs returned:' . print_r($tmpInputRows, true), 1);
  625. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Loading previous responses into the ~THAT~ array.', 4);
  626. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Responses returned:' . print_r($tmpThatRows, true), 1);
  627. $tmpThatRows = array_reverse($tmpThatRows);
  628. foreach ($tmpThatRows as $row)
  629. {
  630. $row = str_replace($puncuation, '.', $row);
  631. $tmpThat[] = explode('.', $row);
  632. }
  633. array_unshift($tmpThat, NULL);
  634. unset ($tmpThat[0]);
  635. foreach ($tmpThat as $index => $value)
  636. {
  637. $value = implode_recursive(' ', $value, __FILE__, __FUNCTION__, __LINE__);
  638. $value = clean_that($value, __FILE__, __FUNCTION__, __LINE__);
  639. $convoArr = push_on_front_convoArr('that', $value, $convoArr);
  640. }
  641. runDebug(__FILE__, __FUNCTION__, __LINE__, 'Loading previous user inputs into the ~INPUT~ array.', 4);
  642. $tmpInputRows = array_reverse($tmpInputRows);
  643. foreach ($tmpInputRows as $row)
  644. {
  645. $row = str_replace($puncuation, '.', $row);
  646. $tmpInput[] = explode('.', $row);
  647. }
  648. array_unshift($tmpThat, NULL);
  649. unset ($tmpThat[0]);
  650. foreach ($tmpInput as $index => $value)
  651. {
  652. $value = implode_recursive(' ', $value, __FILE__, __FUNCTION__, __LINE__);
  653. $value = clean_that($value, __FILE__, __FUNCTION__, __LINE__);
  654. $convoArr = push_on_front_convoArr('input', $value, $convoArr);
  655. }
  656. }
  657. else runDebug(__FILE__, __FUNCTION__, __LINE__, 'Couldn\'t find any previous inputs or responses.', 4);
  658. return $convoArr;
  659. }