-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsonisweb_integration.module
executable file
·779 lines (699 loc) · 29.4 KB
/
sonisweb_integration.module
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
<?php
// $Id: sonisweb_integration.module,v 1.11 2010/06/30 18:40:09 antgiant Exp $
/**
* @file sonisweb_integration.module
* Provides a set of developer functions to be used for integrating with SONISWEB.
*/
/**
* Implements hook_help().
*/
function sonisweb_integration_help($path, $arg) {
switch ($path) {
case 'admin/modules#description':
return t("Provides a set of developer functions to be used for integrating with SONISWEB.");
}
}
/**
* Implements hook_menu().
*
*/
function sonisweb_integration_menu() {
$items['admin/config/sonisweb_integration'] = array(
'title' => 'SONISWEB Integration Settings',
'description' => 'Configure the SONISWEB Integration.',
'page callback' => 'drupal_get_form',
'page arguments' => array('sonisweb_integration_admin_settings'),
'access arguments' => array('administer sonisweb integration'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/sonisweb_integration/check_availability'] = array(
'title' => t('Sonis availability'),
'description' => t('Sonis availability'),
'access arguments' => array('access content'),
'page callback' => 'check_availability',
'file' => 'sonisweb_integration.availability.inc',
'file path' => drupal_get_path('module', 'sonisweb_integration'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function sonisweb_integration_permission() {
return array(
'administer sonisweb integration' => array(
'title' => t('administer sonisweb integration'),
'description' => t('TODO Add a description for \'administer sonisweb integration\''),
),
);
}
/**
* Implementation of hook_init()
*
* This is where the error message to display when Sonis is down is initialized
*/
function sonisweb_integration_init()
{
$availability = variable_get('sonisweb_integration_availability', 'on');
$availabilityError = variable_get('sonisweb_integration_availability_error_message', '');
if (($availability == 'down' || $availability == 'off') && $availabilityError != '') {
// Set a site wide error message on all pages if Sonis is down
drupal_set_message(t($availabilityError), 'warning');
}
}
/**
* Provides admin settings pages.
*/
function sonisweb_integration_admin_settings($form, $form_state) {
$tmp = variable_get('cache_lifetime', 0);
$form['sonisweb_integration_caching_length'] = array(
'#type' => 'select',
'#title' => t('Cache lifetime'),
'#default_value' => variable_get('sonisweb_integration_caching_length', '0'),
'#options' => array(
'0' => t('Disable caching'),
'1' => t('Page load'),
'60' => t('1 min' . ($tmp <= 60 ? "" : "*")),
'180' => t('3 min' . ($tmp <= 180 ? "" : "*")),
'300' => t('5 min' . ($tmp <= 300 ? "" : "*")),
'600' => t('10 min' . ($tmp <= 600 ? "" : "*")),
'900' => t('15 min' . ($tmp <= 900 ? "" : "*")),
'1800' => t('30 min' . ($tmp <= 1800 ? "" : "*")),
'2700' => t('45 min' . ($tmp <= 2700 ? "" : "*")),
'3600' => t('1 hour' . ($tmp <= 3600 ? "" : "*")),
'10800' => t('3 hours' . ($tmp <= 10800 ? "" : "*")),
'21600' => t('6 hours' . ($tmp <= 21600 ? "" : "*")),
'32400' => t('9 hours' . ($tmp <= 32400 ? "" : "*")),
'43200' => t('12 hours' . ($tmp <= 43200 ? "" : "*")),
'86400' => t('1 day' . ($tmp <= 86400 ? "" : "*")),
),
'#required' => TRUE,
'#description' => t("<p>Caching allows Drupal to store the results of a SONISWEB call locally. This will speed up repeated SONISWEB calls. If enabled how long should Drupal consider the cached SONISWEB result to be valid before checking against SONISWEB again?</p><p>(NOTE: Page load allows Drupal to prevent redundant SONISWEB calls within a single page load, but does not preserve data beyond that single page load.)</p>" . ($tmp <= 60 ? "" : "<p><strong class='error'>* means value will be overridden by the sitewide <a href='!url'>Minimum cache lifetime</a> setting.</strong></p>"), array('!url' => url("admin/config/development/performance", array('absolute' => TRUE)))),
);
$form['sonisweb_integration_availability'] = array(
'#type' => 'select',
'#title' => t('Sonis availability'),
'#default_value' => variable_get('sonisweb_integration_availability', 'on'),
'#options' => array(
'on' => t('On'),
'down' => t('Down'),
'off' => t('Off, don\'t check if available'),
),
'#required' => TRUE,
'#description' => t('<p>The Sonis availability will be set to Down if an API call times out after 60 seconds. The module will continue to check every 15 minutes if Sonis is back up. If Sonis will be down for an extended period (for example. if updates are being made), the module should be set to Off and will remain off until an administrator manually turns it back on again. No API calls will be made when set to Down or Off.</p>'),
);
$form['sonisweb_integration_availability_error_message'] = array(
'#type' => 'textfield',
'#title' => t('Custom error message when Sonis is down'),
'#default_value' => variable_get('sonisweb_integration_availability_error_message', ''),
'#description' => t("Provide a custom error message to display when Sonis is not available."),
);
$form['sonisweb_integration_availability_email_addresses'] = array(
'#type' => 'textfield',
'#title' => t('E-mail alerts'),
'#default_value' => variable_get('sonisweb_integration_availability_email_addresses', ''),
'#description' => t("A comma separated list of e-mail addresses to be notified when Sonis is detected as unavailable."),
);
$form['sonisweb_integration_soap_base_url'] = array(
'#type' => 'textfield',
'#title' => t('Base URL'),
'#default_value' => variable_get('sonisweb_integration_soap_base_url', ''),
'#required' => TRUE,
'#description' => t("Enter the base url to the SOAP functionality. Usually in the form of https://sonis.example.com/unencrypted/ " .
"(Note: It is highly recommended that this url be secured via https.)"),
);
$form['sonisweb_integration_admin_user'] = array(
'#type' => 'textfield',
'#title' => t('Administrative Username'),
'#default_value' => variable_get('sonisweb_integration_admin_user', ''),
'#required' => TRUE,
'#description' => t("Enter the administrative username to be used to connect to the API & SQL URLs."),
);
$form['sonisweb_integration_admin_password'] = array(
'#type' => 'password',
'#title' => t('Administrative Password ' . (variable_get('sonisweb_integration_admin_password', '') != '' ? "(Set)" : "")),
'#required' => FALSE,
'#description' => t("Enter the administrative password to be used to connect to the API & SQL URLs."),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Settings'),
);
return $form;
}
/**
* Implements hook_flush_caches().
*
* This tells Drupal's cache handling system the name of our caching
* table, so expired items will be purged automatically and this table
* also affected by the empty all caches function.
*/
function sonisweb_integration_flush_caches() {
return array('cache_sonisweb_integration');
}
/**
* Validates values submitted to admin settings pages.
*/
function sonisweb_integration_admin_settings_validate($form, &$form_state) {
//Require a password only if one is not stored.
if (variable_get('sonisweb_integration_admin_password', '') == "" &&
(!isset($form_state['values']['sonisweb_integration_admin_password']) ||
$form_state['values']['sonisweb_integration_admin_password'] == "")) {
form_set_error('sonisweb_integration_admin_password', t('Administrative Password field is a required.'));
}
$pass = variable_get('sonisweb_integration_admin_password', '');
if ($pass == "" || $form_state['values']['sonisweb_integration_admin_password'] != "") {
$pass = $form_state['values']['sonisweb_integration_admin_password'];
}
if (!_sonisweb_integration_test('api', $form_state['values']['sonisweb_integration_soap_base_url'], $form_state['values']['sonisweb_integration_admin_user'], $pass) || !_sonisweb_integration_test('api', $form_state['values']['sonisweb_integration_soap_base_url'], $form_state['values']['sonisweb_integration_admin_user'], $pass)) {
form_set_error('sonisweb_integration_soap_base_url', t('The Base URL provided is not functioning. (Is the account enabled and the Base URL correct?)'));
}
// Validate that Sonis is available
if ($form_state['values']['sonisweb_integration_availability'] == 'on') {
if(!sonisweb_integration_test_sql()) {
form_set_error('sonisweb_integration_availability', t('Sonis is not currently available. '));
}
}
}
/**
* Stores values submitted to admin settings pages.
*/
function sonisweb_integration_admin_settings_submit($form, &$form_state) {
variable_set('sonisweb_integration_caching_length', $form_state['values']['sonisweb_integration_caching_length']);
variable_set('sonisweb_integration_availability', $form_state['values']['sonisweb_integration_availability']);
if (strripos($form_state['values']['sonisweb_integration_soap_base_url'], "/") != strlen($form_state['values']['sonisweb_integration_soap_base_url']) - 1) {
$form_state['values']['sonisweb_integration_soap_base_url'] .= "/";
}
variable_set('sonisweb_integration_soap_base_url', $form_state['values']['sonisweb_integration_soap_base_url']);
variable_set('sonisweb_integration_admin_user', $form_state['values']['sonisweb_integration_admin_user']);
//Don't overwrite password with empty unless necessary.
if (isset($form_state['values']['sonisweb_integration_admin_user']) && $form_state['values']['sonisweb_integration_admin_user'] != "" &&
isset($form_state['values']['sonisweb_integration_admin_password']) && $form_state['values']['sonisweb_integration_admin_password'] != "") {
variable_set('sonisweb_integration_admin_password', $form_state['values']['sonisweb_integration_admin_password']);
}
if(isset($form_state['values']['sonisweb_integration_availability_error_message'])) {
variable_set('sonisweb_integration_availability_error_message', $form_state['values']['sonisweb_integration_availability_error_message']);
}
if(isset($form_state['values']['sonisweb_integration_availability_email_addresses'])) {
variable_set('sonisweb_integration_availability_email_addresses', $form_state['values']['sonisweb_integration_availability_email_addresses']);
}
drupal_set_message(t('Settings saved.'));
}
/**
* Performs any SONISWEB API call. (See http://sonis.example.com/CFIDE/componentutils/componentdoc.cfm
* for API documentation.)
*
* @param $component
* The SONISWEB component containing the relevant method.
*
* @param $method
* The SONISWEB method to be executed.
*
* @param $hasReturnVariable
* Does the SONISWEB method have a return variable or does it just output to screen?
* Yes = Has return variable.
* No = Outputs directly to screen.
*
* @param $argumentdata
* An array of arrays containing the arguments and values needed for the method call.
* (i.e. array(array('arg1', 'value1'), array('arg2', 'value2'), ...) However, it should be noted
* that values of @username & @password will be transformed to their respective
* stored admin_user & admin_password values.
*
* @param $use_cached
* A true or false variable that determines if a cached value should be used. (Defaults to using cached value.)
*
* @return
* The result of the API Call.
*/
function sonisweb_integration_api($component, $method, $hasReturnVariable, $argumentdata, $use_cached = TRUE) {
//$make_call allows for skipping the API call if needed.
$make_call = TRUE;
$return_value = "";
//Allow other modules to interact with this API call before it is performed
module_invoke_all('sonisweb_integration_api_call_start', $component, $method, $hasReturnVariable, $argumentdata, $make_call, $return_value);
if ($make_call) {
$soap = _sonisweb_integration_setup_soap('api');
//Transform @username & @password into actual values.
array_walk_recursive($argumentdata, "_sonisweb_integration_replace_data");
$return_value = _sonisweb_integration_make_soap_call($use_cached, "api", $argumentdata, $component, $method, $hasReturnVariable);
//Allow other modules to interact with this API call after it is completed
module_invoke_all('sonisweb_integration_api_call_end', $component, $method, $hasReturnVariable, $argumentdata, $make_call, $return_value);
}
return ($return_value);
}
/**
* Runs any read-only SQL Query against the SONISWEB database.
*
* @param $sql
* The SQL Query to be run against the SONISWEB database.
*
* @param $use_cached
* A true or false variable that determines if a cached value should be used. (Defaults to using cached value.)
*
* @return
* A array in the format of array(mysql_fetch_assoc()).
* (i.e. 0 => ([course] => 'English', ...), 1 => ([course] => 'Math', ...)
* (NOTE: Upon error the value will be stored in the array under the key error.)
*/
function sonisweb_integration_sql($sql, $use_cached = TRUE) {
//$make_call allows for skipping the SQL call if needed.
$make_call = TRUE;
$return_value = "";
//Allow other modules to interact with this SQL call before it is performed.
module_invoke_all('sonisweb_integration_sql_call_start', $sql, $make_call, $return_value);
if ($make_call) {
$return_value = _sonisweb_integration_make_soap_call($use_cached, "sql", $sql);
//Allow other modules to interact with this SQL call after it is completed but before the sql_transform is performed.
module_invoke_all('sonisweb_integration_sql_call_pre_transform', $sql, $make_call, $return_value);
$return_value = sonisweb_integration_sql_transform($return_value);
//Allow other modules to interact with this SQL call after it is completed.
module_invoke_all('sonisweb_integration_sql_call_end', $sql, $make_call, $return_value);
}
return ($return_value);
}
/**
* Converts an html drop box output by CFC.drp_box into a Form API friendly
* values array.
*
* @param $dropbox
* The html drop box created by the CDC.drp_box call.
*
* @return
* An array compatible with the #options part of the Drupal Forms API.
*/
function sonisweb_integration_dropbox_to_form_api_options_array($dropbox) {
$dropbox = preg_replace("/\s/", " ", $dropbox);
$dropbox = preg_replace("/<select( [^ ]+)+?[ ]{0,1}>/i", "", $dropbox);
$resulta = preg_replace("/.*?\"(.*?)\".*?/", "$1|", $dropbox);
$resulta = explode("|", $resulta);
array_push($resulta, preg_replace("/>.*$/", "$1", array_pop($resulta)));
array_pop($resulta);
$resultb = preg_replace("/(<option.*?>|<\/select>)/i", "|", $dropbox);
$resultb = preg_replace("/<.*?>/", "", $resultb);
$resultb = preg_replace("/(^[^\|]*?[\|])/", "", $resultb);
$resultb = explode("|", $resultb);
array_push($resultb, preg_replace("/\/.*$/", "$1", array_pop($resultb)));
array_pop($resultb);
$dropbox = array_combine($resulta, $resultb);
return ($dropbox);
}
/**
* Converts ColdFusion query format to mysql_fetch_assoc() format.
*
* @param $result
* A SQL Query result in ColdFusion query format.
*
* @return
* A SQL Query result in the format of array(mysql_fetch_assoc()).
* (i.e. 0 => ([course] => 'English', ...), 1 => ([course] => 'Math', ...).
* NOTE: Upon failure to convert it returns $result untouched.
*/
function sonisweb_integration_sql_transform($result) {
$out = array();
//Only perform transform on properly populated data sets.
if (isset($result['data']) && is_array($result['data']) && $result['data'] != array()) {
foreach ($result['data'] as $row) {
$tmp = array();
$i = 0;
foreach ($result['columnList'] as $column) {
$tmp[$column] = $row[$i++];
}
$out[] = $tmp;
}
}
if ($out == array()) {
return ($result);
}
else {
return ($out);
}
}
/**
* Converts a sql result from ColdFusion format to Form API options format.
*
* @param $result
* A SQL Query result in ColdFusion query or sonisweb_integration_sql format.
*
* @param $code
* The code to be submitted by the form.
*
* @param $display
* The text the form should display to the user.
*
* @return
* An array compatible with the #options part of the Drupal Forms API.
*/
function sonisweb_integration_sql_to_options($results, $code = '', $display = '') {
if (isset($results['data']) && is_array($results['data']) && $results['data'] != array()) {
if ($code != '') {
$code = array_search(strtoupper($code), $results['columnList']);
}
else {
$code = $results['columnList'][0];
}
if ($display != '') {
$display = array_search(strtoupper($display), $results['columnList']);
}
else {
$display = $results['columnList'][1];
}
$out = array();
foreach ($results['data'] as $row) {
$out[trim($row[$code])] = trim($row[$display]);
}
}
else {
$out = array();
if ($code == '') {
$code = array_keys($results[0]);
$code = $code[0];
}
if ($display == '') {
$display = array_keys($results[0]);
$display = $display[1];
}
foreach ($results as $row) {
$out[trim($row[$code])] = trim($row[$display]);
}
}
if ($out == array()) {
return ($results);
}
else {
return ($out);
}
}
/**
* Tests the availability of Sonis by running a simple SQL query.
*
* @return
* Boolean indicating whether the test is successful or not.
*/
function sonisweb_integration_test_sql() {
// Make a simple test sql call to check the availability of Sonis
if(_sonisweb_integration_test('sql', variable_get('sonisweb_integration_soap_base_url'),
variable_get('sonisweb_integration_admin_user'),
variable_get('sonisweb_integration_admin_password'))) {
return TRUE;
}
else {
return FALSE;
}
}
/* -----------------------------------------------------------------------------
*
* Functions below this point are for internal use only!
*
* -----------------------------------------------------------------------------
*/
/**
* Private function. Do not use!
*/
function _sonisweb_integration_test($type, $url, $user, $pass) {
switch ($type) {
case 'api':
$url .= 'soapapi.cfc';
break;
case 'sql':
$url .= 'soapsql.cfc';
break;
}
if ($type == 'api') {
$result = _sonisweb_integration_make_soap_call(FALSE, 'apiTest', array(
array('sonis_ds', "#sonis.ds#"),
array('allow_blank', 'Yes'),
array('multi_select', 'no'),
array('hide', 'no'),
array('hide_excludes', 'no')), "CFC.drp_box", "program", "no", $url, $user, $pass);
if (stripos($result, 'name="prg_cod"') === FALSE) {
return FALSE;
}
else {
return TRUE;
}
}
elseif ($type == 'sql') {
$result = _sonisweb_integration_make_soap_call(FALSE, 'sqlTest', "select top 1 soc_sec from name", "", "", "", $url, $user, $pass);
if (!isset($result->data)) {
return FALSE;
}
else {
return TRUE;
}
}
}
/**
* Private function. Do not use!
*/
function _sonisweb_integration_setup_soap($call_type, $turl = '') {
$url = variable_get('sonisweb_integration_soap_base_url', '');
//Warn Integration Admin's of Insecure Base URL.
global $user;
switch ($call_type) {
case 'api':
$url .= 'soapapi.cfc';
break;
case 'sql':
$url .= 'soapsql.cfc';
break;
case 'test':
$url = $turl;
break;
}
if (user_access('administer sonisweb integration') === TRUE && strtolower(substr($url, 0, 8)) != "https://") {
drupal_set_message(t('SONISWEB Integration Base URL is not secure. Administrator username and password has been sent unencrypted. <a href="@settings">Fix it here.</a>', array('@settings' => url('admin/config/sonisweb_integration'))), 'warning');
}
try {
//Get a handle to the webservice
$client = new SoapClient($url . "?wsdl", array("features" => SOAP_SINGLE_ELEMENT_ARRAYS,
"connection_timeout" => 5));
}
catch (Exception $e) {
watchdog('sonisweb_integration', 'Error establishing SOAP connection (%error)', array('%error' => $e));
return FALSE;
}
return $client;
}
/**
* Private function. Do not use!
*/
function _sonisweb_integration_make_soap_call($use_cached = TRUE, $callType, $sql_or_argumentdata, $component = '', $method = '', $hasReturnVariable = '', $url = '', $username = '', $password = '') {
$cache_len_in_seconds = variable_get('sonisweb_integration_caching_length', '0');
$availability = variable_get('sonisweb_integration_availability', 'on');
//If caching is turned on load cached values
if ($cache_len_in_seconds > 0) {
static $cache_values;
$index = $callType . md5($callType . serialize($sql_or_argumentdata) . $component . $method . $hasReturnVariable . $url . $username . $password);
//If using cached value for longer than page load and value is not already in loaded cache. Check in Drupal's cache.
if ($cache_len_in_seconds > 1 && $use_cached && !isset($cache_values[$index])) {
//Clear old values from Drupal's cache
cache_clear_all(NULL, "cache_sonisweb_integration");
//Get the item from cache if is there.
$tmp = cache_get($index, 'cache_sonisweb_integration');
if (isset($tmp->data) && $tmp->data != "")
$cache_values[$index] = $tmp->data;
}
}
//If not cached or caching disabled make the call.
if ($cache_len_in_seconds == 0 || !isset($cache_values[$index]) || !$use_cached) {
try {
if ($callType == 'api' || $callType == 'apiTest') {
if ($callType == 'apiTest') {
$soap = _sonisweb_integration_setup_soap('test', $url);
}
else {
$soap = _sonisweb_integration_setup_soap('api');
}
if ($soap) {
// If call type is a test, bypass the availability checks
if($callType == 'apiTest') {
$result = $soap->__soapCall("doSQLSomething", array(
'user' => ($username),
'pass' => ($password),
'sql' => $sql_or_argumentdata,
));
}
else {
// Only attempt an API call if Sonis is available (API calls are turned on)
if($availability == 'on') {
// Check the start and end time of the last Sonis API call
$start = variable_get('sonisweb_integration_call_start', $default = 0);
$end = variable_get('sonisweb_integration_call_end', $default = 0);
if($end - $start >= 30){
// The last call took more than 30 seconds, check if Sonis is down
_sonisweb_integration_check_availability();
}
$sessiondata = array();
if(array_key_exists('session_variables', $sql_or_argumentdata)) {
$sessiondata = $sql_or_argumentdata['session_variables'];
unset($sql_or_argumentdata['session_variables']);
}
variable_set('sonisweb_integration_call_start', time());
$result = $soap->__soapCall("doAPISomething", array(
'user' => ($callType == 'apiTest' ? $username : variable_get('sonisweb_integration_admin_user', '')),
'pass' => ($callType == 'apiTest' ? $password : variable_get('sonisweb_integration_admin_password', '')),
'comp' => $component,
'meth' => $method,
'hasReturnVariable' => $hasReturnVariable,
'argumentdata' => $sql_or_argumentdata,
'sessiondata' => $sessiondata,
));
// Sonis call ended, update the end time
variable_set('sonisweb_integration_call_end', time());
}
else if($availability == 'down') {
$lastCheck = variable_get('sonisweb_integration_last_availability_check', $default = 0);
// If it's been more than 5 minutes since the last time Sonis availability was checked, check again
if ($lastCheck - time() > 5) {
_sonisweb_integration_check_availability();
}
}
}
}
}
elseif ($callType == 'sql' || $callType == 'sqlTest') {
if ($callType == 'sqlTest') {
$soap = _sonisweb_integration_setup_soap('test', $url);
}
else {
$soap = _sonisweb_integration_setup_soap('sql');
}
if ($soap) {
// If call type is a test, bypass the availability checks
if($callType == 'sqlTest') {
$result = $soap->__soapCall("doSQLSomething", array(
'user' => ($username),
'pass' => ($password),
'sql' => $sql_or_argumentdata,
));
}
else {
$availability = variable_get('sonisweb_integration_availability', 'on');
// Only attempt an API call if Sonis is available (API calls are turned on)
if($availability == 'on') {
// Check the start and end time of the last Sonis API call
$start = variable_get('sonisweb_integration_call_start', $default = 0);
$end = variable_get('sonisweb_integration_call_end', $default = 0);
if($end - $start >= 30){
// The last call took more than 30 seconds, check if Sonis is down
_sonisweb_integration_check_availability();
}
variable_set('sonisweb_integration_call_start', time());
$result = $soap->__soapCall("doSQLSomething", array(
'user' => ($callType == 'sqlTest' ? $username : variable_get('sonisweb_integration_admin_user', '')),
'pass' => ($callType == 'sqlTest' ? $password : variable_get('sonisweb_integration_admin_password', '')),
'sql' => $sql_or_argumentdata,
));
// Sonis call ended, update the end time
variable_set('sonisweb_integration_call_end', time());
}
else if($availability == 'down') {
$lastCheck = variable_get('sonisweb_integration_last_availability_check', $default = 0);
// If it's been more than 5 minutes since the last time Sonis availability was checked, check again
if ($lastCheck - time() > 5) {
_sonisweb_integration_check_availability();
}
}
}
}
}
else {
return array('error' => t('Bad arguments passed'));
}
if ($callType == 'apiTest' || $callType == 'sqlTest') {
//Cache returned value
if ($cache_len_in_seconds > 0)
$cache_values[$index] = $result;
if ($cache_len_in_seconds > 1)
cache_set($index, $result, 'cache_sonisweb_integration', time() + $cache_len_in_seconds);
return ($result);
}
elseif ($cache_len_in_seconds > 0) {
//Cache returned value
$cache_values[$index] = _sonisweb_integration_to_array($result);
if ($cache_len_in_seconds > 1)
cache_set($index, $cache_values[$index], 'cache_sonisweb_integration', time() + $cache_len_in_seconds);
return ($cache_values[$index]);
}
else {
$tmp = _sonisweb_integration_to_array($result);
return ($tmp);
}
}
catch (Exception $e) {
return array('error' => t('A connection error has occurred'));
}
}
else {
return ($cache_values[$index]);
}
}
/**
* Private function. Do not use!
*/
function _sonisweb_integration_check_availability() {
global $base_url;
variable_set('sonisweb_integration_last_availability_check', time());
// Using CURL calls here so we don't block everything
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url . "/admin/config/sonisweb_integration/check_availability");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (curl_exec($ch) === FALSE) {
die("Curl Failed: " . curl_error($ch));
} else {
$result = curl_exec($ch);
}
if($result){
variable_set('sonisweb_integration_availability', 'on');
// Flush all caches to make sure users get data
cache_clear_all(NULL, "cache_sonisweb_integration");
return TRUE;
}
else {
// Availability check timed out, Sonis is down
variable_set('sonisweb_integration_availability', 'down');
return FALSE;
}
}
/**
* Private function. Do not use!
*/
function _sonisweb_integration_replace_data(&$value, $key) {
if ($value == "@username") {
$value = variable_get('sonisweb_integration_admin_user', '');
}
elseif ($value == "@password") {
$value = variable_get('sonisweb_integration_admin_password', '');
}
}
/**
* Private function. Do not use!
*/
function _sonisweb_integration_to_array($obj) {
$return = array();
if (is_array($obj)) {
$keys = array_keys($obj);
}
elseif (is_object($obj)) {
$keys = array_keys(get_object_vars($obj));
}
else {
return $obj;
}
foreach ($keys as $key) {
if (is_array($obj)) {
$return[$key] = _sonisweb_integration_to_array($obj[$key]);
}
else {
$return[$key] = _sonisweb_integration_to_array($obj->$key);
}
}
return $return;
}