forked from interchange/interchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.pm
2529 lines (2173 loc) · 57.5 KB
/
Util.pm
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
# Vend::Util - Interchange utility functions
#
# Copyright (C) 2002-2009 Interchange Development Group
# Copyright (C) 1996-2002 Red Hat, Inc.
#
# This program was originally based on Vend 0.2 and 0.3
# Copyright 1995 by Andrew M. Wilcox <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA.
package Vend::Util;
require Exporter;
unless( $ENV{MINIVEND_DISABLE_UTF8} ) {
require Encode;
import Encode qw( is_utf8 encode_utf8 );
}
@ISA = qw(Exporter);
@EXPORT = qw(
adjust_time
catfile
check_security
copyref
currency
dbref
dump_structure
errmsg
escape_chars
evalr
dotted_hash
file_modification_time
file_name_is_absolute
find_special_page
format_log_msg
generate_key
get_option_hash
hash_string
header_data_scrub
hexify
is_hash
is_no
is_yes
l
lockfile
logData
logDebug
logError
logGlobal
logOnce
logtime
random_string
readfile
readin
round_to_frac_digits
secure_vendUrl
send_mail
setup_escape_chars
set_lock_type
show_times
string_to_ref
tag_nitems
timecard_stamp
timecard_read
backtrace
uneval
uneval_it
uneval_fast
unhexify
unlockfile
vendUrl
);
use strict;
no warnings qw(uninitialized numeric);
use Config;
use Fcntl;
use Errno;
use Text::ParseWords;
require HTML::Entities;
use Vend::Safe;
use Vend::File;
use subs qw(logError logGlobal);
use vars qw($VERSION @EXPORT @EXPORT_OK);
$VERSION = substr(q$Revision: 2.127 $, 10);
my $Eval_routine;
my $Eval_routine_file;
my $Pretty_uneval;
my $Fast_uneval;
my $Fast_uneval_file;
### END CONFIGURABLE MODULES
## ESCAPE_CHARS
$ESCAPE_CHARS::ok_in_filename =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
'abcdefghijklmnopqrstuvwxyz' .
'0123456789' .
'-:_.$/'
;
$ESCAPE_CHARS::ok_in_url =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
'abcdefghijklmnopqrstuvwxyz' .
'0123456789' .
'-_./~='
;
## This is a character class for HTML::Entities
$ESCAPE_CHARS::std = qq{^\n\t\X !\#\$%\'-;=?-Z\\\]-~};
## Some standard error templates
## This is an alias for a commonly-used function
*dbref = \&Vend::Data::database_exists_ref;
my $need_escape;
sub setup_escape_chars {
my($ok, $i, $a, $t);
## HTML::Entities caches this, let's get it cached right away so
## each child doesn't have to re-eval
my $junk = ">>>123<<<";
HTML::Entities::encode($junk, $ESCAPE_CHARS::std);
foreach $i (0..255) {
$a = chr($i);
if (index($ESCAPE_CHARS::ok_in_filename,$a) == -1) {
$t = '%' . sprintf( "%02X", $i );
}
else {
$t = $a;
}
$ESCAPE_CHARS::translate[$i] = $t;
if (index($ESCAPE_CHARS::ok_in_url,$a) == -1) {
$t = '%' . sprintf( "%02X", $i );
}
else {
$t = $a;
}
$ESCAPE_CHARS::translate_url[$i] = $t;
}
my $string = "[^$ESCAPE_CHARS::ok_in_url]";
$need_escape = qr{$string};
}
# Replace any characters that might not be safe in a filename (especially
# shell metacharacters) with the %HH notation.
sub escape_chars {
my($in) = @_;
my($c, $r);
$r = '';
foreach $c (split(m{}, $in)) {
$r .= $ESCAPE_CHARS::translate[ord($c)];
}
# safe now
return $r;
}
# Replace any characters that might not be safe in an URL
# with the %HH notation.
sub escape_chars_url {
my($in) = @_;
return $in unless $in =~ $need_escape;
my($c, $r);
$r = '';
foreach $c (split(m{}, $in)) {
$r .= $ESCAPE_CHARS::translate_url[ord($c)];
}
# safe now
return $r;
}
# Returns its arguments as a string of tab-separated fields. Tabs in the
# argument values are converted to spaces.
sub tabbed {
return join("\t", map { $_ = '' unless defined $_;
s/\t/ /g;
$_;
} @_);
}
# Returns time in HTTP common log format
sub logtime {
return POSIX::strftime("[%d/%B/%Y:%H:%M:%S %z]", localtime());
}
sub format_log_msg {
my($msg) = @_;
my(@params);
# IP, Session, REMOTE_USER (if any) and time
push @params, ($CGI::remote_host || $CGI::remote_addr || '-');
push @params, ($Vend::SessionName || '-');
push @params, ($CGI::user || '-');
push @params, logtime() unless $Global::SysLog;
# Catalog name
my $string = ! defined $Vend::Cfg ? '-' : ($Vend::Cat || '-');
push @params, $string;
# Path info and script
$string = $CGI::script_name || '-';
$string .= $CGI::path_info || '';
push @params, $string;
# Message, quote newlined area
$msg =~ s/\n/\n> /g;
push @params, $msg;
return join " ", @params;
}
sub round_to_frac_digits {
my ($num, $digits) = @_;
if (defined $digits) {
# use what we were given
}
elsif ( $Vend::Cfg->{Locale} ) {
$digits = $Vend::Cfg->{Locale}{frac_digits};
$digits = 2 if ! defined $digits;
}
else {
$digits = 2;
}
my @frac;
$num =~ /^(-?)(\d*)(?:\.(\d+))?$/
or return $num;
my $sign = $1 || '';
my $int = $2;
@frac = split(m{}, ($3 || 0));
local($^W) = 0;
my $frac = join "", @frac[0 .. $digits - 1];
if($frac[$digits] > 4) {
$frac++;
}
if(length($frac) > $digits) {
$int++;
$frac = 0 x $digits;
}
$frac .= '0' while length($frac) < $digits;
return "$sign$int.$frac";
}
use vars qw/%MIME_type/;
%MIME_type = (qw|
jpg image/jpeg
gif image/gif
jpeg image/jpeg
png image/png
xpm image/xpm
htm text/html
html text/html
txt text/plain
asc text/plain
csv text/plain
xls application/vnd.ms-excel
default application/octet-stream
|
);
# Return a mime type based on either catalog configuration or some defaults
sub mime_type {
my ($val) = @_;
$val =~ s:.*\.::s;
! length($val) and return $Vend::Cfg->{MimeType}{default} || 'text/plain';
$val = lc $val;
return $Vend::Cfg->{MimeType}{$val}
|| $MIME_type{$val}
|| $Vend::Cfg->{MimeType}{default}
|| $MIME_type{default};
}
# Return AMOUNT formatted as currency.
sub commify {
local($_) = shift;
my $sep = shift || ',';
1 while s/^(-?\d+)(\d{3})/$1$sep$2/;
return $_;
}
my %safe_locale = (
C => 1,
en_US => 1,
en_UK => 1,
en_GB => 1,
);
sub safe_sprintf {
# need to supply $fmt as a scalar to prevent prototype problems
my $fmt = shift;
# query the locale
my $save = POSIX::setlocale (&POSIX::LC_NUMERIC);
# This should be faster than doing set every time....but when
# is locale C anymore? Should we set this by default?
return sprintf($fmt, @_) if $safe_locale{$save};
# Need to set.
POSIX::setlocale (&POSIX::LC_NUMERIC, 'C');
my $val = sprintf($fmt, @_);
POSIX::setlocale (&POSIX::LC_NUMERIC, $save);
return $val;
}
sub picture_format {
my($amount, $pic, $sep, $point) = @_;
$pic = reverse $pic;
$point = '.' unless defined $point;
$sep = ',' unless defined $sep;
my $len = $pic =~ /(#+)\Q$point/
? length($1)
: 0
;
$amount = sprintf('%.' . $len . 'f', $amount);
$amount =~ tr/0-9//cd;
my (@dig) = split m{}, $amount;
$pic =~ s/#/pop(@dig)/eg;
$pic =~ s/\Q$sep\E+(?!\d)//;
$pic =~ s/\d/*/g if @dig;
$amount = reverse $pic;
return $amount;
}
sub setlocale {
my ($locale, $currency, $opt) = @_;
#::logDebug("original locale " . (defined $locale ? $locale : 'undef') );
#::logDebug("default locale " . (defined $::Scratch->{mv_locale} ? $::Scratch->{mv_locale} : 'undef') );
if($opt->{get}) {
my $loc = $Vend::Cfg->{Locale_repository} or return;
my $currloc = $Vend::Cfg->{Locale} or return;
for(keys %$loc) {
return $_ if $loc->{$_} eq $currloc;
}
return;
}
$locale = $::Scratch->{mv_locale} unless defined $locale;
#::logDebug("locale is now " . (defined $locale ? $locale : 'undef') );
if ( $locale and not defined $Vend::Cfg->{Locale_repository}{$locale}) {
::logError( "attempt to set non-existant locale '%s'" , $locale );
return '';
}
if ( $currency and not defined $Vend::Cfg->{Locale_repository}{$currency}) {
::logError("attempt to set non-existant currency '%s'" , $currency);
return '';
}
if($locale) {
my $loc = $Vend::Cfg->{Locale} = $Vend::Cfg->{Locale_repository}{$locale};
for(@Vend::Config::Locale_directives_scalar) {
$Vend::Cfg->{$_} = $loc->{$_}
if defined $loc->{$_};
}
for(@Vend::Config::Locale_directives_ary) {
@{$Vend::Cfg->{$_}} = split (/\s+/, $loc->{$_})
if $loc->{$_};
}
for(@Vend::Config::Locale_directives_code) {
next unless $loc->{$_->[0]};
my ($routine, $args) = @{$_}[1,2];
if($args) {
$routine->(@$args);
}
else {
$routine->();
}
}
no strict 'refs';
for(qw/LC_COLLATE LC_CTYPE LC_TIME/) {
next unless $loc->{$_};
POSIX::setlocale(&{"POSIX::$_"}, $loc->{$_});
}
}
if ($currency) {
my $curr = $Vend::Cfg->{Currency_repository}{$currency};
for(@Vend::Config::Locale_directives_currency) {
$Vend::Cfg->{$_} = $curr->{$_}
if defined $curr->{$_};
}
for(@Vend::Config::Locale_keys_currency) {
$Vend::Cfg->{Locale}{$_} = $curr->{$_}
if defined $curr->{$_};
}
}
if(my $ref = $Vend::Cfg->{CodeDef}{LocaleChange}) {
$ref = $ref->{Routine};
if($ref->{all}) {
$ref->{all}->($locale, $opt);
}
if($ref->{lc $locale}) {
$ref->{lc $locale}->($locale, $opt);
}
}
if($opt->{persist}) {
$::Scratch->{mv_locale} = $locale if $locale;
delete $::Scratch->{mv_currency_tmp};
delete $::Scratch->{mv_currency};
$::Scratch->{mv_currency} = $currency if $currency;
}
elsif($currency) {
Vend::Interpolate::set_tmp('mv_currency_tmp')
unless defined $::Scratch->{mv_currency_tmp};
$::Scratch->{mv_currency_tmp} = $currency;
}
else {
delete $::Scratch->{mv_currency_tmp};
delete $::Scratch->{mv_currency};
}
return '';
}
sub currency {
my($amount, $noformat, $convert, $opt) = @_;
$opt = {} unless $opt;
$convert ||= $opt->{convert};
my $pd = $Vend::Cfg->{PriceDivide};
if($opt->{locale}) {
$convert = 1 unless length($convert);
$pd = $Vend::Cfg->{Locale_repository}{$opt->{locale}}{PriceDivide};
}
if($pd and $convert) {
$amount = $amount / $pd;
}
my $hash;
if(
$noformat =~ /\w+=\w\w/
and
ref($hash = get_option_hash($noformat)) eq 'HASH'
)
{
$opt->{display} ||= $hash->{display};
$noformat = $opt->{noformat} = $hash->{noformat};
}
return $amount if $noformat;
my $sep;
my $dec;
my $fmt;
my $precede = '';
my $succede = '';
my $loc = $opt->{locale}
|| $::Scratch->{mv_currency_tmp}
|| $::Scratch->{mv_currency}
|| $Vend::Cfg->{Locale};
if(ref($loc)) {
## Do nothing, is a hash reference
}
elsif($loc) {
$loc = $Vend::Cfg->{Locale_repository}{$loc};
}
if (! $loc) {
$fmt = "%.2f";
}
else {
$sep = $loc->{mon_thousands_sep} || $loc->{thousands_sep} || ',';
$dec = $loc->{mon_decimal_point} || $loc->{decimal_point} || '.';
return picture_format($amount, $loc->{price_picture}, $sep, $dec)
if defined $loc->{price_picture};
if (defined $loc->{frac_digits}) {
$fmt = "%." . $loc->{frac_digits} . "f";
} else {
$fmt = "%.2f";
}
my $cs;
my $display = lc($opt->{display}) || 'symbol';
my $sep_by_space = $loc->{p_sep_by_space};
my $cs_precedes = $loc->{p_cs_precedes};
if( $loc->{int_currency_symbol} && $display eq 'text' ) {
$cs = $loc->{int_currency_symbol};
$cs_precedes = 1;
if (length($cs) > 3 || $cs =~ /\W$/) {
$sep_by_space = 0;
}
else {
$sep_by_space = 1;
}
}
elsif ( $display eq 'none' ) {
$cs = '';
}
elsif ( $display eq 'symbol' ) {
$cs = $loc->{currency_symbol} || '';
}
if($cs) {
if ($cs_precedes) {
$precede = $cs;
$precede = "$precede " if $sep_by_space;
}
else {
$succede = $cs;
$succede = " $succede" if $sep_by_space;
}
}
}
$amount = safe_sprintf($fmt, $amount);
$amount =~ s/\./$dec/ if defined $dec;
$amount = commify($amount, $sep || undef)
if $Vend::Cfg->{PriceCommas};
return "$precede$amount$succede";
}
## random_string
# leaving out 0, O and 1, l
my $random_chars = "ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789";
# Return a string of random characters.
sub random_string {
my ($len) = @_;
$len = 8 unless $len;
my ($r, $i);
$r = '';
for ($i = 0; $i < $len; ++$i) {
$r .= substr($random_chars, int(rand(length($random_chars))), 1);
}
$r;
}
# To generate a unique key for caching
# Not very good without MD5
#
my $Md;
my $Keysub;
eval {require Digest::MD5 };
if(! $@) {
$Md = new Digest::MD5;
$Keysub = sub {
@_ = time() unless @_;
$Md->reset();
if($Global::UTF8) {
$Md->add(map encode_utf8($_), @_);
}
else {
$Md->add(@_);
}
$Md->hexdigest();
};
}
else {
$Keysub = sub {
my $out = '';
@_ = time() unless @_;
for(@_) {
$out .= unpack "%32c*", $_;
$out .= unpack "%32c*", substr($_,5);
$out .= unpack "%32c*", substr($_,-1,5);
}
$out;
};
}
sub generate_key { &$Keysub(@_) }
sub hexify {
my $string = shift;
$string =~ s/(\W)/sprintf '%%%02x', ord($1)/ge;
return $string;
}
sub unhexify {
my $s = shift;
$s =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr(hex($1))/ge;
return $s;
}
*unescape_chars = \&unhexify;
sub unescape_full {
my $url = shift;
$url =~ tr/+/ /;
$url =~ s/<!--.*?-->//sg;
return unhexify($url);
}
## UNEVAL
# Returns a string representation of an anonymous array, hash, or scaler
# that can be eval'ed to produce the same value.
# uneval([1, 2, 3, [4, 5]]) -> '[1,2,3,[4,5,],]'
# Uses either Storable::freeze or Data::Dumper::DumperX or uneval
# in
sub uneval_it {
my($o) = @_; # recursive
my($r, $s, $i, $key, $value);
local($^W) = 0;
$r = ref $o;
if (!$r) {
$o =~ s/([\\"\$@])/\\$1/g;
$s = '"' . $o . '"';
} elsif ($r eq 'ARRAY') {
$s = "[";
foreach $i (0 .. $#$o) {
$s .= uneval_it($o->[$i]) . ",";
}
$s .= "]";
} elsif ($r eq 'HASH') {
$s = "{";
while (($key, $value) = each %$o) {
$s .= "'$key' => " . uneval_it($value) . ",";
}
$s .= "}";
} else {
$s = "'something else'";
}
$s;
}
use subs 'uneval_fast';
sub uneval_it_file {
my ($ref, $fn) = @_;
open(UNEV, ">$fn")
or die "Can't create $fn: $!\n";
print UNEV uneval_fast($ref);
close UNEV;
}
sub eval_it_file {
my ($fn) = @_;
local($/) = undef;
open(UNEV, "< $fn") or return undef;
my $ref = evalr(<UNEV>);
close UNEV;
return $ref;
}
# See if we have Storable and the user has OKed its use
# If so, session storage/write will be about 5x faster
eval {
die unless $ENV{MINIVEND_STORABLE};
require Storable;
import Storable 'freeze';
if ($ENV{MINIVEND_STORABLE_CODE}) {
# allow code references to be stored to the session
$Storable::Deparse = 1;
$Storable::Eval = 1;
}
$Fast_uneval = \&Storable::freeze;
$Fast_uneval_file = \&Storable::store;
$Eval_routine = \&Storable::thaw;
$Eval_routine_file = \&Storable::retrieve;
};
# See if Data::Dumper is installed with XSUB
# If it is, session writes will be about 25-30% faster
eval {
die if $ENV{MINIVEND_NO_DUMPER};
require Data::Dumper;
import Data::Dumper 'DumperX';
$Data::Dumper::Indent = 1;
$Data::Dumper::Terse = 1;
$Data::Dumper::Deepcopy = 1;
if(defined $Fast_uneval) {
$Pretty_uneval = \&Data::Dumper::Dumper;
}
else {
$Pretty_uneval = \&Data::Dumper::DumperX;
$Fast_uneval = \&Data::Dumper::DumperX
}
};
*uneval_fast = defined $Fast_uneval ? $Fast_uneval : \&uneval_it;
*evalr = defined $Eval_routine ? $Eval_routine : sub { eval shift };
*eval_file = defined $Eval_routine_file ? $Eval_routine_file : \&eval_it_file;
*uneval_file = defined $Fast_uneval_file ? $Fast_uneval_file : \&uneval_it_file;
*uneval = defined $Pretty_uneval ? $Pretty_uneval : \&uneval_it;
# Log data fields to a data file.
sub logData {
my($file,@msg) = @_;
my $prefix = '';
$file = ">>$file" unless $file =~ /^[|>]/;
my $msg = tabbed @msg;
eval {
unless($file =~ s/^[|]\s*//) {
# We have checked for beginning > or | previously
open(MVLOGDATA, $file) or die "open\n";
lockfile(\*MVLOGDATA, 1, 1) or die "lock\n";
seek(MVLOGDATA, 0, 2) or die "seek\n";
print(MVLOGDATA "$msg\n") or die "write to\n";
unlockfile(\*MVLOGDATA) or die "unlock\n";
}
else {
my (@args) = grep /\S/, Text::ParseWords::shellwords($file);
open(MVLOGDATA, "|-") || exec @args;
print(MVLOGDATA "$msg\n") or die "pipe to\n";
}
close(MVLOGDATA) or die "close\n";
};
if ($@) {
if($::Limit->{logdata_error_length} > 0) {
$msg = substr($msg, 0, $::Limit->{logdata_error_length});
}
logError ("Could not %s log file '%s': %s\nto log this data:\n%s",
$@,
$file,
$!,
$msg,
);
return 0;
}
1;
}
sub quoted_comma_string {
my ($text) = @_;
my (@fields);
push(@fields, $+) while $text =~ m{
"([^\"\\]*(?:\\.[^\"\\]*)*)"[\s,]? ## std quoted string, w/possible space-comma
| ([^\s,]+)[\s,]? ## anything else, w/possible space-comma
| [,\s]+ ## any comma or whitespace
}gx;
@fields;
}
# Modified from old, old module called Ref.pm
sub copyref {
my($x,$r) = @_;
my($z, $y);
my $rt = ref $x;
if ($rt =~ /SCALAR/) {
# Would \$$x work?
$z = $$x;
return \$z;
} elsif ($rt =~ /HASH/) {
$r = {} unless defined $r;
for $y (sort keys %$x) {
$r->{$y} = ©ref($x->{$y}, $r->{$y});
}
return $r;
} elsif ($rt =~ /ARRAY/) {
$r = [] unless defined $r;
for ($y = 0; $y <= $#{$x}; $y++) {
$r->[$y] = ©ref($x->[$y]);
}
return $r;
} elsif ($rt =~ /REF/) {
$z = ©ref($x);
return \$z;
} elsif (! $rt) {
return $x;
} else {
die "do not know how to copy $x";
}
}
sub check_gate {
my($f, $gatedir) = @_;
my $gate;
if ($gate = readfile("$gatedir/.access_gate") ) {
$f =~ s:.*/::;
$gate = Vend::Interpolate::interpolate_html($gate);
if($gate =~ m!^$f(?:\.html?)?[ \t]*:!m ) {
$gate =~ s!.*(\n|^)$f(?:\.html?)?[ \t]*:!!s;
$gate =~ s/\n[\S].*//s;
$gate =~ s/^\s+//;
}
elsif($gate =~ m{^\*(?:\.html?)?[: \t]+(.*)}m) {
$gate = $1;
}
else {
undef $gate;
}
}
return $gate;
}
sub string_to_ref {
my ($string) = @_;
if($MVSAFE::Safe) {
return eval $string;
}
my $safe = $Vend::Interpolate::safe_safe || new Vend::Safe;
return $safe->reval($string);
}
sub is_hash {
return ref($_[0]) eq 'HASH';
}
sub dotted_hash {
my($hash, $key, $value, $delete_empty) = @_;
$hash = get_option_hash($hash) unless is_hash($hash);
unless (is_hash($hash)) {
return undef unless defined $value;
$hash = {};
}
my @keys = split /[\.:]+/, $key;
my $final;
my $ref;
if(! defined $value) {
# Retrieving
$ref = $hash->{shift @keys};
for(@keys) {
return undef unless is_hash($ref);
$ref = $ref->{$_};
}
return $ref;
}
# Storing
$final = pop @keys;
$ref = $hash;
for(@keys) {
$ref->{$_} = {} unless is_hash($ref->{$_});
$ref = $ref->{$_};
}
if($delete_empty and ! length($value)) {
delete $ref->{$final};
}
else {
$ref->{$final} = $value;
}
$hash = uneval_it($hash);
return $hash;
}
sub get_option_hash {
my $string = shift;
my $merge = shift;
if (ref $string eq 'HASH') {
my $ref = { %$string };
return $ref unless ref $merge;
for(keys %{$merge}) {
$ref->{$_} = $merge->{$_}
unless defined $ref->{$_};
}
return $ref;
}
return {} unless $string and $string =~ /\S/;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
if($string =~ /^{/ and $string =~ /}/) {
return string_to_ref($string);
}
my @opts;
unless ($string =~ /,/) {
@opts = grep $_ ne "=", Text::ParseWords::shellwords($string);
for(@opts) {
s/^(\w[-\w]*\w)=(["'])(.*)\2$/$1$3/;
}
}
else {
@opts = split /\s*,\s*/, $string;
}
my %hash;
for(@opts) {
my ($k, $v) = split /[\s=]+/, $_, 2;
$k =~ s/-/_/g;
$hash{$k} = $v;
}
if($merge) {
return \%hash unless ref $merge;
for(keys %$merge) {
$hash{$_} = $merge->{$_}
unless defined $hash{$_};
}
}
return \%hash;
}
sub word2ary {
my $val = shift;
return $val if ref($val) eq 'ARRAY';
my @ary = grep /\w/, split /[\s,\0]+/, $val;
return \@ary;
}
sub ary2word {
my $val = shift;
return $val if ref($val) ne 'ARRAY';
@$val = grep /\w/, @$val;
return join " ", @$val;
}
## Takes an IC scalar form value (parm=val\nparm2=val) and translates it
## to a reference
sub scalar_to_hash {
my $val = shift;
$val =~ s/^\s+//mg;
$val =~ s/\s+$//mg;
my @args;
@args = split /\n+/, $val;
my $ref = {};
for(@args) {
m!([^=]+)=(.*)!
and $ref->{$1} = $2;
}
return $ref;
}
## Takes a form reference (i.e. from \%CGI::values) and makes into a
## scalar value value (i.e. parm=val\nparm2=val). Also translates it
## via HTML entities -- it is designed to make it into a hidden
## form value
sub hash_to_scalar {
my $ref = shift
or return '';
unless (ref($ref) eq 'HASH') {
die __PACKAGE__ . " hash_to_scalar routine got bad reference.\n";
}
my @parms;
while( my($k, $v) = each %$ref ) {
$v =~ s/\r?\n/\r/g;
push @parms, HTML::Entities::encode("$k=$v");
}
return join "\n", @parms;
}
## This simply returns a hash of words, which may be quoted shellwords
## Replaces most of parse_hash in Vend::Config
sub hash_string {