-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathHandlers.php
More file actions
executable file
·1809 lines (1555 loc) · 61.5 KB
/
Copy pathHandlers.php
File metadata and controls
executable file
·1809 lines (1555 loc) · 61.5 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
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Handlers for each of the QBWC SOAP server required methods
*
* Copyright (c) 2010 Keith Palmer / ConsoliBYTE, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.opensource.org/licenses/eclipse-1.0.php
*
* The QuickBooks Web Connector requires that your SOAP server be able to
* handle six basic methods. Each of the six methods are implemented in this
* class and called by the QuickBooks_Server class instance.
*
* These methods in turn will call the action handlers you register with the
* SOAP server, and also log quite a bit of debugging information to the
* database so that you can see what's happening during the QBWC exchange with
* your SOAP server.
*
* @author Keith Palmer <keith@consolibyte.com>
* @license LICENSE.txt
*
* @package QuickBooks
* @subpackage Server
*/
/**
* Various QuickBooks related utilities methods
*/
QuickBooks_Loader::load('/QuickBooks/Utilities.php');
/**
* Functions for calling callbacks (functions, static methods, object methods, etc.)
*/
QuickBooks_Loader::load('/QuickBooks/Callbacks.php');
/**
* Response container for calls to ->authenticate()
*/
QuickBooks_Loader::load('/QuickBooks/WebConnector/Result/Authenticate.php');
/**
* Response container for calls to ->closeConnection()
*/
QuickBooks_Loader::load('/QuickBooks/WebConnector/Result/CloseConnection.php');
/**
* Response container for calls to ->connectionError()
*/
QuickBooks_Loader::load('/QuickBooks/WebConnector/Result/ConnectionError.php');
/**
* Response container for calls to ->getLastError()
*/
QuickBooks_Loader::load('/QuickBooks/WebConnector/Result/GetLastError.php');
/**
* Response container for calls to ->receiveResponseXML()
*/
QuickBooks_Loader::load('/QuickBooks/WebConnector/Result/ReceiveResponseXML.php');
/**
* Response container for calls to ->sendRequestXML()
*/
QuickBooks_Loader::load('/QuickBooks/WebConnector/Result/SendRequestXML.php');
/**
* Response container for calls to ->getServerVersion()
*/
QuickBooks_Loader::load('/QuickBooks/WebConnector/Result/ServerVersion.php');
/**
* Response container for calls to ->clientVersion()
*/
QuickBooks_Loader::load('/QuickBooks/WebConnector/Result/ClientVersion.php');
/**
* Hook which gets called when the ->authenticate() method gets called
* @param string
*/
define('QUICKBOOKS_HANDLERS_HOOK_AUTHENTICATE', 'QuickBooks_Handlers::authenticate');
/**
* Hook which gets called when the ->clientVersion() method gets called
* @var string
*/
define('QUICKBOOKS_HANDLERS_HOOK_CLIENTVERSION', 'QuickBooks_Handlers::clientVersion');
/**
* Hook which gets called when the ->closeConnection() method gets called
* @var string
*/
define('QUICKBOOKS_HANDLERS_HOOK_CLOSECONNECTION', 'QuickBooks_Handlers::closeConnection');
/**
* Hook which gets called when the ->connectionError() method gets called
* @var string
*/
define('QUICKBOOKS_HANDLERS_HOOK_CONNECTIONERROR', 'QuickBooks_Handlers::connectionError');
/**
*
*/
define('QUICKBOOKS_HANDLERS_HOOK_GETINTERACTIVEURL', 'QuickBooks_Handlers::getInteractiveURL');
/**
*
*/
define('QUICKBOOKS_HANDLERS_HOOK_GETLASTERROR', 'QuickBooks_Handlers::getLastError');
/**
*
*
*/
define('QUICKBOOKS_HANDLERS_HOOK_INTERACTIVEDONE', 'QuickBooks_Handlers::interactiveDone');
/**
*
*
*/
define('QUICKBOOKS_HANDLERS_HOOK_INTERACTIVEREJECTED', 'QuickBooks_Handlers::interactiveRejected');
/**
*
*
*/
define('QUICKBOOKS_HANDLERS_HOOK_RECEIVERESPONSEXML', 'QuickBooks_HandlersS::receiveResponseXML');
/**
*
*/
define('QUICKBOOKS_HANDLERS_HOOK_SENDREQUESTXML', 'QuickBooks_Handlers::sendRequestXML');
/**
*
*/
define('QUICKBOOKS_HANDLERS_HOOK_SERVERVERSION', 'QuickBooks_Handlers::serverVersion');
/**
*
*/
define('QUICKBOOKS_HANDLERS_HOOK_LOGINSUCCESS', 'QuickBooks_Handlers::login-success');
/**
* Hook which is called when a login fails
* @var string
*/
define('QUICKBOOKS_HANDLERS_HOOK_LOGINFAILURE', 'QuickBooks_Handlers::login-fail');
/**
* Alias of {@link QUICKBOOKS_HANDLERS_HOOK_LOGINFAILURE}
* @var string
*/
define('QUICKBOOKS_HANDLERS_HOOK_LOGINFAIL', QUICKBOOKS_HANDLERS_HOOK_LOGINFAILURE);
/**
* Hook which is called when recurring events are registered
* @var string
*/
define('QUICKBOOKS_HANDLERS_HOOK_RECURRING', 'QuickBooks_Handlers::recurring');
/**
* Hook which is called to report a percentage don
* @var string
*/
define('QUICKBOOKS_HANDLERS_HOOK_PERCENT', 'QuickBooks_Handlers::percent');
/**
* Handlers for each of the QBWC SOAP server required methods
*/
class QuickBooks_WebConnector_Handlers
{
const HOOK_AUTHENTICATE = QUICKBOOKS_HANDLERS_HOOK_AUTHENTICATE;
const HOOK_LOGINSUCCESS = QUICKBOOKS_HANDLERS_HOOK_LOGINSUCCESS;
/**
* Driver object instance for backend of SOAP server
* @var QuickBooks_Driver
*/
protected $_driver;
/**
* Raw XML input
* @var string
*/
protected $_input;
/**
* Map of queued actions to function handlers
* @var array
*/
protected $_map;
/**
*
*
*/
protected $_instance_map;
/**
* Map of error codes to function handler
* @var array
*/
protected $_onerror;
/**
*
*
*/
protected $_instance_onerror;
/**
* Map of hook names to function handlers
* @var array
*/
protected $_hooks;
/**
*
*
*/
protected $_instance_hooks;
/**
* Configuration parameters
* @var array
*/
protected $_config;
/**
* Callback configuration parameters
* @var array
*/
protected $_callback_config;
/**
* Create the server handler instance
*
* Optional configuration items should be passed as an associative array with any of these keys:
* - qb_company_file The full filesystem path to a specific QuickBooks company file (by default, it will use the currently open company file)
* - qbwc_min_version Minimum version of the Web Connector that must be used to connect (by default, any version may connect)
* - qbwc_wait_before_next_update Tell the Web Connector to wait this number of seconds before doign another update
* - qbwc_min_run_every_n_seconds Tell the Web Connector to run every n seconds (overrides whatever was in the .QWC web connector configuration file)
* - qbwc_interactive_url The URL to use for Interactive QuickBooks Web Connector sessions
* - server_version Server version string
* - authenticate_handler If you want to use some custom authentication method, put the function name of your custom authentication function here
* - autoadd_missing_requestid This defaults to TRUE, if TRUE and you forget to embed a requestID="..." attribute, it will try to automatically add that attribute for you
*
* @param mixed $dsn_or_conn DSN connection string for QuickBooks queue
* @param array $map A map of QuickBooks API calls to callback functions/methods
* @param array $onerror A map of QuickBooks error codes to callback functions/methods
* @param array $hooks A map of hook names to callback functions/methods
* @param string $input Raw XML input from QuickBooks API call
* @param array $handler_config An array of configuration options
* @param array $driver_config An array of driver configuration options
*/
public function __construct($dsn_or_conn, $map, $onerror, $hooks, $log_level, $input, $handler_config = array(), $driver_config = array(), $callback_config = array())
{
$this->_driver = QuickBooks_Utilities::driverFactory($dsn_or_conn, $driver_config, $hooks, $log_level);
$this->_input = $input;
$this->_map = $map;
$this->_onerror = $onerror;
$this->_hooks = array();
foreach ($hooks as $hook => $funcs)
{
if (!is_array($funcs))
{
$funcs = array( $funcs );
}
$this->_hooks[$hook] = $funcs;
}
$this->_config = $this->_defaults($handler_config);
$this->_callback_config = $callback_config;
//$this->_driver->log('Handler is starting up...: ' . print_r($this->_config, true), '', QUICKBOOKS_LOG_DEBUG);
$this->_log('Handler is starting up...: ' . print_r($this->_config, true), '', QUICKBOOKS_LOG_DEBUG);
}
/**
* Massage any optional configuration flags
*
* @param array $config
* @return array
*/
protected function _defaults($config)
{
$url = '?';
if (isset($_SERVER['REQUEST_URI']))
{
$url = $_SERVER['REQUEST_URI'];
}
$defaults = array(
'qb_company_file' => null, // To force a specific company file to be used
'qbwc_min_version' => null, // Minimum version of the QBWC that must be used to connect
'qbwc_wait_before_next_update' => null, // Tell the QBWC to wait this number of seconds before doing another update
'qbwc_min_run_every_n_seconds' => null, // Tell the QBWC to run every n seconds (overrides whatever was in the .QWC web connector configuration file)
'qbwc_version_warning_message' => null, // Not implemented...
'qbwc_version_error_message' => null, // Not implemented...
'qbwc_interactive_url' => null, // Provide the URL for an interactive session to the QuickBooks Web Connector
'autoadd_missing_requestid' => true,
'check_valid_requestid' => true,
'server_version' => 'PHP QuickBooks SOAP Server v' . QUICKBOOKS_PACKAGE_VERSION . ' at ' . $url, // Server version string
'authenticate' => null, // If you want to use some custom authentication scheme (and not the quickbooks_user MySQL table) you can specify your own function here
'authenticate_dsn' => null, // (backward compat. for 'authenticate')
'map_application_identifiers' => true, // Try to map web application IDs to QuickBooks ListIDs/TxnIDs
'allow_remote_addr' => array(),
'deny_remote_addr' => array(),
'convert_unix_newlines' => true,
'deny_concurrent_logins' => true,
'deny_concurrent_timeout' => 60,
'deny_reallyfast_logins' => true,
'deny_reallyfast_timeout' => 600,
'masking' => true,
);
$config = array_merge($defaults, $config);
// Make sure this is an *array* of addresses to allow
if (!is_array($config['allow_remote_addr']))
{
$config['allow_remote_addr'] = array( $config['allow_remote_addr'] );
}
// Make sure this is an *array* of addresses to deny
if (!is_array($config['deny_remote_addr']))
{
$config['deny_remote_addr'] = array( $config['deny_remote_addr'] );
}
$config['autoadd_missing_requestid'] = (boolean) $config['autoadd_missing_requestid'];
$config['check_valid_requestid'] = (boolean) $config['check_valid_requestid'];
$config['map_application_identifiers'] = (boolean) $config['map_application_identifiers'];
$config['convert_unix_newlines'] = (boolean) $config['convert_unix_newlines'];
$config['deny_concurrent_logins'] = (boolean) $config['deny_concurrent_logins'];
$config['deny_concurrent_timeout'] = (int) max(1, $config['deny_concurrent_timeout']);
$config['deny_reallyfast_logins'] = (boolean) $config['deny_reallyfast_logins'];
$config['deny_reallyfast_timeout'] = (int) max(1, $config['deny_reallyfast_timeout']);
return $config;
}
/**
* Check if a given remote address (IP address) is allowed based on allow and deny arrays
*
* @param string $remoteaddr
* @param array $allow
* @param array $deny
* @return boolean
*/
protected function _checkRemote($remoteaddr, $arr_allow, $arr_deny)
{
return QuickBooks_Utilities::checkRemoteAddress($remoteaddr, $arr_allow, $arr_deny);
}
/**
* Log a message to the error/debug log
*
* @param string $msg
* @param string $ticket
* @param integer $level
* @return boolean
*/
protected function _log($msg, $ticket, $level = QUICKBOOKS_LOG_NORMAL)
{
$Driver = $this->_driver;
if ($this->_config['masking'])
{
$msg = QuickBooks_Utilities::mask($msg);
}
if ($Driver)
{
return $Driver->log($msg, $ticket, $level);
}
return false;
}
/**
* Queue up recurring events that are overdue to be run
*
* @param string $ticket
* @return boolean
*/
protected function _handleRecurringEvents($ticket)
{
if ($user = $this->_driver->authResolve($ticket))
{
while ($next = $this->_driver->recurDequeue($user, true))
{
//$this->_driver->log('Dequeued a recurring event, enqueuing!', $ticket, QUICKBOOKS_LOG_VERBOSE);
$this->_log('Dequeued a recurring event, enqueuing!', $ticket, QUICKBOOKS_LOG_VERBOSE);
$extra = null;
if ($next['extra'])
{
$extra = unserialize($next['extra']);
}
//print_r($next);
$hookerr = '';
$this->_callHook($ticket,
QUICKBOOKS_HANDLERS_HOOK_RECURRING,
null, //$this->_constructRequestID($next['qb_action'], $next['ident']),
$next['qb_action'],
$next['ident'],
$extra,
$hookerr);
// $ticket, $hook, $requestID, $action, $ident, $extra, &$err, $xml = '', $qb_identifiers = array()
//print_r($next);
//exit;
// (boolean) $next['replace']
// $user, $action, $ident, $replace = true, $priority = 0, $extra = null, $qbxml = null
$this->_driver->queueEnqueue($user, $next['qb_action'], $next['ident'], true, (int) $next['priority'], $extra, $next['qbxml']);
}
return true;
}
return false;
}
/**
* Authenticate method for the QuickBooks Web Connector SOAP service
*
* The authenticate method is called when the Web Connector establishes a
* connection with the SOAP server in order to ensure that there is work to
* do and that the Web Connector is allowed to connect/that it actually is
* the Web Connector that is connecting and sending us messages.
*
* The stdClass object that is received as a parameter will have two
* members:
* - strUserName The username provided in the QWC file to the Web Connector
* - strPassword The password the end-user enters into the QuickBooks Web Connector application
*
* The return object should be an array with two elements. The first
* element is a generated login ticket (or an empty string if the login
* failed) and the second string is either "none" (for successful log-ins
* with nothing to do in the queue) or "nvu" if the login failed.
*
* The following user-defined hooks are invoked:
* - QUICKBOOKS_HANDLERS_HOOK_AUTHENTICATE
* - QUICKBOOKS_HANDLERS_HOOK_LOGINSUCCESS
* - QUICKBOOKS_HANDLERS_HOOK_LOGINFAILURE
*
* @param stdClass $obj The SOAP object that gets sent by the Web Connector
* @return QuickBooks_Result_Authenticate A container object to send back to the Web Connector
*/
public function authenticate($obj)
{
//$this->_driver->log('authenticate()', '', QUICKBOOKS_LOG_VERBOSE);
$this->_log('authenticate()', '', QUICKBOOKS_LOG_VERBOSE);
$ticket = '';
$status = '';
// Authenticate login hook
$hookdata = array(
'username' => $obj->strUserName,
'password' => $obj->strPassword,
);
$hookerr = '';
$this->_callHook($ticket, QUICKBOOKS_HANDLERS_HOOK_AUTHENTICATE, null, null, null, null, $hookerr, null, array(), $hookdata);
// Remote address allow/deny
if (false == $this->_checkRemote($_SERVER['REMOTE_ADDR'], $this->_config['allow_remote_addr'], $this->_config['deny_remote_addr']))
{
//$this->_driver->log('Connection from remote address rejected: ' . $_SERVER['REMOTE_ADDR'], null, QUICKBOOKS_LOG_VERBOSE);
$this->_log('Connection from remote address rejected: ' . $_SERVER['REMOTE_ADDR'], null, QUICKBOOKS_LOG_VERBOSE);
return new QuickBooks_WebConnector_Result_Authenticate('', 'nvu', null, null);
}
// If we do either concurrent login checks, or rate-limiting, we need to grab the date/time
// of the last connection.
$authLast = null;
if ($this->_config['deny_concurrent_logins'] or $this->_config['deny_reallyfast_logins'])
{
$authlast = $this->_driver->authLast($obj->strUserName);
}
// Check for concurrent logins
if ($this->_config['deny_concurrent_logins'])
{
if ($authlast and
time() - strtotime($authlast[1]) < $this->_config['deny_concurrent_timeout'])
{
$this->_log('Denied concurrent login from: ' . $obj->strUserName, null, QUICKBOOKS_LOG_VERBOSE);
return new QuickBooks_WebConnector_Result_Authenticate('CONC1234', 'none', null, null);
}
}
// Rate-limiting
if ($this->_config['deny_reallyfast_logins'])
{
if ($authlast and
time() - strtotime($authlast[1]) < $this->_config['deny_reallyfast_timeout'])
{
$this->_log('Denied really fast login from: ' . $obj->strUserName . ' (last login: ' . $authlast[1] . ')', null, QUICKBOOKS_LOG_VERBOSE);
return new QuickBooks_WebConnector_Result_Authenticate('FAST1234', 'none', null, null);
}
}
// Custom authentication backends
$override_dsn = $this->_config['authenticate'];
if (!empty($this->_config['authenticate_dsn']))
{
// Backwards compat.
$override_dsn = $this->_config['authenticate_dsn'];
}
$auth = null;
/*
if (strlen($override_dsn))
{
$override_dsn = str_replace('function://', '', $override_dsn);
}
*/
$company_file = null;
$wait_before_next_update = null;
$min_run_every_n_seconds = null;
$customauth_company_file = null;
$customauth_wait_before_next_update = null;
$customauth_min_run_every_n_seconds = null;
if (is_array($override_dsn) or strlen(isset($override_dsn) ? $override_dsn : '')) // Custom autj
{
//if ($auth->authenticate($obj->strUserName, $obj->strPassword, $customauth_company_file, $customauth_wait_before_next_update, $customauth_min_run_every_n_seconds) and
//if ($override_dsn($obj->strUserName, $obj->strPassword, $customauth_company_file, $customauth_wait_before_next_update, $customauth_min_run_every_n_seconds) and
if (QuickBooks_Callbacks::callAuthenticate($this->_driver, $override_dsn, $obj->strUserName, $obj->strPassword, $customauth_company_file, $customauth_wait_before_next_update, $customauth_min_run_every_n_seconds) and
$ticket = $this->_driver->authLogin($obj->strUserName, $obj->strPassword, $company_file, $wait_before_next_update, $min_run_every_n_seconds, true))
{
//$this->_driver->log('Login (' . $parse['scheme'] . '): ' . $obj->strUserName, $ticket, QUICKBOOKS_LOG_DEBUG);
$this->_log('Login via ' . print_r($override_dsn, true) . ': ' . $obj->strUserName, $ticket, QUICKBOOKS_LOG_DEBUG);
if ($customauth_company_file)
{
$status = $customauth_company_file;
}
else if ($company_file)
{
$status = $company_file;
}
else if ($this->_config['qb_company_file'])
{
$status = $this->_config['qb_company_file'];
}
if ((int) $customauth_wait_before_next_update)
{
$wait_before_next_update = (int) $customauth_wait_before_next_update;
}
else if ((int) $wait_before_next_update)
{
;
}
else if ((int) $this->_config['qbwc_wait_before_next_update'])
{
$wait_before_next_update = (int) $this->_config['qbwc_wait_before_next_update'];
}
if ((int) $customauth_min_run_every_n_seconds)
{
$min_run_every_n_seconds = (int) $customauth_min_run_every_n_seconds;
}
else if ((int) $min_run_every_n_seconds)
{
;
}
else if ((int) $this->_config['qbwc_min_run_every_n_seconds'])
{
$min_run_every_n_seconds = (int) $this->_config['qbwc_min_run_every_n_seconds'];
}
// Call login hook
$hookdata = array(
'authenticate_dsn' => $override_dsn,
'username' => $obj->strUserName,
'password' => $obj->strPassword,
'ticket' => $ticket,
'qb_company_file' => $status,
'qbwc_wait_before_next_update' => $wait_before_next_update,
'qbwc_min_run_every_n_seconds' => $min_run_every_n_seconds,
);
$hookerr = '';
$this->_callHook($ticket, QuickBooks_WebConnector_Handlers::HOOK_LOGINSUCCESS, null, null, null, null, $hookerr, null, array(), $hookdata);
// Move any recurring events that are due to the queue table
$this->_handleRecurringEvents($ticket);
if (!$this->_driver->queueDequeue($obj->strUserName))
{
$status = 'none';
}
// Login success (with a custom login handler)!
}
else
{
//$this->_driver->log('Login failed (' . $parse['scheme'] . '): ' . $obj->strUserName, '', QUICKBOOKS_LOG_DEBUG);
$this->_log('Login failed: ' . $obj->strUserName, '', QUICKBOOKS_LOG_DEBUG);
$hookdata = array(
'authenticate_dsn' => $override_dsn,
'username' => $obj->strUserName,
'password' => $obj->strPassword,
);
$hookerr = '';
$this->_callHook(null, QUICKBOOKS_HANDLERS_HOOK_LOGINFAILURE, null, null, null, null, $hookerr, null, array(), $hookdata);
$ticket = '';
$status = 'nvu'; // Invalid username/password
}
return new QuickBooks_WebConnector_Result_Authenticate($ticket, $status, $wait_before_next_update, $min_run_every_n_seconds);
}
else // Standard authentication
{
if ($ticket = $this->_driver->authLogin($obj->strUserName, $obj->strPassword, $company_file, $wait_before_next_update, $min_run_every_n_seconds))
{
//$this->_driver->log('Login: ' . $obj->strUserName, $ticket, QUICKBOOKS_LOG_DEBUG);
$this->_log('Login: ' . $obj->strUserName, $ticket, QUICKBOOKS_LOG_DEBUG);
if (!strlen($company_file) and $this->_config['qb_company_file'])
{
$status = $this->_config['qb_company_file'];
}
else if (strlen($company_file))
{
$status = $company_file;
}
if (! (int) $wait_before_next_update and (int) $this->_config['qbwc_wait_before_next_update'])
{
$wait_before_next_update = (int) $this->_config['qbwc_wait_before_next_update'];
}
if (! (int) $min_run_every_n_seconds and (int) $this->_config['qbwc_min_run_every_n_seconds'])
{
$min_run_every_n_seconds = (int) $this->_config['qbwc_min_run_every_n_seconds'];
}
$hookdata = array(
'username' => $obj->strUserName,
'password' => $obj->strPassword,
'ticket' => $ticket,
'qb_company_file' => $status,
'qbwc_wait_before_next_update' => $wait_before_next_update,
'qbwc_min_run_every_n_seconds' => $min_run_every_n_seconds,
);
$hookerr = '';
$this->_callHook($ticket, QUICKBOOKS_HANDLERS_HOOK_LOGINSUCCESS, null, null, null, null, $hookerr, null, array(), $hookdata);
$this->_handleRecurringEvents($ticket);
if (!$this->_driver->queueDequeue($obj->strUserName))
{
$status = 'none'; // Good login, but there isn't anything in the queue
}
// Login success!
}
else
{
//$this->_driver->log('Login failed: ' . $obj->strUserName, '', QUICKBOOKS_LOG_DEBUG);
$this->_log('Login failed: ' . $obj->strUserName, '', QUICKBOOKS_LOG_DEBUG);
$hookdata = array(
'username' => $obj->strUserName,
'password' => $obj->strPassword,
);
$hookerr = '';
$this->_callHook(null, QUICKBOOKS_HANDLERS_HOOK_LOGINFAILURE, null, null, null, null, $hookerr, null, array(), $hookdata);
$ticket = '';
$status = 'nvu'; // Invalid username/password
}
return new QuickBooks_WebConnector_Result_Authenticate($ticket, $status, $wait_before_next_update, $min_run_every_n_seconds);
}
}
/**
* SendRequestXML method for the QuickBooks Web Connector SOAP server - Generate and send a request to QuickBooks
*
* The QuickBooks Web Connector calls this method to ask for things to do.
* So, calling this method is the Web Connectors way of saying: "Please
* send me a command so that I can pass that command on to QuickBooks."
* After it passes the command to QuickBooks, it will pass the response
* back via a call to receiveResponseXML().
*
* The stdClass object passed as a parameter should contain these members:
* - ticket The login session ticket
* - strHCPResponse
* - strCompanyFileName
* - qbXMLCountry The country code for whatever version of QuickBooks is sitting behind the Web Connector
* - qbXMLMajorVers The major version code of the QuickBooks web connector
* - qbXMLMinorVers The minor version code of the QuickBooks web connector
*
* You should return either an empty string "" to signal an error state, or
* a valid qbXML or qbposXML request.
*
* The following user-defined hooks are invoked by this method:
* - QUICKBOOKS_HANDLERS_HOOK_SENDREQUESTXML
*
* @param stdClass $obj
* @return QuickBooks_Result_SendRequestXML
*/
public function sendRequestXML($obj)
{
//$this->_driver->log('sendRequestXML()', $obj->ticket, QUICKBOOKS_LOG_VERBOSE);
$this->_log('sendRequestXML()', $obj->ticket, QUICKBOOKS_LOG_VERBOSE);
if ($this->_driver->authCheck($obj->ticket))
{
$user = $this->_driver->authResolve($obj->ticket);
$hookdata = array(
'username' => $user,
'ticket' => $obj->ticket,
'strHCPResponse' => $obj->strHCPResponse,
'strCompanyFileName' => $obj->strCompanyFileName,
'qbXMLCountry' => $obj->qbXMLCountry,
'qbXMLMajorVers' => $obj->qbXMLMajorVers,
'qbXMLMinorVers' => $obj->qbXMLMinorVers,
);
$hookerr = '';
$this->_callHook($obj->ticket, QUICKBOOKS_HANDLERS_HOOK_SENDREQUESTXML, null, null, null, null, $hookerr, null, array(), $hookdata);
// _callHook($ticket, $hook, $requestID, $action, $ident, $extra, &$err, $xml = '', $qb_identifiers = array(), $hook_data = array())
// Move recurring events which are due to run to the queue table
// We *CAN'T* re-register recurring events here, otherwise, we run
// the risk of re-adding an event which has occured, *before* the
// entire session has finishing running. Thus, we'd create an
// infinite loop of web connector that would never end.
//$this->_handleRecurringEvents($obj->ticket);
if ($next = $this->_driver->queueDequeue($user, true)) // Fetch the next action/command from the queue
{
//$this->_driver->log('Dequeued: ( ' . $next['qb_action'] . ', ' . $next['ident'] . ' ) ', $obj->ticket, QUICKBOOKS_LOG_DEBUG);
$this->_log('Dequeued: ( ' . $next['qb_action'] . ', ' . $next['ident'] . ' ) ', $obj->ticket, QUICKBOOKS_LOG_DEBUG);
//$this->_driver->queueStatus($obj->ticket, $next['qb_action'], $next['ident'], QUICKBOOKS_STATUS_PROCESSING);
$this->_driver->queueStatus($obj->ticket, $next['quickbooks_queue_id'], QUICKBOOKS_STATUS_PROCESSING);
/*
// Here's a strange case, interactive mode handler
if ($next['qb_action'] == QUICKBOOKS_INTERACTIVE_MODE)
{
// Set the error to "Interactive mode"
$this->_driver->errorLog($obj->ticket, QUICKBOOKS_ERROR_OK, QUICKBOOKS_INTERACTIVE_MODE);
// This will cause ->getLastError() to be called, and ->getLastError() will then return the string "Interactive mode" which will cause QuickBooks to call ->getInteractiveURL() and start an interactive session... I think...?
return new QuickBooks_Result_SendRequestXML('');
}
*/
$extra = '';
if ($next['extra'])
{
$extra = unserialize($next['extra']);
}
$err = '';
$xml = '';
//$last_action_time = $this->_driver->queueActionLast($user, $next['qb_action']);
//$last_actionident_time = $this->_driver->queueActionIdentLast($user, $next['qb_action'], $next['ident']);
$last_action_time = null;
$last_actionident_time = null;
// Call the mapped function that should generate an appropriate qbXML request
$xml = $this->_callMappedFunction(0, $user, $next['quickbooks_queue_id'], $next['qb_action'], $next['ident'], $extra, $err, $last_action_time, $last_actionident_time, $obj->qbXMLMajorVers . '.' . $obj->qbXMLMinorVers, $obj->qbXMLCountry, $next['qbxml']);
// Make sure there's no whitespace around it
$xml = trim($xml);
// NoOp can be returned to skip this current operation. This will cause getLastError
// to be called, at which point NoOp should be returned to tell the Web
// Connector to then pause for 5 seconds before asking for another request.
if ($xml == QUICKBOOKS_NOOP)
{
$this->_driver->errorLog($obj->ticket, 0, QUICKBOOKS_NOOP);
// Mark it as a NoOp to remove it from the queue
//$this->_driver->queueStatus($obj->ticket, $next['qb_action'], $next['ident'], QUICKBOOKS_STATUS_NOOP, 'Handler function returned: ' . QUICKBOOKS_NOOP);
$this->_driver->queueStatus($obj->ticket, $next['quickbooks_queue_id'], QUICKBOOKS_STATUS_NOOP, 'Handler function returned: ' . QUICKBOOKS_NOOP);
return new QuickBooks_WebConnector_Result_SendRequestXML('');
}
// If the requestID="..." attribute was not specified, we can try to automatically add it to the request
$requestID = null;
if (!($requestID = $this->_extractRequestID($xml)) and
$this->_config['autoadd_missing_requestid'])
{
// Find the <DoSomethingRq tag
foreach (QuickBooks_Utilities::listActions() as $action)
{
$request = QuickBooks_Utilities::actionToRequest($action);
if (false !== strpos($xml, '<' . $request . ' '))
{
//$xml = str_replace('<' . $request . ' ', '<' . $request . ' requestID="' . $this->_constructRequestID($next['qb_action'], $next['ident']) . '" ', $xml);
$xml = str_replace('<' . $request . ' ', '<' . $request . ' requestID="' . $next['quickbooks_queue_id'] . '" ', $xml);
break;
}
else if (false !== strpos($xml, '<' . $request . '>'))
{
//$xml = str_replace('<' . $request . '>', '<' . $request . ' requestID="' . $this->_constructRequestID($next['qb_action'], $next['ident']) . '">', $xml);
$xml = str_replace('<' . $request . '>', '<' . $request . ' requestID="' . $next['quickbooks_queue_id'] . '">', $xml);
break;
}
}
}
else if ($this->_config['check_valid_requestid'])
{
// They embedded a requestID="..." attribute, let's make sure it's valid
//$embedded_action = null;
//$embedded_ident = null;
//$this->_parseRequestID($requestID, $embedded_action, $embedded_ident);
//if ($embedded_action != $next['qb_action'] or $embedded_ident != $next['ident'])
if ($next['quickbooks_queue_id'] != $requestID)
{
// They are sending this request with an INVALID requestID! Error this out and warn them!
$err = 'This request contains an invalid embedded requestID="..." attribute; either embed the $requestID parameter, or leave out the requestID="..." attribute entirely, found [' . $requestID . ' vs. expected ' . $next['quickbooks_queue_id'] . ']!';
}
}
/*
if ($this->_config['convert_unix_newlines'] and
false === strpos($xml, "\r") and // there are currently no Windows newlines...
false !== strpos($xml, "\n")) // ... but there *are* Unix newlines!
{
; // (this is currently broken/unimplemented)
}
*/
if ($err) // The function encountered an error when generating the qbXML request
{
//$this->_driver->errorLog($obj->ticket, QUICKBOOKS_ERROR_HANDLER, $err);
//$this->_driver->log('ERROR: ' . $err, $obj->ticket, QUICKBOOKS_LOG_NORMAL);
//$this->_driver->queueStatus($obj->ticket, $next['qb_action'], $next['ident'], QUICKBOOKS_STATUS_ERROR, 'Registered handler returned error: ' . $err);
$errerr = '';
//$this->_handleError($obj->ticket, QUICKBOOKS_ERROR_HANDLER, $err, $this->_constructRequestID($next['qb_action'], $next['ident']), $next['qb_action'], $next['ident'], $extra, $errerr, $xml);
$this->_handleError($obj->ticket, QUICKBOOKS_ERROR_HANDLER, $err, $next['quickbooks_queue_id'], $next['qb_action'], $next['ident'], $extra, $errerr, $xml);
return new QuickBooks_WebConnector_Result_SendRequestXML('');
}
else
{
//$this->_driver->log('Outgoing XML request: ' . $xml, $obj->ticket, QUICKBOOKS_LOG_DEBUG);
$this->_log('Outgoing XML request: ' . $xml, $obj->ticket, QUICKBOOKS_LOG_DEBUG);
if (strlen($xml) and // Returned XML AND
!$this->_extractRequestID($xml)) // Does not have a requestID in the request
{
// Mark it as successful right now
//$this->_driver->queueStatus($obj->ticket, $next['qb_action'], $next['ident'], QUICKBOOKS_STATUS_SUCCESS, 'Unverified... no requestID attribute in XML stream.');
$this->_driver->queueStatus($obj->ticket, $next['quickbooks_queue_id'], QUICKBOOKS_STATUS_SUCCESS, 'Unverified... no requestID attribute in XML stream.');
}
return new QuickBooks_WebConnector_Result_SendRequestXML($xml);
}
}
}
// Reporting an error, this will cause the QBWC to call ->getLastError()
return new QuickBooks_WebConnector_Result_SendRequestXML('');
}
/**
* Extract the requestID attribute from an XML stream
*
* @param string $xml The XML stream to look for a requestID attribute in
* @return mixed The request ID
*/
protected function _extractRequestID($xml)
{
return QuickBooks_Utilities::extractRequestID($xml);
}
/**
* Create a requestID string from action and ident parts
*
* @param string $action
* @param mixed $ident
* @return string
*/
/*
protected function _constructRequestID($action, $ident)
{
return QuickBooks_Utilities::constructRequestID($action, $ident);
}
*/
/**
* Parse a requestID string into it's action and ident parts
*
* @param string $requestID
* @param string $action
* @param mixed $ident
* @return void
*/
/*
protected function _parseRequestID($requestID, &$action, &$ident)
{
return QuickBooks_Utilities::parseRequestID($requestID, $action, $ident);
}
*/
/**
* Extract a unique record identifier from an XML response
*
* Some (most?) records within QuickBooks have unique identifiers which are
* returned with the qbXML responses. This method will try to extract all
* identifiers it can find from a qbXML response and return them in an
* associative array.
*
* For example, Customers have unique ListIDs, Invoices have unique TxnIDs,
* etc. For an AddCustomer request, you'll get an array that looks like
* this:
* <code>
* array(
* 'ListID' => '2C0000-1039887390'
* )
* </code>
*
* Other transactions might have more than one identifier. For instance, a
* call to AddInvoice returns both a ListID and a TxnID:
* <code>
* array(
* 'ListID' => '200000-1036881887', // This is actually part of the 'CustomerRef' entity in the Invoice XML response
* 'TxnID' => '11C26-1196256987', // This is the actual transaction ID for the Invoice XML response
* )
* </code>
*
* *** IMPORTANT *** If there are duplicate fields (i.e.: 3 different
* ListIDs returned) then only the first value encountered will appear in
* the associative array.
*
* The following elements/attributes are supported:
* - ListID
* - TxnID
* - iteratorID
* - OwnerID
* - TxnLineID
*
* @param string $xml The XML stream to look for an identifier in
* @return array An associative array mapping identifier fields to identifier values
*/
protected function _extractIdentifiers($xml)
{
$fetch_tagdata = array(
'ListID',
'TxnID',
'OwnerID',
'TxnLineID',
'EditSequence',
'FullName',
'Name',
'RefNumber',
);
$fetch_attributes = array(
'requestID',
'iteratorID',
'iteratorRemainingCount',
'metaData',
'retCount',
'statusCode',
'statusSeverity',
'statusMessage',
'newMessageSetID',