-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWEBAPI.pm
More file actions
executable file
·4306 lines (3574 loc) · 120 KB
/
Copy pathWEBAPI.pm
File metadata and controls
executable file
·4306 lines (3574 loc) · 120 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
package WEBAPI;
use Data::Dumper;
##
## PACKAGE: WEBAPI
##
## purpose:
## collectively, a wrapper of functions for sync.cgi and legacy.cgi (which are actually the same application)
## this will be a collection of all new format apis, in one, single, comprehensive location.
## note: I expect this module to grow *very* large, thats okay because eventually webapi will have it's own
## cluster of servers - and the whole thing will be mod_perl, and it'll be great, and i'll be popular,
## and people will like me, because i'm handsome, and smart, and .. err.
##
##
## these two values contain error codes 1024, 1025 (min/max compat level exceeded)
$WEBAPI::MAX_ALLOWED_COMPAT_LEVEL = 222;
$WEBAPI::MIN_ALLOWED_COMPAT_LEVEL = 201;
=pod
[[SECTION]Releases Notes]
<li> 2011/05/04: Added CUSTOMERPROCESS API call.
[[/SUBSECTION]]
[[/SECTION]]
[[SECTION]Compatibility Levels]
Current minimum compatibility level: 200 (released 11/15/10)
[[BREAK]]
[[STAFF]]
** when bumping compatibility level we should also change $WEBAPI::MAX_ALLOWED_COMPAT_LEVEL
[[/STAFF]]
<li> 221: 8/19/13 modified <PROFILES> response in webdb sync
<li> 220: 11/26/12 new order format (v220)
<li> 210: 9/17/12 addition of uuid= in stuff
<li> 205: 1/5/12 ADDPRIVATE macro does an overrite, not an append. (backward compatibility release)
<li> 204: 12/29/11 fixes issues with payment processing (not explicitly declared in code, because they're "SAFE"/forward compat)
<li> 203: 10/24/11 has strict encoding rules for options, modifier must be encoded or there will be an error.
<li> 202: 10/24/11 (version 202 and lower adds backward support (via strip) for double quotes in stuff item options modifier=)
<li> 202: 5/6/11 extends MSGID in emails node from 24 characters from 10
<li> 201: 2/16/11 adds MSGTYPE TICKET in emails node to WEBDBSYNC
<li> 200: 11/15/10 order generation version 5
<li> 117: 4/7/09 changes webdb sync in versioncheck
<li> 116: 5/21/08 re-enables image delete (for 116 and higher)
<li> 116: 4/10/08 note: 115 is was never apparently released due to bugs, skipping to 116 to be safe.
<li> 115: 2/23/08 [note: released to 114] changed format for stids (cheap hack: e.g. abc/123*xyz:ffff becomes 12
<li> 114: 12/26/07 new email changes (shuts down sendmail)
<li> 113: skipped for bad luck
<li> 112: 10/27/07 versions below have backward compatibility for company_logo in merchant sync
<li> 111: 10/09/07 convert ZOM and ZWM clients to ZID
<li> 110: 8/21/07 changes to events (ts was time)
<li> 109: 4/19/07 implements zoovy.virtual zoovy.prod_supplier zoovy.prod_supplierid removes supplier from skulist.
<li> 108: 3/13/07 changes xml output of stuff for orders
[[/SECTION]]
=cut
use Digest::MD5;
use Compress::Bzip2 qw();
use Compress::Zlib qw();
use MIME::Base64;
use IO::String;
use XML::SAX::Simple qw();
use XML::Simple qw();
use Data::Dumper;
use MIME::Base64;
use locale;
use utf8 qw();
use Encode qw();
use lib "/backend/lib";
require ZOOVY;
require PRODUCT;
# require STUFF;
require LUSER;
require ORDER::BATCH;
require ZTOOLKIT::XMLUTIL;
require SITE;
require CART2;
require DOMAIN;
use strict;
##
## called from /webapi/banners
##
sub handle_banners {
my ($req,$HEADERSREF) = @_;
require ADVERT;
my @URLS = ADVERT::retrieve_urls('',15);
my $jsarray = ''; foreach my $url (@URLS) { $jsarray .= "'$url',"; } chop($jsarray);
my $BODY = '';
$HEADERSREF->{'Content-Type'} = 'text/html';
$BODY .= (qq~
<body width="320" height="240" scroll="no" bgcolor="FFFFFF" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" onLoad="progress();">
<center>
<iframe SRC="$URLS[0]" id="iFrameAd" NAME="iFrameAd" WIDTH=320 HEIGHT=240 ALIGN="MIDDLE" FRAMEBORDER=0 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING="no"></iframe>
</center><SCRIPT>
<!--
var counter = 0;
var lastTime = 0;
function progress() {
// called by the flash progress bar
// never flips an add until 15 seconds or more have elapsed
var d = new Date();
var timeIs = d.getTime()/1000;
if (lastTime + 5 < timeIs) {
lastTime = timeIs;
var URLS = new Array($jsarray);
frames['iFrameAd'].location.href = URLS[counter];
counter = counter + 1;
if (counter>=URLS.length) { counter = 0; }
}
setTimeout('progress()',5000);
}
if (window.resizeTo) { top.resizeTo(326,246); }
//-->
</SCRIPT></body>
~);
return(200,$BODY);
}
##
##
##
sub handle_check {
my ($req,$HEADERSREF) = @_;
use lib "/backend/lib";
require DBINFO;
#
# parameters
# V=CLIENT-VERSION
# TYPE=ORDER
# USERNAME=USERNAME
# OID=####-##-######
# DIGEST=base64digest
#
# RESPONSE FORMAT PLAINTEXT HTTP/200 = SUCCESS
# TS:########
# ERR:xyz
#
my $RESPONSE = '';
my $form = $req->parameters();
# leave the digest out for the testing
my $V = $form->{'V'};
my $TYPE = $form->{'TYPE'};
my $USERNAME = $form->{'USERNAME'};
if ($V eq '') {
$RESPONSE = "ERR:V= is a required parameter";
}
elsif ($USERNAME eq '') {
$RESPONSE = "ERR:USERNAME= is a required parameter";
}
elsif (($TYPE eq 'ORDER') && ($form->{'OID'} eq '')) {
$RESPONSE = "ERR:OID is a required parameter for TYPE=ORDER";
}
elsif ($TYPE eq 'ORDER') {
my ($udbh) = &DBINFO::db_user_connect($USERNAME);
my $qtOID = $udbh->quote($form->{'OID'});
my ($MID) = &ZOOVY::resolve_mid($USERNAME);
my $TB = &DBINFO::resolve_orders_tb($USERNAME,$MID);
my $pstmt = "select MODIFIED_GMT from $TB where ORDERID=$qtOID and MID=$MID";
my ($TS) = $udbh->selectrow_array($pstmt);
if (not defined $TS) {
$RESPONSE = "ERR:ORDER was not defined";
}
else {
$RESPONSE = "TS:$TS";
}
&DBINFO::db_user_close();
}
else {
$RESPONSE = "ERR:Unknown TYPE=$TYPE";
}
$HEADERSREF->{'Content-Type'} = 'text/plain';
return(200, $RESPONSE);
}
##
##
##
sub show_msgs {
my ($msgs) = @_;
my $output = '';
foreach my $msg (@{$msgs}) {
my ($type,$msg) = split(/\|/,$msg,2);
$type = uc($type);
my $hint = '';
if ($msg =~ /\n\n/s) { ($msg,$hint) = split(/\n\n/,$msg); }
$msg = &ZOOVY::incode($msg);
if ($hint ne '') {
$msg = "<div align=\"left\">$msg<div align=\"left\" class=\"hint\">".&ZOOVY::incode($hint)."</div></div>"; }
if (($type eq 'SUCCESS') || ($type eq 'WIN') || ($type eq 'INFO')) {
$msg = "<div style='width: 800px; align: center' class='success'>$msg</div>"; }
elsif (($type eq 'WARN') || ($type eq 'WARNING') || ($type eq 'CAUTION')) {
$msg = "<div style='width: 800px; align: center' class='warning'>$msg</div>"; }
elsif (($type eq 'ERROR') || ($type eq 'ERR')) {
$msg = "<div style='width: 800px; align: center' class='error'>$msg</div>"; }
elsif ($type eq 'TODO') { $msg = "<div style='width: 800px; align: center' class='todo'>$msg</div>"; }
elsif ($type eq 'LEGACY') { $msg = "<div style='width: 800px; align: center' class='warning legacy'>$msg</div>"; }
elsif ($type eq 'ISE') { $msg = "<div style='width: 800px; align: center' class='error ise'>$msg</div>"; }
elsif ($type eq 'LINK') {
## LINK|/path/to/url|text
my ($href,$txt) = split(/\|/,$msg,2);
if ($txt eq '') { $hint = "Link $msg"; }
$msg = "<div style='width: 800px; align: center' class='todo'><a target=\"_blank\" href=\"$href\">$txt</a></div>";
}
else {
$msg = "<div style='width: 800px; align: center' class=\"unknown_class_$type\">$msg</div>";
}
$output .= $msg;
}
return($output);
}
sub handle_pogwizard {
my ($req,$HEADERSREF) = @_;
use lib "/backend/lib";
use ZOOVY;
use CGI;
use STUFF::CGI;
use STUFF2;
use POGS;
use strict;
use Data::Dumper;
#
# http://www.zoovy.com/webapi/merchant/pogwizard.cgi?USERNAME=jefatech&PRODUCT=GD58 (legacy)
# http://www.zoovy.com/webapi/merchant/pogwizard.cgi?USERNAME=jefatech&PRODUCT=RV245RD (rich)
# http://www.zoovy.com/webapi/merchant/pogwizard.cgi?USERNAME=outpost&PRODUCT=W9732 (non-inv)
# http://www.zoovy.com/webapi/merchant/pogwizard.cgi?USERNAME=1stproweddingalbums&PRODUCT=WBK101015PBSPKG (excessive)
#
my $BODY = '';
my $q = new CGI;
my $USERNAME = $q->param('USERNAME');
my $PRODUCT = $q->param('PRODUCT');
if ((not defined $PRODUCT) || ($PRODUCT eq '')) { $PRODUCT = $q->param('product'); }
my $CLIENT = $q->param('CLIENT');
my $STID = $q->param('STID');
my $COMPAT = int($q->param('COMPAT'));
if ($COMPAT==0) { $COMPAT = 107; }
print STDERR "USER:$USERNAME PRODUCT:$PRODUCT COMPAT: $COMPAT\n";
my $VERB = $q->param('VERB');
my ($P) = PRODUCT->new($USERNAME,$PRODUCT,'create'=>0);
# my @pogs = &POGS::text_to_struct($USERNAME,$prodref->{'zoovy:pogs'},1);
my $selectedref = {};
my $lm = LISTING::MSGS->new($USERNAME);
my ($stuff2) = STUFF2->new($USERNAME);
if ($VERB eq 'SAVE') {
require STUFF::CGI;
## Build a hashref of key/value pairs!
my %params = ();
my %lcparams = ();
foreach my $k ($q->param()) {
$params{$k} = $q->param($k);
$lcparams{lc($k)} = $params{$k};
}
#my $stuff = STUFF->new($USERNAME);
#my @errors = ();
#my @items = &STUFF::CGI::parse_products($USERNAME,\%lcparams,0,\@errors);
#foreach my $item (@items) {
# $stuff->legacy_cram($item);
# }
## note: we need to pass zero_qty_okay but it seems like legacy_parse already does that for us
($stuff2,$lm) = &STUFF::CGI::legacy_parse($stuff2,\%lcparams,'*LM'=>$lm);
if (not $lm->can_proceed()) {
$VERB = 'TRYAGAIN';
}
}
if ($VERB eq 'SAVE') {
my ($xml,$errors) = $stuff2->as_xml($COMPAT);
$HEADERSREF->{'Content-Type'} = 'text/html';
$BODY .= (qq~
<html>
<font color="blue">Success!</font><br>
<b>This window should close automatically in a moment.</b><br>
<!--
<POGWIZARD>$xml</POGWIZARD>
-->~);
$BODY .= (qq~</html>~);
}
if (($VERB eq '') || ($VERB eq 'TRYAGAIN')) {
$HEADERSREF->{'Content-Type'} = 'text/html';
my $msgs = '';
if ($VERB eq 'TRYAGAIN') {
my @msgs = ();
foreach my $msg (@{$lm->msgs()}) {
my ($msgref,$status) = LISTING::MSGS::msg_to_disposition($msg);
push @msgs, "$status|$msgref->{'+'}";
}
$msgs = &WEBAPI::show_msgs(\@msgs);
}
my $html = &POGS::struct_to_html($P,$selectedref,4);
$BODY .= (qq~
<head>
<link rel="STYLESHEET" type="text/css" href="/biz/standard.css">
</head>
<body>
<form method=post action="/webapi/merchant/pogwizard.cgi">
<input type="hidden" name="USERNAME" value="$USERNAME">
<input type="hidden" name="product" value="$PRODUCT">
<input type="hidden" name="CLIENT" value="$CLIENT">
<input type="hidden" name="VERB" value="SAVE">
<input type="hidden" name="COMPAT" value="$COMPAT">
$msgs
$html
<input type="submit" value=" Submit ">
</form>
</body>
~);
}
return(200,$BODY);
}
sub handle_sync {
my ($req,$HEADERSREF) = @_;
## my $HEADERS_IN = $req->headers();
my $MID = 0;
my $USERNAME = ''; # the X-ZOOVY-USERNAME variable
my $LUSER = ''; # Login Username (e.g. the value after the *)
my $SERVER = &ZOOVY::servername();
my $DATA = ''; # the actual DATA received from the post.
my $LENGTH = -1; # the X-LENGTH variable
my $XREQUEST = ''; # the X-ZOOVY-REQUEST variable
my $XTIME = -1; # the X-TIME variable
my $XAPI = ''; # the API name as it was passed X-ZOOVY-API
my $ACTUALMD5 = ''; # the computed MD5 value
my $XSECURITY = ''; # the X-ZOOVY-SECURITY variable
my $API = ''; # the actual API (before the params)
my @APIPARAMS = (); # the parameters (delimited by /)
my $EC = 0; # Error Code
my $XCOMPRESS = ''; # the type of compress specified by X-COMPRESS
$::XCLIENTCODE = 'WTF'; # the code portion of the X-CLIENT variable (e.g. ZOM/ZWM)
$::XCOMPAT = 0; # the compatability mode
$::XERRORS = 0; # the X-ERRORS variable
$ENV{"REMOTE_ADDR"} = $req->address();
##
## $::XCOMPAT = 100 -- initial release
## $::XCOMPAT = 101 -- 12/25/04 sync process for skulist changed formats slightly.
## $::XCOMPAT = 102 -- 1/31/05 added VERSION 2 to ZSHIP::xml_out
## $::XCOMPAT = 103 -- 4/4/05 changes to customer sync [CRITICAL ERROR]
## $::XCOMPAT = 104 -- 4/25/05 changes to customer sync <CUSTOMERSYNC>
## $::XCOMPAT = 105 -- 5/28/05 adds required X-CLIENT code. (zwm)
## $::XCOMPAT = 106 -- 10/26/05 adds support for X-CLIENT:VERSION:SEAT (zwm)
## $::XCOMPAT = 107 -- 7/18/06 changes format for incompletesync. (zom)
## $::XCOMPAT = 108 -- 3/13/07 changes xml output of stuff for orders
## $::XCOMPAT = 109 -- 4/19/07 implements zoovy.virtual zoovy.prod_supplier zoovy.prod_supplierid removes supplier from skulist.
## $::XCOMPAT = 110 -- 8/21/07 changes to events (ts was time)
## $::XCOMPAT = 111 -- 10/09/07 convert ZOM and ZWM clients to ZID
## $::XCOMPAT = 112 -- 10/27/07 versions below have backward compatibility for company_logo in merchant sync
## $::XCOMPAT = 113 -- skipped for bad luck
## $::XCOMPAT = 114 -- 12/26/07 new email changes (shuts down sendmail)
## $::XCOMPAT = 115 -- 2/23/08 [note: released to 114] changed format for stids (cheap hack: e.g. abc/123*xyz:ffff becomes 123*abc/xyz:ffff)
## $::XCOMPAT = 116 -- 4/10/08 note: 115 is was never apparently released due to bugs, skipping to 116 to be safe.
## $::XCOMPAT = 116 -- 5/21/08 re-enables image delete (for 116 and higher)
## $::XCOMPAT = 117 -- 4/7/09 changes webdb sync in versioncheck
## $::XCOMPAT == **HEY** we're maintaining this document in WEBAPI.pm now (around line 37)
##
if ($EC==0) { if ($SERVER eq '') { $EC = 998; } }
## Verify user exists!
if ($EC==0) {
$USERNAME = $req->header(lc('X-ZOOVY-USERNAME'));
## separate the LUSER from the USERNAME
if (index($USERNAME,'*')>=0) { ($USERNAME,$LUSER) = split(/\*/,$USERNAME); }
$MID = &ZOOVY::resolve_mid($USERNAME);
if ($USERNAME eq '') { $EC = 1000; }
}
## Read in Data and Check Length
if ($EC == 0) {
$LENGTH = int($req->header(lc('X-LENGTH')));
if ($LENGTH < 0) { $EC = 1002; }
$DATA = $req->content();
#open F, ">/tmp/body";
#print F $DATA."\n";
#use Data::Dumper; print F Dumper($req->headers());
#close F;
if (length($DATA)!=$LENGTH) {
$EC = 2000;
$WEBAPI::ERRORS{$EC} = "Oh My! Length of content ".length($DATA)." received does not match X-LENGTH ($LENGTH).";
}
}
if (defined $req->header(lc('X-ZOOVY-API'))) {
$XAPI = $req->header(lc('X-ZOOVY-API'));
}
else { $EC = 1003; }
if ( ($EC==0) && ($XAPI eq '') ) { $EC = 2003; }
($API,@APIPARAMS) = split(/\//,$XAPI);
if ( ($EC==0) && ($API eq 'PAYPROCESS') ) { $EC = 3; }
if ( ($EC==0) && (not defined $WEBAPI::APIS{$API}) ) {
$EC = 2004; $WEBAPI::ERRORS{$EC} = "Unknown/Invalid API [$API] called.";
}
## check for X-ZOOVY-REQUEST and X-TIME
if ($EC==0) {
if (defined $req->header(lc('X-ZOOVY-REQUEST'))) { $XREQUEST = $req->header(lc('X-ZOOVY-REQUEST')); } else { $EC = 1004; }
if (($EC==0) && ($XREQUEST eq '')) { $EC = 2001; }
if (defined $req->header(lc('X-TIME'))) { $XTIME = $req->header(lc('X-TIME')); } else { $EC = 1005; }
}
if ($EC==0) {
## check to make sure X-CLIENT is set.
($::XCLIENTCODE) = $req->header(lc('X-CLIENT')); # get the ZOM or ZWM out of the X-CLIENT variable
my ($code,$version,$seat) = split(/\:/,$::XCLIENTCODE,2);
if ($code eq '') { $EC = 2098; }
elsif ($code =~ /^ZID\./) {} ## all series 8 desktop clients!
elsif ($code eq 'ZID') {} ## ZOOVY INTEGRATED DESKTOP
elsif ($code eq 'ZOM') {} ## ORDER MANAGER
elsif ($code eq 'ZOME') {} ## ENTERPRISE CLIENT
elsif ($code eq 'ZSM') {} ## SYNC MANAGER
elsif ($code eq 'ZWM') {} ## WAREHOUSE MANAGER
elsif ($code eq 'API') {} ## WAREHOUSE MANAGER
else { $EC = 2099; }
}
## make sure we always set the compatibilitylevel otherwise we echo an old compat level zero warning.
if (defined $req->header(lc('X-COMPAT'))) { $::XCOMPAT = int($req->header(lc('X-COMPAT'))); }
if ($EC==0) {
if ($::XCOMPAT<$WEBAPI::MIN_ALLOWED_COMPAT_LEVEL) { $EC = 1024; }
elsif ($::XCOMPAT>$WEBAPI::MAX_ALLOWED_COMPAT_LEVEL) { $EC = 1025; }
if ($EC>0) {
&ZOOVY::confess($USERNAME,"WEBAPI VERSION: $::XCOMPAT is no longer available MIN:$WEBAPI::MIN_ALLOWED_COMPAT_LEVEL MAX:$WEBAPI::MAX_ALLOWED_COMPAT_LEVEL");
}
}
## verify security
if ($EC==0) {
$XSECURITY = $req->header(lc('X-ZOOVY-SECURITY'));
my $XPASS = undef;
if ($XSECURITY eq '') {
$EC = 1001;
}
my ($CODE,$VERSION,$SEAT) = split(/\:/,$::XCLIENTCODE,3);
if ($::XCOMPAT>=111) {
if (($CODE eq 'ZOM') || ($CODE eq 'ZWM')) { $CODE = 'ZID'; }
}
if ($EC==0) {
## check to make sure we have a license (and appropriate seat count)
my ($gref) = &ZWEBSITE::fetch_globalref($USERNAME);
my $FLAGS = ','.$gref->{'cached_flags'}.',';
# my $FLAGS = ','.&ZWEBSITE::fetch_website_attrib($USERNAME,'cached_flags').',';
$FLAGS =~ s/ZPM/ZWM/g; # convert warehouse manager to product manager seats
$EC = 556;
if ($CODE =~ /^ZID\./) {
## all series 8 desktop clients match the regex above!
$SEAT = 1;
}
elsif ((($CODE eq 'ZOME') || ($CODE eq 'ZOM') || ($CODE eq 'ZWM') || ($CODE eq '')) && ($SEAT eq '')) {
$SEAT = 1;
}
# print STDERR "SEAT[$SEAT] CODE[$CODE]\n";
foreach my $flag (split(/,/,$FLAGS)) {
my ($flag,$count) = split(/\*/,$flag);
if (int($count)==0) { $count++; }
# print STDERR "FLAG: [$flag] eq [$CODE] $SEAT>0 $SEAT<=$count\n";
if (($flag eq $CODE) && ($SEAT>0) && ($SEAT<=$count)) {
$EC = 0; # whew, we're licensed to use this seat!
}
}
## CHEAP HACK FOR SYNC MANAGER!
if ($CODE eq 'ZSM') { $EC = 0; }
if ($CODE eq 'ZOME') { $EC = 0; }
if ($CODE eq 'ZID') { $EC = 0; $SEAT = ''; }
if ($CODE eq 'API') { $EC = 0; $SEAT = ''; }
if ($CODE =~ /ZID\./) {
$CODE = 'ZID';
$EC = 0; $SEAT = '';
} ## version 8 of ZID series.
if ($FLAGS !~ /,BASIC,/) {
$EC = 555; # insufficient access
}
if (($FLAGS =~ /,PKG=SHOPCART,/) && ($FLAGS !~ /,ZID,/)) {
$EC = 555;
}
}
if ($EC==0) {
my $PASSWORD = '';
my $MODE = 'PASSWORD';
if ($XSECURITY =~ /^([A-Z]+)\:(.*?)$/) { $MODE = $1; $XSECURITY = $2; }
# print STDERR "XSECURITY: [$XSECURITY]\n";
if ($EC != 0) {
}
elsif ($XSECURITY eq '') {
$EC = 2008;
}
elsif ($MODE eq 'TOKEN') {
## if we pass TOKEN:digest then we lookup token_zom or token_zwm
if ((int($SEAT)==0) || (int($SEAT)==1)) { $SEAT = ''; }
# print STDERR "Looking for Token: 'token_'.lc($CODE.$SEAT)\n";
my ($gref) = &ZWEBSITE::fetch_globalref($USERNAME);
my $TOKEN = $gref->{'webapi_zid'} || $gref->{'%plugins'}->{'desktop.zoovy.com'}->{'~password'};
my $IN = $USERNAME . (($LUSER ne '')?'*'.$LUSER:'') . $TOKEN . $XAPI . $XREQUEST . $XTIME . $DATA;
my $ACTUALMD5 = Digest::MD5::md5_hex( $IN );
if ($XSECURITY ne $ACTUALMD5) {
$EC = 2009;
}
}
elsif ($MODE eq 'PASSWORD') {
## this is for PASS:digest
##if ($LUSER eq '') { $LUSER = 'admin'; }
#my $zdbh = &DBINFO::db_user_connect($USERNAME);
#my $pstmt = "select PASSHASH,PASSSALT,IS_ADMIN from LUSERS where MID=$MID and USERNAME=".$zdbh->quote($USERNAME)." and LUSER=".$zdbh->quote($LUSER);
#&DBINFO::db_user_close();
$LUSER = '';
my ($gref) = &ZWEBSITE::fetch_globalref($USERNAME);
my $CFG = $gref->{'%plugins'}->{'desktop.zoovy.com'} || {};
if (not $CFG->{'enable'}) {
$EC = 2011;
}
elsif ($CFG->{'~password'} eq '') {
$EC = 2006;
}
else {
$PASSWORD = $CFG->{'~password'};
}
if ($EC == 0) {
my $IN = $USERNAME . (($LUSER ne '')?'*'.$LUSER:'') . $PASSWORD . $XAPI . $XREQUEST . $XTIME . $DATA;
$ACTUALMD5 = Digest::MD5::md5_hex( $IN );
if ($ACTUALMD5 ne $XSECURITY) {
$EC = '2005';
}
}
#elsif ($XPASS eq '') {
# $EC = 2006;
# }
#open F, ">/tmp/x";
#print F "X:$PASSHASH $PASSSALT -$XPASS- $XSECURITY\n";
#close F;
## ORDER MANAGER SENDS ENCRYPTED PASSWORDS OVER SSL! WTF
#elsif (uc($PASSWORD) ne uc($XPASS)) {
# $EC = '2006';
# }
}
else {
$EC = 2007;
}
print STDERR "SERVER: CLIENT=$::XCLIENTCODE USER=$USERNAME\n";
}
}
## Handle Compression
if ($EC==0) {
$::XERRORS = $req->header(lc('X-ERRORS'));
$XCOMPRESS = $req->header(lc('X-COMPRESS'));
my $xc = $XCOMPRESS;
if ($DATA eq '') {
# print STDERR "DATA IS BLANK [API=$API]\n";
}
elsif (substr($xc,0,7) eq 'BASE64:') {
## Un-MIME encode if necessary
$xc = substr($xc,7); # Strip BASE64:
$DATA = decode_base64($DATA);
# print STDERR "DID DECODE\n";
}
if ($DATA eq '') {
## no data, don't try to decompress!
}
elsif ($xc eq 'NONE') {
## no compress!
}
elsif ($xc eq 'BZIP2') {
my $TMP = $DATA;
$DATA = Compress::Bzip2::decompress($TMP);
if (not defined $DATA) { $EC = 997; }
}
elsif ($xc eq 'GZIP') {
# $dest = Compress::Zlib::memGzip($buffer) ;
$DATA = Compress::Zlib::memGunzip($DATA);
if (not defined $DATA) { $EC = 996; }
}
elsif ($xc eq 'ZLIB') {
$DATA = Compress::Zlib::uncompress($DATA);
if (not defined $DATA) { $EC = 995; }
}
else {
$EC = 1006;
}
}
my $DEBUG = qq~<Debug>
<x-request>$XREQUEST</x-request>
<x-username>$USERNAME</x-username>
<x-mid>$MID</x-mid>
<x-api>$XAPI</x-api>
<x-time>$XTIME</x-time>
<x-security>$XSECURITY</x-security>
<x-realsecurity>$ACTUALMD5</x-realsecurity>
</Debug>~;
$DEBUG = '';
## NOT LEGACY
# print STDERR "NOT LEGACY\n";
$HEADERSREF->{'Content-Type'} = 'text/xml';
my $BODY = '';
$BODY .= ("<Response>\n");
$BODY .= ("<Server>".&ZOOVY::servername()."</Server>\n");
$BODY .= ("<StartTime>".(time())."</StartTime>\n");
#if ($USERNAME eq 'bamtar') { $::XERRORS++; }
#if ($USERNAME eq 'froggysfog') { $::XERRORS++; }
if (-f "/dev/shm/sync.debug") {
open F, ">>/tmp/sync.log";
print F sprintf("%d\t%s\n",time(),$XAPI);
close F;
}
if ($EC!=0) {
$BODY .= ($DEBUG);
$BODY .= ("<Errors>\n");
if ($XREQUEST eq '') { $XREQUEST = -1; }
$BODY .= ("<Error Id=\"$XREQUEST\" Code=\"$EC\">[ERR#$EC] $WEBAPI::ERRORS{$EC}</Error>\n");
$BODY .= ("</Errors>\n");
}
elsif ($EC==0) {
$BODY .= ($DEBUG);
$BODY .= ("<Api>$API</Api>\n");
my ($BatchID,$PickupTime,$xmlOut) = ();
my ($GUID) = $req->header(lc('X-GUID'));
if ($GUID ne '') {
require BATCHJOB;
my ($bj) = BATCHJOB->create($USERNAME,
'PRT'=>0,
'GUID'=>$GUID,
'EXEC'=>'WEBAPI/API',
'%VARS'=>{ XAPI=>$XAPI, XREQUEST=>$XREQUEST, DATA=>$DATA },
'TITLE'=>"WEBAPI $API",
);
$bj->start();
($BatchID) = $bj->id();
}
else {
my ($udbh) = &DBINFO::db_user_connect($USERNAME);
eval {
($PickupTime,$xmlOut) = $WEBAPI::APIS{$API}->($USERNAME,$XAPI,$XREQUEST,$DATA);
## note: since db_user_close returns 0 - it causes the eval to return zero.
};
if ($@) {
$PickupTime = -2;
$xmlOut = "WEBAPI $XAPI Failure\n$@\n";
&ZOOVY::confess(
$USERNAME,"$xmlOut\n===== XREQUEST: ======\n$XREQUEST\n\n===== DATA: =====\n$DATA\n\n\n",
justkidding=>1
);
}
&DBINFO::db_user_close();
}
if (($PickupTime==0) && ($BatchID>0)) {
$BODY .= ("<Batch ID=\"$BatchID\" GUID=\"$GUID\"></Batch>\n");
}
elsif ($PickupTime<0) {
$BODY .= ("<Errors><Error Id=\"$XREQUEST\" Code=\"$PickupTime\">$xmlOut</Error></Errors>\n");
}
else {
$BODY .= (&WEBAPI::addRequest($XCOMPRESS,$XREQUEST,$PickupTime,$xmlOut));
}
if ($::XERRORS) {
use Data::Dumper;
open F, ">/tmp/XERRORS-$USERNAME-$XREQUEST-$XCOMPRESS.$::XERRORS";
print F "XREQUEST: $XREQUEST\n";
print F Dumper($USERNAME,$XAPI,$XREQUEST,$DATA);
print F "\n\n\n\n-------------------------------------------------------------\nOUTPUT: ".Dumper($PickupTime,$xmlOut);
close F;
}
}
$BODY .= ("<Time>".(time())."</Time>\n");
$BODY .= ("</Response>\n");
return(200,$BODY);
}
sub userlog {
my ($USERNAME,$API,$MSG) = @_;
return(LUSER::log( { LUSER=>'WEBAPI', USERNAME=>$USERNAME }, "WEBAPI.$API", $MSG, "API"));
}
%WEBAPI::ERRORS = (
'1' => 'Generic Error... shit happened!',
'2' => 'WEBAPI Internal Application Failure',
'3' => 'We regret to inform you that your requested action was denied because payment processing with Order Manager v10 is no longer compatible with the Zoovy servers.',
'555' => 'Insufficient access flags, please contact Zoovy support',
'556' => 'Seat not licensed',
'989' => 'Internal MIME Base64 encoding error.',
'990' => 'Internal MIME Base64 decoding error.',
'995' => 'Internal Zlib decomrpession error. Could not decompress data.',
'996' => 'Internal Gzip decompression error. Could not decompress data.',
'997' => 'Internal Bzip2 decompression error. Could not decompress data.',
'998' => 'Internal Error - cannot determine server name.',
'999' => 'Internal Error - cannot connect to database!',
'1000' => 'X-ZOOVY-USERNAME was not found in header.',
'1001' => 'X-ZOOVY-SECURITY was not found in header.',
'1002' => 'X-LENGTH was not found in header.',
'1003' => 'X-ZOOVY-API was not found in header.',
'1004' => 'X-ZOOVY-REQUEST was not found in header.',
'1005' => 'X-TIME was not found in header.',
'1006' => 'X-COMPRESS is missing or contains an invalid value, please pick one: NONE|BZIP2|GZIP.',
'1024' => 'X-COMPAT Error Compatibility level too low, please upgrade to a newer version of your software.',
'1025' => 'X-COMPAT Error Compatibility level passed exceeds maximum allowed value.',
'2000' => 'Length of content does not match X-Length.',
'2001' => 'X-ZOOVY-REQUEST appears to be blank.',
'2002' => 'Deviation of X-TIME too large.',
'2003' => 'Variable X-ZOOVY-API was received, but is blank.',
'2004' => 'Invalid API called.',
'2005' => 'Security Digests do not match - probably invalid password.',
'2006' => 'Received legacy X-ZOOVY-PASSWORD variable, but it did not match password on file.',
'2007' => 'Received unknown MODE: in XSECURITY try either PASSWORD or TOKEN',
'2008' => 'X-SECURITY cannot be blank',
'2009' => 'X-SECURITY is invalid.',
'2010' => 'User attempting to authenticate does not have administrative access',
'2011' => 'Admin sync is not enabled - please go to Setup | Integrations | Shipping | Zoovy Desktop and enable it.',
'2012' => 'Admin sync password cannot be blank',
'2098' => 'X-CLIENT is required at this compatibility level',
'2099' => 'Unknown X-CLIENT value - please contact zoovy support',
);
%WEBAPI::APIS = (
'BATCH' => \&WEBAPI::batch,
'SUPPORT' => \&WEBAPI::support,
'TEST' => \&WEBAPI::testSync,
'PICKUP' => \&WEBAPI::Pickup,
'ORDERSYNC' => \&WEBAPI::OrderSync,
'ORDERLIST' => \&WEBAPI::OrderList,
'CUSTOMERSYNC' => \&WEBAPI::CustomerSync,
# 'PAYPROCESS' => \&WEBAPI::payProcess,
'CUSTOMERPROCESS' => \&WEBAPI::customerProcess,
'ORDERPROCESS' => \&WEBAPI::orderProcess,
'CALCSHIP' => \&WEBAPI::calcShip,
'ORDERBLOCK' => \&WEBAPI::orderBlock,
'INCOMPLETESYNC' => \&WEBAPI::incompleteSync,
'MERCHANTSYNC' => \&WEBAPI::merchantSync,
'WEBDBSYNC' => \&WEBAPI::webdbSync,
'INVENTORYSYNC' => \&WEBAPI::inventorySync,
'SKULIST' => \&WEBAPI::skulistSync,
'VERSIONCHECK' => \&WEBAPI::versioncheck,
'PAYMENTMETHODS' => \&WEBAPI::paymentMethodsSync,
'NAVCATSYNC' => \&WEBAPI::navcatSync,
'LOOKUP' => \&WEBAPI::lookup,
'PRODSYNC' => \&WEBAPI::prodSync,
'IMAGESYNC' => \&WEBAPI::imageSync,
'SENDMAIL' => \&WEBAPI::sendMail,
'ADMINSYNC' => \&WEBAPI::adminSync,
'REGISTER' => \&WEBAPI::registerSync,
'TOXMLSYNC' => \&WEBAPI::toxmlSync,
'LAUNCHGROUPSYNC' => \&WEBAPI::launchGroupSync,
'SOGSYNC' => \&WEBAPI::sogSync,
'GIFTCARDSYNC' => \&WEBAPI::giftCardSync,
'WALLETSYNC' => \&WEBAPI::walletSync,
'CSVIMPORT' => \&WEBAPI::csvImportSync,
'RESOURCE' => \&WEBAPI::resourceSync,
'SUPPLIERSYNC' => \&WEBAPI::supplierSync,
);
## goes through and replaces all the keys that have colons with dashes e.g.
## zoovy:prod_name becomes zoovy-prod_name
sub hashref_colon_to_dashxml {
my ($hashref) = @_;
my $BUFFER = "";
my $k2 = '';
foreach my $k (keys %{$hashref}) {
next if (index($k,':')<0);
$k2 = lc($k);
$k2 =~ s/\:/-/;
$BUFFER .= "<$k2>".&ZOOVY::incode($hashref->{$k})."</$k2>\n";
}
return($BUFFER);
}
##
## SUPPLIERSYNC
##
#=pod
#
#[[SECTION]API: SUPPLIERSYNC]
#sends XML data from pub1 to Supply Chain
#ie Order Confirmations
#[[/SECTION]]
#
#=cut
#
sub supplierSync {
my ($USERNAME,$XAPI,$ID,$DATA) = @_;
require SUPPLIER;
my ($API,$METHOD,$ACTION) = split(/\//,$XAPI,3);
## SUPPLIER::from_xml deals with error handling
my ($ERROR,$XML) = SUPPLIER::from_xml($USERNAME,$DATA,$METHOD,$ACTION,$::XCOMPAT);
## embed error in XML as needed
if ($ERROR ne '') { $XML .= "<Errors><Error>$ERROR</Error></Errors>"; }
$XML = "<supplier$METHOD$ACTION>$XML</supplier$METHOD$ACTION>";
return($ERROR,$XML);
}
##
## Records the registration of a client.
##
sub registerSync {
my ($USERNAME,$XAPI,$ID,$DATA) = @_;
require ZTOOLKIT::SECUREKEY;
my $securekey = &ZTOOLKIT::SECUREKEY::gen_key($USERNAME,'ZO');
##
## insert overly elaborate seat registration process here.
##
my $XML = qq~<SecureKey>$securekey</SecureKey>~;
$XML = "<Registration>$XML</Registration>";
return($XML);
}
##
## RESOURCE/filename
##
=pod
[[SECTION]API: RESOURCE]
Purpose: Downloads internal resource files from Zoovy. Files can be requested as either .xml, .json, or .yaml
[[BREAK]]
<li> shipcodes.ext: a list of all carrier codes, and associated properties.
<li> shipcountries.ext: a list of all shipping countries, if they are safe, etc.
<li> flexedit.ext: a list of all fields in the zoovy platform
<li> payment_status.ext: a complete list of all possible payment status codes, and their definitions.
<li> review_status.ext: a complete list of all possible review/fraud status codes, and their definitions.
<li> integrations.ext: a complete list of all integrations and their id's, dst codes, meta, attributes, etc.
<li> warehosues.ext:
[[/SECTION]]
=cut
sub resourceSync {
my ($USERNAME,$XAPI,$ID,$DATA) = @_;
require XML::Simple;
require JSON::XS;
my $xs = new XML::Simple(ForceArray=>1,KeyAttr=>"");
my $out = '';
my ($API,$FILENAME) = split(/\//,$XAPI,3);
my $ref = undef;
my $EXT = undef;
if ($FILENAME =~ /^shipcodes\.(.*?)$/) {
$EXT = $1;
$ref = \%ZSHIP::SHIPCODES;
# $out = $xs->XMLout(\%ZSHIP::SHIPCODES);
}
elsif ($FILENAME =~ /^shipcountries\.(.*?)$/) {
$EXT = $1;
$ref = Storable::retrieve("/httpd/static/country-highrisk.bin");
if ($EXT eq 'xml') {
$ref = { 'country'=>$ref };
}
}
elsif ($FILENAME =~ /^flexedit\.(.*?)$/) {
$EXT = $1;
require PRODUCT::FLEXEDIT;
$ref = \%PRODUCT::FLEXEDIT::fields;
}
elsif ($FILENAME =~ /^payment_status\.(.*?)$/) {
$EXT = $1;
require ZPAY;
$ref = [];
foreach my $ps (sort keys %ZPAY::PAYMENT_STATUS) {
push @{$ref}, { 'ps'=>$ps, 'txt'=>$ZPAY::PAYMENT_STATUS{$ps} };
}
}
elsif ($FILENAME =~ /^review_status\.(.*?)$/) {
$EXT = $1;
require ZPAY;
$ref = [];
foreach my $rs (sort keys %ZPAY::REVIEW_STATUS) {
push @{$ref}, { 'rs'=>$rs, 'txt'=>$ZPAY::REVIEW_STATUS{$rs} };
}
}
elsif ($FILENAME eq /^integrations\.(.*?)$/) {
$EXT = $1;
$ref = \@ZOOVY::INTEGRATIONS;
}
elsif ($FILENAME eq /^warehouse.(.*?)$/) {
$EXT = $1;
}
if ($EXT eq 'yaml') {
$out = YAML::Syck::Dump($ref);
}
elsif ($EXT eq 'xml') {
$out = $xs->XMLout($ref);
}
elsif ($EXT eq 'json') {