Repository navigation
Expand file tree
/
Copy pathsynapse.php
More file actions
360 lines (330 loc) · 12.9 KB
/
Copy pathsynapse.php
File metadata and controls
360 lines (330 loc) · 12.9 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
use WHMCS\Database\Capsule;
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
require_once __DIR__ . '/includes/version.php';
require_once __DIR__ . '/includes/synapse_shared.php';
function synapse_config()
{
return [
'name' => 'Synapse AI Autopilot',
'description' => 'Enterprise AI automation for customer support tickets with VM management integration.',
'version' => SYNAPSE_ADDON_VERSION,
'author' => 'YecoAI',
'language' => 'english',
'fields' => [
'license_key' => [
'FriendlyName' => 'License Key',
'Type' => 'password',
'Size' => '40',
'Description' => 'Your Synapse license key (SYN-XXXX-XXXX-XXXX). HMAC signatures and payload encryption are derived from this key automatically.',
],
'backend_url' => [
'FriendlyName' => 'Backend URL',
'Type' => 'text',
'Size' => '50',
'Default' => 'https://api-synapse.yecoai.com/api/v1',
'Description' => 'Synapse API base URL including /api/v1 (e.g. https://api-synapse.yecoai.com/api/v1)',
],
'confidence_threshold' => [
'FriendlyName' => 'Confidence Threshold (%)',
'Type' => 'text',
'Size' => '5',
'Default' => '85',
'Description' => 'Minimum AI confidence required for automatic actions (50-99)',
],
'auto_close' => [
'FriendlyName' => 'Auto-Close Resolved Tickets',
'Type' => 'yesno',
'Description' => 'Automatically close tickets when AI provides complete solution',
],
'debug_mode' => [
'FriendlyName' => 'Debug Mode',
'Type' => 'yesno',
'Description' => 'Enable detailed logging for troubleshooting',
],
'callback_secret' => [
'FriendlyName' => 'Callback HMAC Secret',
'Type' => 'password',
'Size' => '64',
'Description' => 'Filled automatically from the Synapse backend. Used to sign backend-to-WHMCS callbacks.',
],
'api_admin' => [
'FriendlyName' => 'WHMCS API Admin Username',
'Type' => 'text',
'Size' => '40',
'Description' => 'Dedicated admin username used for localAPI calls. Create a least-privilege admin and set it here.',
],
'whitelist_ips' => [
'FriendlyName' => 'Backend IP Whitelist',
'Type' => 'textarea',
'Rows' => '3',
'Description' => 'Comma-separated list of allowed backend IPs (optional)',
],
]
];
}
function synapse_activate()
{
$query = "CREATE TABLE IF NOT EXISTS `synapse_tickets` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`whmcs_ticket_id` int(10) NOT NULL,
`synapse_ticket_id` varchar(64) NOT NULL,
`confidence` decimal(5,2) DEFAULT '0.00',
`action_taken` varchar(255) DEFAULT NULL,
`ai_decision` enum('autopilot','copilot','escalated') DEFAULT 'escalated',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `whmcs_ticket_id` (`whmcs_ticket_id`),
KEY `synapse_ticket_id` (`synapse_ticket_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
full_query($query);
$query = "CREATE TABLE IF NOT EXISTS `synapse_config` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`department_id` int(10) NOT NULL,
`department_name` varchar(255) NOT NULL,
`ai_mode` enum('copilot','autopilot') DEFAULT 'copilot',
`enabled` tinyint(1) DEFAULT '1',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `department_id` (`department_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
full_query($query);
$query = "CREATE TABLE IF NOT EXISTS `synapse_queue` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`whmcs_ticket_id` int(10) NOT NULL,
`is_reply` tinyint(1) NOT NULL DEFAULT '0',
`status` varchar(20) NOT NULL DEFAULT 'pending',
`event_type` varchar(32) NOT NULL DEFAULT 'ingest',
`attempts` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
`last_error` text DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`processed_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `status` (`status`),
KEY `whmcs_ticket_id` (`whmcs_ticket_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
full_query($query);
return [
'status' => 'success',
'description' => 'Synapse AI Autopilot activated successfully. Configure your license key to begin.',
];
}
function synapse_deactivate()
{
return [
'status' => 'success',
'description' => 'Synapse AI Autopilot deactivated. Database tables preserved.',
];
}
function synapse_upgrade($vars)
{
$version = $vars['version'];
$query = "CREATE TABLE IF NOT EXISTS `synapse_queue` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`whmcs_ticket_id` int(10) NOT NULL,
`is_reply` tinyint(1) NOT NULL DEFAULT '0',
`status` varchar(20) NOT NULL DEFAULT 'pending',
`event_type` varchar(32) NOT NULL DEFAULT 'ingest',
`attempts` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
`last_error` text DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`processed_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `status` (`status`),
KEY `whmcs_ticket_id` (`whmcs_ticket_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
full_query($query);
if (class_exists('WHMCS\\Database\\Capsule') && Capsule::schema()->hasTable('synapse_queue') && !Capsule::schema()->hasColumn('synapse_queue', 'message')) {
Capsule::schema()->table('synapse_queue', function ($table) {
$table->text('message')->nullable();
});
}
if (class_exists('WHMCS\\Database\\Capsule') && Capsule::schema()->hasTable('synapse_queue') && !Capsule::schema()->hasColumn('synapse_queue', 'reply_id')) {
Capsule::schema()->table('synapse_queue', function ($table) {
$table->integer('reply_id')->nullable();
});
}
if (class_exists('WHMCS\\Database\\Capsule') && Capsule::schema()->hasTable('synapse_queue') && !Capsule::schema()->hasColumn('synapse_queue', 'event_type')) {
Capsule::schema()->table('synapse_queue', function ($table) {
$table->string('event_type', 32)->default('ingest');
});
}
if (class_exists('WHMCS\\Database\\Capsule') && Capsule::schema()->hasTable('synapse_config')) {
try {
Capsule::table('synapse_config')->where('ai_mode', 'observe')->update(['ai_mode' => 'copilot']);
} catch (Exception $e) {
}
try {
full_query("ALTER TABLE `synapse_config` MODIFY `ai_mode` enum('copilot','autopilot') DEFAULT 'copilot'");
} catch (Exception $e) {
}
}
return [
'status' => 'success',
'description' => "Synapse AI Autopilot upgraded to version {$version}.",
];
}
function synapse_cron($vars)
{
if (!defined('SYNAPSE_CALLBACK_SKIP_MAIN')) {
define('SYNAPSE_CALLBACK_SKIP_MAIN', true);
}
if (file_exists(__DIR__ . '/callback.php')) {
require_once __DIR__ . '/callback.php';
if (function_exists('synapseCleanupNonces')) {
synapseCleanupNonces();
}
}
if (file_exists(__DIR__ . '/hooks.php')) {
require_once __DIR__ . '/hooks.php';
if (function_exists('synapseSweepStaleApprovals')) {
synapseSweepStaleApprovals(100);
}
if (function_exists('synapseProcessQueue')) {
synapseProcessQueue(25);
}
}
}
function synapseAdminUrl($modulelink, $tab, $extra = [])
{
$parts = [];
$parsed = parse_url((string) $modulelink);
$path = $parsed['path'] ?? 'addonmodules.php';
if (!empty($parsed['query'])) {
parse_str($parsed['query'], $parts);
}
unset($parts['action']);
$parts['module'] = $parts['module'] ?? 'synapse';
$parts['synapse_tab'] = $tab;
foreach ($extra as $key => $value) {
if ($value === null || $value === '') {
continue;
}
$parts[$key] = $value;
}
return $path . '?' . http_build_query($parts);
}
function synapseAdminAjaxUrl($modulelink, $action, $extra = [])
{
$parts = [];
$parsed = parse_url((string) $modulelink);
$path = $parsed['path'] ?? 'addonmodules.php';
if (!empty($parsed['query'])) {
parse_str($parsed['query'], $parts);
}
unset($parts['action']);
$parts['module'] = $parts['module'] ?? 'synapse';
$parts['synapse_ajax'] = $action;
foreach ($extra as $key => $value) {
if ($value === null || $value === '') {
continue;
}
$parts[$key] = $value;
}
return $path . '?' . http_build_query($parts);
}
function synapse_output($vars)
{
$ajaxAction = (string) ($_REQUEST['synapse_ajax'] ?? '');
if ($ajaxAction !== '') {
require_once __DIR__ . '/includes/ajax_handlers.php';
synapseRequireAdmin();
synapseRunAjaxAndExit($ajaxAction);
}
$license_key = $vars['license_key'];
$backend_url = rtrim($vars['backend_url'], '/');
$debug_mode = $vars['debug_mode'];
if (empty($license_key) || empty($backend_url)) {
echo '<div class="alert alert-warning">
<strong>Configuration Required:</strong> Please configure your license key and backend URL.
</div>';
return;
}
$modulelink = $vars['modulelink'];
$synapse_tab = (string) ($_REQUEST['synapse_tab'] ?? $_GET['synapse_tab'] ?? 'overview');
$allowed_tabs = ['overview', 'update', 'departments', 'tickets', 'diagnostics', 'logs'];
if (!in_array($synapse_tab, $allowed_tabs, true)) {
$synapse_tab = 'overview';
}
$csrfToken = '';
if (class_exists('WHMCS\\Session')) {
$csrfToken = (string) WHMCS\Session::get('synapseCsrfToken');
}
if ($csrfToken === '' && !empty($_SESSION['synapseCsrfToken'])) {
$csrfToken = (string) $_SESSION['synapseCsrfToken'];
}
if ($csrfToken === '') {
$csrfToken = bin2hex(random_bytes(16));
}
if (class_exists('WHMCS\\Session')) {
WHMCS\Session::set('synapseCsrfToken', $csrfToken);
}
$_SESSION['synapseCsrfToken'] = $csrfToken;
echo '<script>window.synapseCsrfToken = ' . json_encode($csrfToken) . ';</script>';
echo '<div class="synapse-admin-panel">';
include __DIR__ . '/templates/navigation.php';
switch ($synapse_tab) {
case 'update':
include __DIR__ . '/templates/update.php';
break;
case 'departments':
include __DIR__ . '/templates/departments.php';
break;
case 'diagnostics':
include __DIR__ . '/templates/diagnostics.php';
break;
case 'tickets':
include __DIR__ . '/templates/tickets.php';
break;
case 'logs':
include __DIR__ . '/templates/logs.php';
break;
default:
include __DIR__ . '/templates/dashboard.php';
}
echo '</div>';
echo '<style>
.synapse-admin-panel { margin-top: 20px; }
.synapse-nav { border-bottom: 1px solid #ddd; margin-bottom: 20px; }
.synapse-nav a {
display: inline-block;
padding: 10px 15px;
margin-right: 5px;
text-decoration: none;
border-bottom: 2px solid transparent;
}
.synapse-nav a.active {
border-bottom-color: #0073aa;
font-weight: bold;
}
.synapse-card {
background: white;
border: 1px solid #ddd;
border-radius: 4px;
padding: 20px;
margin-bottom: 20px;
}
.synapse-metric {
text-align: center;
padding: 15px;
background: #f8f9fa;
border-radius: 4px;
margin-bottom: 15px;
}
.synapse-metric h3 { margin: 0 0 5px 0; font-size: 24px; }
.synapse-metric p { margin: 0; color: #666; }
.synapse-status {
padding: 3px 8px;
border-radius: 3px;
font-size: 11px;
font-weight: bold;
}
.status-healthy { background: #d4edda; color: #155724; }
.status-warning { background: #fff3cd; color: #856404; }
.status-error { background: #f8d7da; color: #721c24; }
</style>';
}