-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.php
571 lines (489 loc) · 19.5 KB
/
cron.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<html>
<head>
<title>Cyclone - Cron job</title>
<style>
body {
background-color: #222222;
color: #00f000;
font-family: Monospace;
font-size: 12px;
}
strong {
font-weight: bolder;
color: #ffffff;
}
.load_module {
color: #888888;
}
.warning {
color: yellow;
}
.error {
color: #f00000;
}
.rule_status {
color: #888888;
}
.rule_trigger {
color: #0D58A6;
}
.rule_filter {
color: #4188D2;
}
.rule_action {
color: #689CD2;
}
</style>
</head>
<body>
<?php
/*
- Created by Torin
- This script is to be executed as a cron job every 5 minutes or so
- Implemented with PHP for its direct access to MySQL
- Any text output from this script is completely optional for debugging purpose only
- How it works:
- This script goes through all modules and call their generic fetch/update function
- The module code supposes to perform their magic and push new events to (global) events table
*/
//In case something breaks in the future
/*
$index = strrpos(__FILE__, '/') + 1;
$path = substr(__FILE__, 0, $index);
chdir($path);
*/
//phase 1
require_once("util_users.php");
require_once("util_modules.php");
require_once("util_rules.php");
require_once("util_configs.php");
require_once("cron_helper.php");
echo date(DATE_ISO8601) . "<br/>\n";
echo realpath(dirname(__FILE__)) . "<br/>\n";
//Global variables
$user_id = 0;
$modules_array = array();
$configs_array = array();
//Fake authentication data for testing
$input_user_name = "ACD49441-44BA-6D95-84B4-E6E3AF40CC84";
$input_user_password = "";
$md5_user_password = ($input_user_password == "") ? "" : md5($input_user_password);
echo "user_name: $input_user_name (strlen=" . strlen($input_user_name) . ")<br/>\n";
//-----------------------------------------------------------
// Obtain a 'user_id' to be used throughout the session
$user_id = func_authorizeUser($input_user_name, $md5_user_password);
echo "user_id: <strong>$user_id</strong><br/>\n";
if ($user_id <= 0) {
echo "<span class='error'>ERROR:</span>Authorization failed. Halt.<br/>\n";
die();
}
echo "Authorization success. Continue...<br/>\n";
echo "<br/>\n\n";
//-----------------------------------------------------------
// Retrieve configurations for all modules for this user
// and hash them by module_id for convenient access later
$sql_result_configs = func_getConfigsByUserID($user_id);
while ($sql_result_configs != null && $one_record = mysql_fetch_assoc($sql_result_configs))
{
$module_id = $one_record['module_id'];
if (!isset($configs_array[$module_id]) || $configs_array[$module_id] == null)
$configs_array[$module_id] = array();
$configs_array[$module_id][] = $one_record;
}
$sql_result_configs = null;
//-----------------------------------------------------------
// Display a list of trigger modules, add metadata
echo "-----------------------------------------------------------<br/>\n";
echo " All trigger modules:<br/>\n";
echo "-----------------------------------------------------------<br/>\n";
echo " <ul>\n";
$sql_result_triggers = func_getTriggerModules();
while ($one_record = mysql_fetch_assoc($sql_result_triggers))
{
$module_id = $one_record['module_id'];
$module_name = $one_record['module_name'];
$module_type = $one_record['module_type'];
$module_description = $one_record['module_description'];
$module_type_string = "";
if (intval($module_type) == 0) $module_type_string = "Standalone";
else if (intval($module_type) == 1) $module_type_string = "Hardware";
else if (intval($module_type) == 2) $module_type_string = "Software API";
$modules_array[$module_id] = $one_record;
echo " <li><strong>$module_name</strong> - $module_type_string - $module_description</li>\n";
}
$sql_result_triggers = null;
echo " </ul>\n";
//-----------------------------------------------------------
// Execute the headless triggers
echo "-----------------------------------------------------------<br/>\n";
echo " Perform triggers:<br/>\n";
echo "-----------------------------------------------------------<br/>\n";
$pretty_end_string = " </li>\n\n";
echo " <ul>\n\n";
foreach ($modules_array as $module_id => $one_module)
{
//try loading module, skip if fail, print out pretty output along the way
echo " <li>\n";
echo " <span class='load_module'>\n";
$module_loaded = load_module( $one_module, true );
if ($module_loaded == false) {
echo " </span>\n";
echo $pretty_end_string;
continue;
}
$one_module = $modules_array[$module_id];
$module_object = $one_module['module_object'];
//Print out number of triggers/triggers for this module
$number_of_triggers = $module_object->getNumberOfTriggers();
$number_of_headless_triggers = $module_object->getNumberOfHeadlessTriggers();
if ($number_of_triggers == null || $number_of_triggers <= 0) {
echo " module has no triggers/triggered. Skipping... <br/>\n";
echo " <span class='warning'>WARNING:</span> Please create at least 1 trigger/trigger for this module.<br/>\n";
echo " </span>\n";
echo $pretty_end_string;
continue;
}
echo " module has " . $number_of_triggers . " possible triggers(s)<br/>\n";
echo " module has " . $number_of_headless_triggers . " headless triggers(s)<br/>\n";
echo " </span>\n";
//Perform headless trigger
echo " ";
$event_ids = $module_object->trigger_headless();
if (count($event_ids) <= 0)
echo " module generated no headless trigger<br/>\n";
//Array of none headless trigger IDs
$none_headless_trigger_array = $module_object->getNoneHeadlessTriggerIDArray();
//No none-headless triggers to perform
if (count($none_headless_trigger_array) <= 0) {
echo " module generated no param-based trigger<br/>\n";
echo $pretty_end_string;
continue;
}
//Retrieve all user's rules with these trigger IDs
$trigger_rule_array = array();
$sql_result_rules = func_getRulesByUserIDAndTriggerIDs($user_id, $none_headless_trigger_array);
while ($sql_result_rules != null && $one_record = mysql_fetch_assoc($sql_result_rules))
{
$rule_id = $one_record['rule_id'];
$trigger_rule_array[$rule_id] = $one_record;
}
$sql_result_rules = null;
//Perform trigger with parameters
foreach ($trigger_rule_array as $rule_id => $one_rule)
{
$rule_id = $one_rule['rule_id'];
$rule_trigger_id = $one_rule['trigger_id'];
$the_rule = $one_rule;
$rule_trigger_param = $the_rule['trigger_param'];
//This is only applicable to param-based triggers: no param, no go
if ($rule_trigger_param == null || strlen($rule_trigger_param) < 5)
continue;
echo " perform trigger_id $rule_trigger_id with params: $rule_trigger_param<br/>\n";
echo " ";
$trigger_id = $module_object->trigger($rule_trigger_id, $rule_trigger_param, $rule_id);
$event_ids = $trigger_id;
}
echo $pretty_end_string;
}
echo " </ul>\n\n";
//-----------------------------------------------------------
// Phase 2, retrieve all new events in the global events queue & filter them
//-----------------------------------------------------------
echo "-----------------------------------------------------------<br/>\n";
echo "Process events<br/>\n";
echo "-----------------------------------------------------------<br/>\n";
require_once("util_events.php");
require_once("util_rules.php");
require_once("util_filters.php");
require_once("util_actions.php");
$event_array = array();
$rule_array = array();
$trigger_id_array = array();
//constants
$event_ignored_rule_status_status = 1; //no rule to process this trigger
$event_ignored_error_module_status = 2; //module is not loaded to process this trigger
$event_ignored_error_filter_module_status = 3; //module is not loaded to filter this trigger
$event_ignored_error_filter_module_no_filter_status = 4; //module is loaded but no filter
$event_ignored_error_action_module_status = 5; //module is not loaded to take action for this trigger
$event_ignored_error_action_error_status = 6; //error occured while performing action
$event_pending_action_status = 30; //passed filters, waiting for action
$event_filtered_status = 31; //being blocked by filter, no action needed
$event_done_action_status = 32; //action has been taken
//-----------------------------------------------------------
// Retrieve new events in queue for this user
$sql_result_queue = func_getEventsByUserID($user_id);
while ($sql_result_queue != null && $one_record = mysql_fetch_assoc($sql_result_queue))
{
$event_id = $one_record['event_id'];
$trigger_id = $one_record['trigger_id'];
$event_param = $one_record['event_param'];
$event_array[] = $one_record;
$trigger_id_array[$trigger_id] = $trigger_id;
}
$sql_result_queue = null;
$num_queue = count($event_array);
if ($num_queue <= 0) {
echo "<strong>No events to process. Done!</strong><br/>\n";
die();
}
echo "Number of events: <strong>$num_queue</strong><br/>\n";
//-----------------------------------------------------------
// Retrieve user's rules
$sql_result_rules = func_getRulesByUserIDAndTriggerIDs($user_id, $trigger_id_array);
while ($sql_result_rules != null && $one_record = mysql_fetch_assoc($sql_result_rules))
{
$rule_id = $one_record['rule_id'];
$rule_array[$rule_id] = $one_record;
}
$sql_result_rules = null;
$num_rules = count($rule_array);
if ($num_rules <= 0) {
echo "<strong>No rule to process. Done!</strong><br/>\n";
//flag all events (of user_id) as ignored
for ($event_index=0; $event_index < count($event_array); $event_index++)
func_updateEventStatus($event_id, $event_ignored_rule_status_status);
die();
}
echo "Number of rules: <strong>$num_rules</strong><br/>\n";
//-----------------------------------------------------------
// Process the queue
echo " <ul>\n\n";
for ($event_index=0; $event_index < count($event_array); $event_index++)
{
$one_queue = $event_array[$event_index];
$event_id = $one_queue['event_id'];
$trigger_id = $one_queue['trigger_id'];
$trigger_rule_id = intval($one_queue['rule_id']);
$event_param = $one_queue['event_param'];
$event_param_array = json_decode($event_param);
$event_status = $one_queue['event_status'];
$last_updated = $one_queue['last_updated'];
//indicate that this event is not a global event
//and should only be processed by matching rule_id
if ($trigger_rule_id == null || $trigger_rule_id == "null" || $trigger_rule_id <= 0)
$trigger_rule_id = null;
echo " <li>\n";
if ($trigger_rule_id == null) echo " <strong>event_id $event_id</strong> (global)<br/>\n";
else echo " <strong>event_id $event_id</strong> (for rule $trigger_rule_id only)<br/>\n";
echo " <span class='rule_trigger'>\n";
echo " trigger_id $trigger_id - event_param $event_param <br/>\n";
echo " </span>\n";
//Check if we have any rules set for this trigger
$the_rule = null;
foreach ($rule_array as $rule_id => $one_rule)
{
$rule_id = $one_rule['rule_id'];
$rule_trigger_id = $one_rule['trigger_id'];
if ($rule_trigger_id != $trigger_id)
continue;
$the_rule = $one_rule;
}
//No rules set for this trigger, flag status as 'ignored. no rules'
if ($the_rule == null) {
func_updateEventStatus($event_id, $event_ignored_rule_status_status);
echo " <span class='rule_status'>\n";
echo " no rule to process this event. ignored. <br/>\n";
echo " </span>\n";
echo $pretty_end_string;
continue;
}
//-----------------------------------------------------------
// Process the rule
//Again, for each rule, apply it if it matches the trigger_id & rule_id
$the_rule = null;
foreach ($rule_array as $rule_id => $one_rule)
{
$rule_id = $one_rule['rule_id'];
$rule_trigger_id = $one_rule['trigger_id'];
$rule_trigger_param = $one_rule['trigger_param'];
$rule_trigger_param_array = json_decode($rule_trigger_param);
if ($rule_trigger_id != $trigger_id)
continue;
if ($trigger_rule_id != null && $trigger_rule_id != $rule_id)
continue;
//event_param must contain ALL trigger_param (not the other way)
$matching_params = true;
if ($rule_trigger_param_array != null && count($rule_trigger_param_array) > 0)
{
foreach ($rule_trigger_param_array as $rule_trigger_param_name => $rule_trigger_param_value)
{
$found = false;
foreach ($event_param_array as $event_param_array_name => $event_param_array_value)
{
if ($rule_trigger_param_name != $event_param_array_name)
continue;
if ($rule_trigger_param_value != $event_param_array_value)
continue;
$found = true;
break;
}
if ($found)
continue;
$matching_params = false;
break;
}
}
//Next rule
if (!$matching_params)
continue;
$the_rule = $one_rule;
echo " <span class='rule_status'>\n";
echo " applying rule_id $rule_id... <br/>\n";
echo " </span>\n";
//--------------------------
//Filter section
echo " <span class='rule_filter'>\n";
//Get the filter ID & filter param in this rule
$filter_id = intval( $the_rule['filter_id'] );
//No filter, just pass through
$filter_passed = false;
if ($filter_id <= 0) {
$filter_passed = true;
echo " bypass filter<br/>\n";
}
$filter_param = $the_rule['filter_param'];
//Has a filter, load filter module & execute it to find the result
$sql_filter_module = null;
$module_id = 0;
$module_loaded = false;
if ($filter_passed == false)
{
//get the module that handles this filter
$sql_filter_module = func_getModuleByFilterID( $filter_id );
//no module to handle this filter, flag status as 'ignored. no filter module'
if ($sql_filter_module == null)
{
func_updateEventStatus($event_id, $event_ignored_error_filter_module_status);
echo " no module to filter this trigger. ignored. <br/>\n";
echo " </span>\n";
echo $pretty_end_string;
continue;
//next rule
}
//Load the filter module
$module_id = $sql_filter_module['module_id'];
$module_already_loaded = func_isModuleLoaded($module_id);
if ($module_already_loaded == false) {
echo " <span class='load_module'>\n";
$module_loaded = load_module($sql_filter_module, true);
echo " </span>\n";
if ($module_loaded == false) {
func_updateEventStatus($event_id, $event_ignored_error_filter_module_status);
echo " no module to filter this trigger. ignored. <br/>\n";
echo " </span>\n";
echo $pretty_end_string;
continue;
//next rule
}
}
//Store the module object for reuse
$one_module = $modules_array[$module_id];
$module_object = $one_module['module_object'];
//Print out number of filters for this module
$number_of_filters = $module_object->getNumberOfFilters();
if ($number_of_filters == null || $number_of_filters <= 0) {
func_updateEventStatus($event_id, $event_ignored_error_filter_module_no_filter_status);
echo " filter module contains no filters. Skipping... <br/>\n";
if ($module_already_loaded == false)
echo " <span class='warning'>WARNING:</span> Please create at least 1 filter for this module.<br/>\n";
echo " </span>\n";
echo $pretty_end_string;
continue;
//next rule
}
//print once
if ($module_already_loaded == false)
echo " filter module contains " . $number_of_filters . " possible filter(s)<br/>\n";
//Get the result of filtering
$filter_passed = $module_object->filter($event_param, $filter_id, $filter_param);
echo " filter result: " . ($filter_passed == true ? "passed" : "blocked") . "<br/>\n";
if ($filter_passed == false) {
func_updateEventStatus($event_id, $event_filtered_status);
echo " </span>\n";
echo $pretty_end_string;
continue;
//next rule
}
} //end of filtering (if filter_id presents)
echo " </span>\n";
//--------------------------
// Action section
//Get the action ID & action param in this rule
$action_id = intval( $the_rule['action_id'] );
//No action, just pass through
$action_ok = false;
if ($action_id <= 0)
$action_ok = true;
$action_param = $the_rule['action_param'];
//Has an action, load action module & execute it to find the result
$sql_action_module = null;
$module_id = 0;
$module_loaded = false;
if ($action_ok == false)
{
//get the module that handles this action
$sql_action_module = func_getModuleByActionID( $action_id );
//no module to handle this action, flag status as 'ignored. no action module'
if ($sql_action_module == null)
{
func_updateEventStatus($event_id, $event_ignored_error_action_module_status);
echo " no module to action this trigger. ignored. <br/>\n";
echo $pretty_end_string;
continue;
//next rule
}
$module_id = $sql_action_module['module_id'];
echo " <span class='load_module'>\n";
$module_loaded = load_module($sql_action_module, true);
}
//Load the action module
if ($module_loaded == false) {
//func_updateEventStatus($event_id, $event_ignored_error_action_module_status);
echo " <span class='warning'>WARNING:</span> no module to perform action. ignored. <br/>\n";
echo " </span>\n";
echo $pretty_end_string;
continue;
//next rule
}
$one_module = $modules_array[$module_id];
$module_object = $one_module['module_object'];
//Print out number of actions for this module
$number_of_actions = $module_object->getNumberOfActions();
if ($number_of_actions == null || $number_of_actions <= 0) {
echo " module contains no actions. Skipping... <br/>\n";
echo " <span class='warning'>WARNING:</span> Please create at least 1 action for this module.<br/>\n";
echo " </span>\n";
echo $pretty_end_string;
continue;
}
echo " module contains " . $number_of_actions . " possible action(s)<br/>\n";
echo " </span>\n";
echo " <span class='rule_action'>\n";
//Get the result of action
echo " perform action_id $action_id with action params: $action_param<br/>\n";
$action_result = $module_object->action($action_id, $event_param, $action_param);
echo " action result: " . ($action_result != null ? "ok" : "not-ok") . "<br/>\n";
echo " </span>\n";
if ($action_result == null) {
func_updateEventStatus($event_id, $event_ignored_error_action_error_status);
continue;
//next rule
}
//Action success, flag as done
func_updateEventStatus($event_id, $event_done_action_status);
echo $pretty_end_string;
} //end of for-loop for each rule
//No rules set for this trigger (no rules matching event parameters), flag status as 'ignored. no rules'
if ($the_rule == null) {
func_updateAsyncEventStatus($event_id, $event_ignored_rule_status_status);
echo " <span class='rule_status'>\n";
echo " no rule to process this event. ignored. <br/>\n";
echo " </span>\n";
echo $pretty_end_string;
continue;
}
} //end of for-loop for event processing
echo " </ul>\n";
?>
</body>
</html>