PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/chatbot/conversation_start.php

http://github.com/Program-O/Program-O
PHP | 268 lines | 171 code | 44 blank | 53 comment | 12 complexity | 2cbfa24c31bec415382d0bff032ff031 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /***************************************
  3. * http://www.program-o.com
  4. * PROGRAM O
  5. * Version: 2.6.11
  6. * FILE: chatbot/conversation_start.php
  7. * AUTHOR: Elizabeth Perreau and Dave Morton
  8. * DATE: FEB 01 2016
  9. * DETAILS: this file is the landing page for all calls to access the bots
  10. ***************************************/
  11. set_time_limit(0);
  12. $time_start = microtime(true);
  13. $script_start = $time_start;
  14. $last_timestamp = $time_start;
  15. $thisFile = __FILE__;
  16. $configPath = (defined('_CONF_PATH_')) ? _CONF_PATH_ : '../config/';
  17. require_once("{$configPath}global_config.php");
  18. //load shared files
  19. /** @noinspection PhpIncludeInspection */
  20. require_once(_LIB_PATH_ . 'PDO_functions.php');
  21. /** @noinspection PhpIncludeInspection */
  22. include_once(_LIB_PATH_ . "error_functions.php");
  23. /** @noinspection PhpIncludeInspection */
  24. include_once(_LIB_PATH_ . 'misc_functions.php');
  25. ini_set('default_charset', $charset);
  26. //------------------------------------------------------------------------
  27. // Error Handler
  28. //------------------------------------------------------------------------
  29. // set to the user defined error handler
  30. set_error_handler("myErrorHandler");
  31. //open db connection
  32. // Collect system specs for the first debug message
  33. $versionCheckSQL = 'select version();';
  34. $result = db_fetch($versionCheckSQL, null, __FILE__, __FUNCTION__, __LINE__);
  35. $mySQL_version = $result['version()'];
  36. $pgoVersion = VERSION;
  37. $phpVersion = phpversion();
  38. $serverSoftware = $_SERVER['SERVER_SOFTWARE'];
  39. $mbEnabled = (IS_MB_ENABLED) ? 'true' : 'false';
  40. $firstDebugMessage = "Conversation starting. Current system specs:
  41. Program O version: $pgoVersion
  42. Server Software: $serverSoftware
  43. PHP Version: $phpVersion
  44. OS: $os
  45. OS Version: $osv
  46. MySQL Version: $mySQL_version
  47. Multi-byte functions enabled: $mbEnabled";
  48. //leave this first debug call in as it wipes any existing file for this session
  49. runDebug(__FILE__, __FUNCTION__, __LINE__, $firstDebugMessage, 0);
  50. //load all the chatbot functions
  51. /** @noinspection PhpIncludeInspection */
  52. include_once(_BOTCORE_PATH_ . "aiml" . $path_separator . "load_aimlfunctions.php");
  53. //load all the user functions
  54. /** @noinspection PhpIncludeInspection */
  55. include_once(_BOTCORE_PATH_ . "conversation" . $path_separator . "load_convofunctions.php");
  56. //load all the user functions
  57. /** @noinspection PhpIncludeInspection */
  58. include_once(_BOTCORE_PATH_ . "user" . $path_separator . "load_userfunctions.php");
  59. //load all the user addons
  60. /** @noinspection PhpIncludeInspection */
  61. include_once(_ADDONS_PATH_ . "load_addons.php");
  62. runDebug(__FILE__, __FUNCTION__, __LINE__, "Loaded all Includes", 4);
  63. //initialise globals
  64. $convoArr = array();
  65. $convoArrStack = array();
  66. $new_convo_id = false;
  67. $old_convo_id = false;
  68. $say = '';
  69. $display = "";
  70. $options = array(
  71. 'convo_id' => FILTER_SANITIZE_STRING,
  72. 'bot_id' => FILTER_SANITIZE_NUMBER_INT,
  73. 'say' => array(
  74. 'filter' => FILTER_SANITIZE_STRING,
  75. 'flags' => FILTER_FLAG_NO_ENCODE_QUOTES
  76. ),
  77. 'format' => FILTER_SANITIZE_STRING,
  78. 'debug_mode' => FILTER_SANITIZE_NUMBER_INT,
  79. 'debug_level' => FILTER_SANITIZE_NUMBER_INT,
  80. 'name' => FILTER_SANITIZE_STRING
  81. );
  82. $form_vars_post = filter_input_array(INPUT_POST, $options);
  83. $form_vars_get = filter_input_array(INPUT_GET, $options);
  84. $form_vars = array_merge((array)$form_vars_get, (array)$form_vars_post);
  85. if (!isset($form_vars['say']))
  86. {
  87. $form_vars['say'] = '';
  88. }
  89. $say = ($say !== '') ? $say : trim($form_vars['say']);
  90. $session_name = 'PGOv' . VERSION;
  91. session_name($session_name);
  92. session_start();
  93. /** @noinspection PhpUndefinedVariableInspection */
  94. $debug_level = (isset($_SESSION['programo']['conversation']['debug_level'])) ? $_SESSION['programo']['conversation']['debug_level'] : $debug_level;
  95. $debug_level = (isset($form_vars['debug_level'])) ? $form_vars['debug_level'] : $debug_level;
  96. /** @noinspection PhpUndefinedVariableInspection */
  97. $debug_mode = (isset($form_vars['debug_mode'])) ? $form_vars['debug_mode'] : $debug_mode;
  98. if (isset($form_vars['convo_id']))
  99. {
  100. session_id($form_vars['convo_id']);
  101. }
  102. $convo_id = session_id();
  103. runDebug(__FILE__, __FUNCTION__, __LINE__, "Debug level: $debug_level" . PHP_EOL . "session ID = $convo_id", 0);
  104. //if the user has said something
  105. runDebug(__FILE__, __FUNCTION__, __LINE__, "Conversation continuing. User said '{$say}'.", 4);
  106. if (!empty($say)) {
  107. // Chect to see if the user is clearing properties
  108. $lc_say = _strtolower($say);
  109. $convoArr = read_from_session();
  110. if ($lc_say == 'clear properties' || $lc_say == ':reset bot')
  111. {
  112. runDebug(__FILE__, __FUNCTION__, __LINE__, "Clearing client properties and starting over.", 4);
  113. $_SESSION = array();
  114. $user_id = (isset($convoArr['conversation']['user_id'])) ? $convoArr['conversation']['user_id'] : -1;
  115. /** @noinspection SqlDialectInspection */
  116. $sql = "DELETE FROM `$dbn`.`client_properties` WHERE `user_id` = :user_id;";
  117. $params = array(':user_id' => $user_id);
  118. $numRows = db_write($sql, $params, false, __FILE__, __FUNCTION__, __LINE__);
  119. $convoArr['client_properties'] = null;
  120. $convoArr['conversation'] = array();
  121. $convoArr['conversation']['user_id'] = $user_id;
  122. // Get old convo id, to use for later
  123. $old_convo_id = session_id();
  124. // Note: This will destroy the session, and not just the session data!
  125. // Finally, destroy the session.
  126. runDebug(__FILE__, __FUNCTION__, __LINE__, "Generating new session ID.", 4);
  127. session_regenerate_id(true);
  128. $new_convo_id = session_id();
  129. $params = session_get_cookie_params();
  130. setcookie($session_name, $new_convo_id, time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
  131. // Update the users table, and clear out any unused client properties as needed
  132. /** @noinspection SqlDialectInspection */
  133. $sql = "UPDATE `$dbn`.`users` SET `session_id` = '$new_convo_id' WHERE `session_id` = '$old_convo_id';";
  134. runDebug(__FILE__, __FUNCTION__, __LINE__, "Update user - SQL:\n$sql", 3);
  135. $confirm = db_write($sql, null, false, __FILE__, __FUNCTION__, __LINE__);
  136. // Get user id, so that we can clear the client properties
  137. /** @noinspection SqlDialectInspection */
  138. $sql = "SELECT `id` FROM `$dbn`.`users` WHERE `session_id` = :new_convo_id limit 1;";
  139. $params = array(':new_convo_id' => $new_convo_id);
  140. $row = db_fetch($sql, $params, __FILE__, __FUNCTION__, __LINE__);
  141. if ($row !== false) {
  142. $user_id = $row['id'];
  143. $convoArr['conversation']['user_id'] = $user_id;
  144. $convoArr['conversation']['convo_id'] = $new_convo_id;
  145. runDebug(__FILE__, __FUNCTION__, __LINE__, "User ID = $user_id.", 4);
  146. /** @noinspection SqlDialectInspection */
  147. $sql = "DELETE FROM `$dbn`.`client_properties` WHERE `user_id` = $user_id;";
  148. runDebug(__FILE__, __FUNCTION__, __LINE__, "Clear client properties from the DB - SQL:\n$sql", 4);
  149. }
  150. $say = "Hello";
  151. }
  152. // Load bot and user stored values
  153. $convoArr = load_default_bot_values($convoArr);
  154. $convoArr = check_set_convo_id($convoArr);
  155. $convoArr = check_set_bot($convoArr);
  156. $convoArr = check_set_user($convoArr);
  157. // run any pre-processing addons
  158. $convoArr = run_pre_input_addons($convoArr, $say);
  159. $say = $convoArr['say'];
  160. $rawSay = $say;
  161. $convoArr['conversation']['rawSay'] = $rawSay;
  162. $say = normalize_text($say);
  163. $bot_id = (isset($form_vars['bot_id'])) ? $form_vars['bot_id'] : $bot_id;
  164. runDebug(__FILE__, __FUNCTION__, __LINE__, "Details:\nUser say: " . $say . "\nConvo id: " . $convo_id . "\nBot id: " . $bot_id . "\nFormat: " . $form_vars['format'], 2);
  165. if (!empty($form_vars['name']))
  166. {
  167. $user_name = $form_vars['name'];
  168. $convoArr['conversation']['user_name'] = $user_name;
  169. $convoArr['client_properties']['name'] = $user_name;
  170. }
  171. if (!isset($convoArr['conversation']['user_id']) && isset($user_id))
  172. {
  173. $convoArr['conversation']['user_id'] = $user_id;
  174. }
  175. $convoArr = check_set_format($convoArr);
  176. $convoArr = load_that($convoArr);
  177. //save_file(_LOG_PATH_ . 'ca.txt', print_r($convoArr,true));
  178. $convoArr = buildNounList($convoArr);
  179. $convoArr['time_start'] = $time_start;
  180. $convoArr = load_bot_config($convoArr);
  181. //if totallines isn't set then this is new user
  182. runDebug(__FILE__, __FUNCTION__, __LINE__, "Default debug level = $debug_level", 0);
  183. $debug_level = isset($convoArr['conversation']['debug_level']) ? $convoArr['conversation']['debug_level'] : $debug_level;
  184. runDebug(__FILE__, __FUNCTION__, __LINE__, "Current debug level = $debug_level", 0);
  185. if (!isset ($convoArr['conversation']['totallines'])) {
  186. //load the chatbot configuration for a new user
  187. $convoArr = intialise_convoArray($convoArr);
  188. //add the bot_id dependant vars
  189. $convoArr = add_firstturn_conversation_vars($convoArr);
  190. $convoArr['conversation']['totallines'] = 0;
  191. $convoArr = get_user_id($convoArr);
  192. }
  193. $convoArr['aiml'] = array();
  194. //add the latest thing the user said
  195. runDebug(__FILE__, __FUNCTION__, __LINE__, "Say = $say: raw say = $rawSay", 0);
  196. $convoArr = add_new_conversation_vars($say, $convoArr);
  197. //parse the aiml
  198. $convoArr = make_conversation($convoArr);
  199. $convoArr = run_mid_level_addons($convoArr);
  200. $convoArr = log_conversation($convoArr);
  201. #$convoArr = log_conversation_state($convoArr);
  202. $convoArr = write_to_session($convoArr);
  203. $convoArr = get_conversation($convoArr);
  204. $convoArr = run_post_response_useraddons($convoArr);
  205. //return the values to display
  206. $display = $convoArr['send_to_user'];
  207. $time_start = $convoArr['time_start'];
  208. unset($convoArr['nounList']);
  209. }
  210. else
  211. {
  212. $convoArr = intialise_convoArray($convoArr);
  213. runDebug(__FILE__, __FUNCTION__, __LINE__, "Conversation initialized, awaiting user", 2);
  214. $convoArr['send_to_user'] = 'User input not detected.';
  215. $cva = print_r($convoArr, true);
  216. //error_log("Convo Array:\n$cva", 3, _LOG_PATH_ . 'convoArr.txt');
  217. }
  218. runDebug(__FILE__, __FUNCTION__, __LINE__, "Closing Database", 2);
  219. $time_end = microtime(true);
  220. $time = number_format(round(($time_end - $script_start) * 1000, 7), 3);
  221. display_conversation($convoArr);
  222. runDebug(__FILE__, __FUNCTION__, __LINE__, "Conversation Ending. Elapsed time: $time milliseconds.", 0);
  223. $convoArr = handleDebug($convoArr, $time); // Make sure this is the last line in the file, so that all debug entries are captured.
  224. pgo_session_gc();