-
Notifications
You must be signed in to change notification settings - Fork 4
/
nodejs.php
157 lines (126 loc) · 3.13 KB
/
nodejs.php
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @file
* Provide a listener class to handle all messages from Node.js server.
*/
require_once('../../class2.php');
e107_require_once(e_PLUGIN . 'nodejs/nodejs.main.php');
/**
* Class NodejsListener.
*/
class NodejsListener
{
/**
* Constructor function.
*/
public function __construct()
{
if(!isset($_POST['serviceKey']) || !nodejs_is_valid_service_key($_POST['serviceKey']))
{
header('Content-Type: application/json');
echo nodejs_json_encode(array('error' => 'Invalid service key.'));
exit();
}
if(!isset($_POST['messageJson']))
{
header('Content-Type: application/json');
echo nodejs_json_encode(array('error' => 'No message.'));
exit();
}
$message = json_decode($_POST['messageJson'], true);
// $log = e107::getLog();
// $log->add('NODEJS', $message, E_LOG_INFORMATIVE, '');
$this->message_handler($message);
}
/**
* Callback: handles all messages from Node.js server.
*/
public function message_handler($message)
{
$response = array();
switch($message['messageType'])
{
case 'authenticate':
$response = nodejs_auth_check($message);
break;
case 'userOffline':
if(empty($message['uid']))
{
$response['error'] = 'Missing uid for userOffline message.';
}
else
{
if(!preg_match('/^\d+$/', $message['uid']))
{
$response['error'] = 'Invalid (!/^\d+$/) uid for userOffline message.';
}
else
{
nodejs_user_set_offline($message['uid']);
$response['message'] = "User {$message['uid']} set offline.";
}
}
break;
default:
$handlers = array();
$handlers += self::get_message_handlers($message['messageType']);
foreach($handlers as $callback)
{
$callback($message, $response);
}
}
// TODO provides the way to alter response message.
$var = $response ? $response : array('error' => 'Not implemented');
// $log = e107::getLog();
// $log->add('NODEJS', (array) $var, E_LOG_INFORMATIVE, '');
header('Content-Type: application/json');
echo nodejs_json_encode($var);
exit();
}
/**
* Get a list of message handlers are defined in plugins.
*/
function get_message_handlers($type)
{
$sql = e107::getDb();
$handlers = array();
$enabledPlugins = array();
// Get list of enabled plugins.
$sql->select("plugin", "*", "plugin_id !='' order by plugin_path ASC");
while($row = $sql->fetch())
{
if($row['plugin_installflag'] == 1)
{
$enabledPlugins[] = $row['plugin_path'];
}
}
$addonList = e107::getPlugConfig('nodejs')->get('nodejs_addon_list', array());
foreach($addonList as $plugin)
{
if(in_array($plugin, $enabledPlugins))
{
$file = e_PLUGIN . $plugin . '/e_nodejs.php';
if(is_readable($file))
{
e107_require_once($file);
$addonClass = $plugin . '_nodejs';
if(class_exists($addonClass))
{
$addon = new $addonClass();
if(method_exists($addon, 'msgHandlers'))
{
$return = $addon->msgHandlers($type);
if(is_array($return))
{
$handlers[] = $return;
}
}
}
}
}
}
return $handlers;
}
}
// Class installation.
new NodejsListener();