-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.php
More file actions
112 lines (99 loc) · 3.49 KB
/
backend.php
File metadata and controls
112 lines (99 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php ob_start();
define('DIALOG_SOURCE_FILE', "questions.json");
define('MYSQL_USER',"user");
define('MYSQL_PWD',"password");
define('MYSQL_DB',"itscript_test");
Abstract class Node{
private $question = '';
private $id = null;
private $ansvers = array();
private $type = 'radio';
public function __construct($args){
$this->id=$args['id'];
$this->question=$args['question'];
$this->ansvers=$args['ansvers'];
$this->type=$args['type'];
}
public function params(){
return array(
'id'=>$this->id,
'type'=>$this->type,
'ansvers'=>$this->ansvers,
'question'=>$this->question
);
}
public function work($args){
return '';
}
}
class RootNode extends Node{
public function work($args){
switch ($args['ansver']) {
case 1:
$next_node = controller_get_node(2);
return array('next_dialog'=>$next_node->params());
case 2:
$next_node = controller_get_node(1);
return array('next_dialog'=>$next_node->params());
case 3:
$next_node = controller_get_node(2);
return array('next_dialog'=>$next_node->params());
default:
return array('error'=>'Wrong ansver');
}
}
}
class OrderNumber extends Node
{
public function work($args){
return array('result'=>'Заказ номер '.$args['ansver'].' доставлен.');
}
}
class SomethingHappened extends Node
{
public function work($args){
return array('result'=>'Как печально, то что случилось: '.$args['ansver']);
}
}
function controller_get_node($id){
$questions_file=fopen(DIALOG_SOURCE_FILE, "r");
$questions=json_decode(stream_get_contents($questions_file),true);
fclose($questions_file);
if (isset($questions[$id])){
$class=$questions[$id]['class'];
return new $class($questions[$id]);
} else {
return null;
}
}
function log_to_db($client,$dialog_id,$request,$response){
require_once 'lib/safemysql.class.php';
$opts = array(
'user' => MYSQL_USER,
'pass' => MYSQL_PWD,
'db' => MYSQL_DB
);
$db = new SafeMySQL($opts);
$db->query("CREATE TABLE IF NOT EXISTS `logs` (`client_id` VARCHAR(50),`dialog_id` INT, `request` TEXT, `response` TEXT)");
$db->query("INSERT INTO `logs` VALUES (?s,?i,?s,?s)",$client,$dialog_id,$request,$response);
}
session_start();
if (isset($_REQUEST['dialog-id'])){
$node = controller_get_node($_REQUEST['dialog-id']);
if ($node){
$result = $node->work($_REQUEST);
} else {
$result = array('error'=>'node not found');
}
log_to_db(session_id(),$_REQUEST['dialog-id'],json_encode($_REQUEST),json_encode($result));
} else {
session_regenerate_id(true);
$result = array(
'session_id'=>session_id(),
'node'=>controller_get_node(0)->params()
);
log_to_db(session_id(),0,'New client',json_encode($result));
}
ob_end_flush();
exit(json_encode($result));
?>