Repository navigation
Expand file tree
/
Copy pathdefault.include.pl
More file actions
executable file
·8494 lines (8449 loc) · 336 KB
/
Copy pathdefault.include.pl
File metadata and controls
executable file
·8494 lines (8449 loc) · 336 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
#!/usr/bin/perl
################################################################################
#
# global libs for AGI, perl scripts and CGI extra libs for multilevel services
#
################################################################################
$|=1;$!=1; # disable buffer
use File::Copy;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use DBI;
use LWP 5.69;
use HTML::Template::Expr;
use HTTP::Request::Common qw( POST );
use Logger::Syslog;
# Added for Twilio API Integration By Zenofon SMS Team
use WWW::Twilio::API;
$app_root = "/usr/local/multilevel/";
$template_folder = "$app_root/www/design/";
$email_template_folder = "$app_root/www/design/";
%template_buffer = ();
$database = null;
$conection = null;
$database_connected = 0;
$database_last_error = "";
$database_dsn = "dbi:mysql:multilevel:127.0.0.1:3306";
$database_user = "multilevel";
$database_password = "multilevel";
$im_identify = "/usr/bin/identify";
$im_convert = "/usr/bin/convert";
$im_composite = "/usr/bin/composite";
#
# development overwrite
#if ( ($ENV{REMOTE_ADDR} eq "127.0.0.1") && ($ENV{SERVER_ADDR} eq "127.0.0.1") ) {
# $database_dsn = "dbi:mysql:multilevel:127.0.0.1:3306";
# $database_user = "root";
# $database_password = "root";
#}
#
# in future, make this thing permanent in modperl.
# TODO: do we really need that? are we using that?
%global_cache = ();
%cache_request = ();
%cache_session = ();
%cache_user = ();
%cache_global = ();
return 1;
#=======================================================
#=======================================================
# move to default include
#=======================================================
sub get_carrier_by_number(){
local($number) = @_;
local($tmp,$tmp1,$tmp2,%hash,%out,%data_out,%data_in,$sql);
#
# create default values
$out{found} = 0;
$out{carrier_id} = 0;
$out{carrier_name} = "";
$out{carrier_is_mobile} = 0;
$out{flag_premium_signin} = 0;
#
# === hack start ===
#warning("MMM ($out{flag_premium_signin}) ($number)");
#if (substr($number,0,7) eq "1212000"){$out{flag_premium_signin} = 1;}
#if (substr($number,0,6) eq "212000"){$out{flag_premium_signin} = 1;}
#warning("MMM ($out{flag_premium_signin}) ($number)");
# === hack end ===
#
#
# return from check or delete cache
$sql = "select 1,1,carrier.id,carrier.name,carrier.flag_is_mobile,carrier.flag_premium_signin from carrier,carrier_cache_data247 where carrier.id=carrier_cache_data247.carrier_id and carrier_cache_data247.number='$number' ";
%data_in = database_select_as_hash($sql,"flag,id,name,flag_is_mobile,flag_premium_signin");
if ($data_in{1}{flag} eq 1) {
$out{found} = 1;
$out{carrier_id} = $data_in{1}{id};
$out{carrier_name} = $data_in{1}{name};
$out{carrier_is_mobile} = $data_in{1}{flag_is_mobile};
$out{flag_premium_signin} = $data_in{1}{flag_premium_signin};
#
# === hack start ===
#if (substr($number,0,7) eq "1212000"){$out{flag_premium_signin} = 1;}
#if (substr($number,0,6) eq "212000"){$out{flag_premium_signin} = 1;}
# === hack end ===
#
return %out;
}
#
# query data247 (abort if fail)
%data_out = &data247_query_number($number);
if ($data_out{ok} ne 1) {return %out}
if ($data_out{carrier_id} eq "") {return %out}
$data_out{carrier_is_mobile} = ($data_out{wless} eq "y") ? 1 : 0;
#
# check provider table
$sql = "select 1,1,id,name,flag_is_mobile,flag_premium_signin from carrier where data247_id='$data_out{carrier_id}'";
%data_in = database_select_as_hash($sql,"flag,id,name,flag_is_mobile,flag_premium_signin");
if ($data_in{1}{flag} ne 1) {
#
# new provider, lets add
$sql = "insert into carrier (data247_id,name,flag_is_mobile) values ('$data_out{carrier_id}','$data_out{carrier_name}','$data_out{carrier_is_mobile}') ";
&database_do($sql);
$sql = "select 1,1,id,name,flag_is_mobile,flag_premium_signin from carrier where data247_id='$data_out{carrier_id}'";
%data_in = database_select_as_hash($sql,"flag,id,name,flag_is_mobile,flag_premium_signin");
}
#
# mix data
if ( ($data_in{1}{flag} eq 1) && ($data_in{1}{id} ne "") ) {
$out{found} = 1;
$out{carrier_id} = $data_in{1}{id};
$out{carrier_name} = $data_in{1}{name};
$out{carrier_is_mobile} = $data_in{1}{flag_is_mobile};
$out{flag_premium_signin} = $data_in{1}{flag_premium_signin};
#
# update cache table
&database_do("insert into carrier_cache_data247 (number,carrier_id,last_change) values ('$number','$out{carrier_id}',now() ) ");
&database_do("update carrier_cache_data247 set carrier_id='$out{carrier_id}',last_change=now() where number='$number' ");
}
#
# return
#
# === hack start ===
#if (substr($number,0,7) eq "1212000"){$out{flag_premium_signin} = 1;}
#if (substr($number,0,6) eq "212000"){$out{flag_premium_signin} = 1;}
# === hack end ===
#
return %out;
}
sub data247_query_number(){
my($number) = @_;
my $data247_user = "neyfrota";
my $data247_password = "dt1014gb";
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Headers;
use XML::Simple;
use Data::Dumper;
my %data = ();
$data{ok} = 0;
$data{error_network} = 0;
$data{error_data} = 0;
$data{status} = "";
my $browser = LWP::UserAgent->new;
my $url = "https://api.data24-7.com/carrier.php?username=".$data247_user."&password=".$data247_password."&p1=".$number;
#my $url = "http://127.0.0.1:82/data247.cgi?username=".$data247_user."&password=".$data247_password."&p1=".$number;
my $response = $browser->get($url);
if (!$response->is_error) {
my $simplexml = XML::Simple->new();
my $xmldata = $simplexml->XMLin($response->content);
$data{number} = $xmldata->{results}->{result}->{number} || "";
$data{carrier_id} = $xmldata->{results}->{result}->{carrier_id} || "";
$data{carrier_name} = $xmldata->{results}->{result}->{carrier_name} || "";
$data{wless} = $xmldata->{results}->{result}->{wless} || "";
$data{status} = $xmldata->{results}->{result}->{status} || "";
$data{sms_address} = $xmldata->{results}->{result}->{sms_address} || "";
$data{mms_address} = $xmldata->{results}->{result}->{mms_address} || "";
$data{error_data} = ($xmldata->{results}->{result}->{item} eq 1) ? 0 : 1;
$data{ok} = ( ($data{error_data} eq 0) && ($data{status} eq "OK") ) ? 1 : 0;
#
#$data{error_data} = 1;
#foreach my $item_on_loop (@{$xmldata->{results}->{result}}){
# $data{number} = $item_on_loop->{number} || "";
# $data{carrier_id} = $item_on_loop->{carrier_id} || "";
# $data{carrier_name} = $item_on_loop->{carrier_name} || "";
# $data{wless} = $item_on_loop->{wless} || "";
# $data{status} = $item_on_loop->{status} || "";
# $data{sms_address} = $item_on_loop->{sms_address} || "";
# $data{mms_address} = $item_on_loop->{mms_address} || "";
# $data{error_data} = 0;
# last;
#}
#$data{ok} = ($data{status} eq "OK") ? 1 : 0;
#
} else {
$data{error_network} = 1;
$data{status} = "Network error:".$response->message;
}
my $buf = "";
$buf .= "QUERY_TIME=".time."\n";
$buf .= "QUERY_NUMBER=".$number."\n";
foreach (sort keys %data) {$buf .= "DATA $_=$data{$_}\n"}
$buf .= "\n\n";
open(OUTOUT,">>/tmp/data247_query_number.log");
print OUTOUT $buf;
close(OUTOUT);
return %data;
}
#------------------------
# some lost things
#------------------------
sub sql_to_hash_by_page(){
#
# basic, query sql database by page and put in hash
# $data{DATA} is the same format as template loops. just drop DATA in the loop you want
# remeber you NEED add " LIMIT #LIMIT1 , #LIMIT2" in your DATA query in order to limit page itens.
#
# her is a example how to query and add on template hash.
#
# ==== CGI START ====
# %template_data = ();
# %users_list = &sql_to_hash_by_page((
# 'sql_total'=>"SELECT count(*) FROM users ",
# 'sql_data'=>"SELECT id,name,phone FROM users ORDER BY date desc LIMIT #LIMIT1 , #LIMIT2 ",
# 'sql_data_names'=>"user_id,user_name,user_phone",
# 'page_now'=>$form{page_number},
# 'page_size'=>5
# ));
# if ($users_list{OK} eq 1){
# #
# # put DATA into users_list loop
# $template_data{users_list_found}= 1;
# %{$template_data{users_list}} = %{$users_list{DATA}};
# #
# # create loop with page info
# $template_data{users_list_page_min} = $users_list{page_min};
# $template_data{users_list_page_max} = $users_list{page_max};
# $template_data{users_list_page_now} = $users_list{page_now};
# $template_data{users_list_page_previous} = ($template_data{page_now} > $template_data{page_min}) ? $template_data{page_now}-1 : "";
# $template_data{users_list_page_next} = ($template_data{page_now} < $template_data{page_max}) ? $template_data{page_now}+1 : "";
# foreach $p ($users_list{page_min}..$users_list{page_max}) {
# $template_data{users_list_pages}{$p}{page} = $p;
# $template_data{users_list_pages}{$p}{selected} = ($p eq $t{thread_page}) ? 1 : 0;
# }
# }
# &template_print("template.html",%template_data);
# ==== CGI STOP ====
#
# ==== TEMPLATE.HTML START ====
# <table>
# <TMPL_LOOP NAME="users_list">
# <tr>
# <td>%user_id%</td>
# <td>%user_name%</td>
# <td>%user_phone%</td>
# </tr>
# </TMPL_LOOP>
# </table>
# <br>
# Page %users_list_page_now% of %users_list_page_max%<br>
# Select page:
# <TMPL_LOOP NAME="users_list_pages"><a href=?page_number=%page%>%page%</a>,</TMPL_LOOP>
# ==== TEMPLATE.HTML STOP ====
#
local(%data) = @_;
local(%hash,%hash1,$hash2,$tmp,$tmp1,$tmp2,@array,@array1,@array2);
#
# pega page limits
%hash = &database_select($data{sql_total});
$data{count} = ($hash{OK} eq 1) ? &clean_int($hash{DATA}{0}{0}) : 0;
$data{count} = ($data{count} eq "") ? 0 : $data{count};
$data{page_size} = &clean_int($data{page_size});
$data{page_size} = ($data{page_size} eq "") ? $workgroup_config{page_size} : $data{page_size};
$data{page_size} = ($data{page_size} > 1024) ? 1024 : $data{page_size};
$data{page_size} = ($data{page_size} < 1 ) ? 1 : $data{page_size};
$data{page_min} = 1;
$data{page_max} = int(($data{count}-1)/$data{page_size})+1;
$data{page_max} = ($data{page_max}<$data{page_min}) ? $data{page_min} : $data{page_max};
$data{page_now} = &clean_int($data{page_now});
$data{page_now} = ($data{page_now}<$data{page_min}) ? $data{page_min} : $data{page_now};
$data{page_now} = ($data{page_now}>$data{page_max}) ? $data{page_max} : $data{page_now};
$data{sql_limit_1} = ($data{page_now}-1)*$data{page_size};
$data{sql_limit_2} = $data{page_size};
#
# pega ids
if ($data{count} > 0){
$data{sql_data_run} = $data{sql_data};
$tmp2=$data{sql_limit_1}; $tmp1="#LIMIT1"; $data{sql_data_run} =~ s/$tmp1/$tmp2/eg;
$tmp2=$data{sql_limit_2}; $tmp1="#LIMIT2"; $data{sql_data_run} =~ s/$tmp1/$tmp2/eg;
%hash = &database_select($data{sql_data_run},$data{sql_data_names});
if ($hash{OK} eq 1) {
%{$data{DATA}} = %{$hash{DATA}};
$data{ROWS} = $hash{ROWS};
$data{COLS} = $hash{COLS};
$data{OK} = 1;
}
}
#
# return
return %data;
}
sub ip_flood_counter(){
local ($section) = @_;
local ($ip) = $ENV{REMOTE_ADDR};
local ($buf,$out,$tmp,$tmp1,$tmp2,%hash,$counter_1,$counter_2,$timestamp);
#
# remember in runtime query
if ($app{ip_flood_counter_ip} eq $ip){
return ($app{ip_flood_counter_1},$app{ip_flood_counter_2})
}
#
# lets query and count
$counter_1 = 0;
$counter_2 = 0;
%hash = &database_select_as_hash("SELECT 1,1,counter_1,counter_2,unix_timestamp(timestamp) FROM security_ip_flood where ip='$ip'","flag,counter_1,counter_2,timestamp");
if ($hash{1}{flag} eq 1) {
$counter_1 = ($hash{1}{counter_1} ne "") ? $hash{1}{counter_1}: 1;
$counter_2 = ($hash{1}{counter_2} ne "") ? $hash{1}{counter_2}: 1;
$timestamp = ($hash{1}{timestamp} ne "") ? $hash{1}{timestamp}: time;
if ( (time-$timestamp)<(60) ) {$counter_1++;} else {$counter_1 = 0;}
if ( (time-$timestamp)<(60*10) ) {$counter_2++;} else {$counter_2 = 0;}
&database_do("
update security_ip_flood set
counter_1 = '$counter_1',
counter_2 = '$counter_2',
timestamp = now()
where ip='$ip'
");
} else {
&database_do("
insert into security_ip_flood
(ip, timestamp, counter_1, counter_2 ) values
('$ip', now(), '1', '1' )
");
$counter_1 = 1;
$counter_2 = 1;
}
$app{ip_flood_counter_ip} = $ip;
$app{ip_flood_counter_1} = $counter_1;
$app{ip_flood_counter_2} = $counter_2;
return($counter_1,$counter_2);
}
sub ip_flood_surge_protection(){
#%now_cookie = &cookie_read();
#if ($now_cookie{is_root} ne "Yes") {
# %t = ();
# $t{dic}{title} = "Under maintenance";
# $t{dic}{content} = qq[
# <div class=clear style="padding:50px;>
# <img src=/design/icons/error.png hspace=0 vspace=0 border=0 align=left style="margin-right:5px;">
# <h2>Under maintenance</h2><br>
# The website is temporally down due to undergoing maintenance work. We apologize for the inconvenience this may cause you and encourage you to check back with us again shortly. Thank you.
# </div>
# ];
# template_print("template.html",%t);
# exit;
#}
if ($ENV{REMOTE_ADDR} eq "127.0.0.1") {return}
local ($section) = @_;
local ($buf,$out,$tmp,$tmp1,$tmp2,%hash,$counter_1,$counter_2,$timestamp);
($counter_1,$counter_2) = &ip_flood_counter();
if ( ($counter_1 > 10) || ($counter_2 > 60) ) {
&action_history("ipflood",('value_new'=>$ENV{REMOTE_ADDR}, 'value_old'=>"$section" ));
$buf = "";
$buf .= "DATE $today{DATE_TO_PRINT} $today{TIME_TO_PRINT} \n";
$buf .= "DATE_ID = $today{DATE_ID}$today{TIME_ID} \n";
$buf .= "SECTION = $section \n";
$buf .= "COUNTERS = $ip - $counter_1 - $counter_2\n";
foreach(sort keys %form){$buf .= "FORM $_ = $form{$_}\n";}
foreach(sort keys %ENV){
if (index($_,"SSL_") eq 0) {next}
if (index($_,"SERVER_") eq 0) {next}
$buf .= "ENV $_ = $ENV{$_}\n";
}
open(LOG,">>/usr/local/multilevel/data/logs/ip_flood.log");
print LOG "\n\n$buf";
close(LOG);
print "Content-type: text/html\n";
print "Cache-Control: no-cache, must-revalidate\n";
print "status:503\n";
print "\n";
print qq[
<body bgcolor=#ffffff color=#000000 >
<font face=verdana,arial size=2>
<div style="padding:50px;">
<div class=alert_box style="width:600px;padding:0px;margin:0px;border:1px solid #f8d322;background-color:#fff18e;">
<div class=alert_box_inside style="padding:0px;border:0px;margin-top:4px;margin-left:7px;margin-right:5px;margin-bottom:7px;padding-left:22px;padding-top:0px;background-image:url(/design/icons/forbidden.png);background-repeat:no-repeat;background-position:0 3;">
<font size=3><b>Warning</b>:</font><br>
You triggered website surge protection by doing too many requests in a short time.<br>
Please make a short break, slow down and try again.<br>
</div>
</div>
</div>
];
exit;
#sleep(30);
#When you restart doing requests AFTER that, slow down or you might get locked out for a longer time!<br>
}
}
sub mobile_provider_send_sms(){
local($provider_id,$number,$message) = @_;
local ($email,%hash,$sql);
$provider_id = &clean_int($provider_id);
$sql = "SELECT 1,1,mobileProviderEmail FROM product_mobile_providers where mobileProviderID='$provider_id' ";
%hash = database_select_as_hash($sql,"flag,domain");
if ($hash{1}{flag} eq 1) {
$email = &clean_int(&format_E164_number($number,"USA"))."\@".$hash{1}{domain};
&send_email($email,$email,"",$message);
return 1;
} else {
return 0;
}
}
sub dial_and_play_code(){
local($number,$code,$service_id) = @_;
local($rate_id,%rate_data,$callback_queue_folder,$asterisk_string,$callback_file,$callback_file_buf,%my_timestamp,$timestamp_future,$cmd,$tmp);
#
# find rate table to use
$rate_id = &data_get("adm_data","rate","play_code");
if ($rate_id eq "") {return 0;} # no rate, no dial
#
# get rate for number and dialstring
%rate_data = &multilevel_rate_table_get($number,$rate_id);
if ($rate_data{ok_to_use} ne 1) {return 0;} # no rate, no dial
$asterisk_string = $rate_data{asterisk_string};
#
# prepare call file
$callback_queue_folder = "/var/spool/asterisk/outgoing/";
$callback_file = time.$number.".sendcode.call";
$callback_file_buf = "Channel: $asterisk_string\n";
$callback_file_buf .= "MaxRetries: 2\n";
$callback_file_buf .= "RetryTime: 5\n";
$callback_file_buf .= "WaitTime: 40\n";
$callback_file_buf .= "Application: AGI\n";
$callback_file_buf .= "CallerID: \"Zenofon\" <9172849450>\n";
#$callback_file_buf .= "Data: play_code.pl|code=$code|\n";
$callback_file_buf .= "Data: play_code.pl,code=$code\n";
$callback_file_buf .= "AlwaysDelete:Yes\n";
$callback_file_buf .= "Archive:No\n";
#
# write call file with 5 seconds in future (not state of art)
%my_timestamp = &get_today(time+5);
$timestamp_future = substr("0000".$my_timestamp{YEAR},-4,4) . substr("00".$my_timestamp{MONTH},-2,2) . substr("00".$my_timestamp{DAY},-2,2) . substr("00".$my_timestamp{HOUR},-2,2) . substr("00".$my_timestamp{MINUTE},-2,2) .".".substr("00".$my_timestamp{SECOND},-2,2);
open (OUT,">/tmp/$callback_file");
print OUT $callback_file_buf;
close (OUT);
$cmd = "chmod 666 /tmp/$callback_file; ";
$cmd .= "touch -t $timestamp_future /tmp/$callback_file; ";
$cmd .= "mv /tmp/$callback_file $callback_queue_folder; ";
$tmp = `$cmd`;
#
# ok!
return 1;
}
#------------------------
#
#------------------------
# log_debug api
#
# try to make a common place to store all debug logs.
# log debug on filesystem make life complex with multiple servers.
# This funcions try make life easy on this task
#
# right now:
# we create functions with basic to survive, so this is what we have:
# - log_debug_add($ref,%hash) Dump hash into text, add to log and link references. Return log_debug_id
# - log_debug_get($log_debug_id) Get debug text for this entry
# - reference is "TYPE_1=ID_1,...TYPE_n=ID_n" example $ref = "call_id=987981,credit_id=2,commission_id=3474";
# - reference type and id must be max 32 char length
# - hash is our classic data hash we use in many points at our api. Check multilevel_commission_new example
#
# in future:
# log_debug_add($ref,[$text,%hash]) accept text or hash, and do the magic to normalise to text
# log_debug_get([$ref,$log_debug_id]) can receive references also. More reference we give, more precise he can find correct log_debug_id
# log_debug_get_list([$ref,$log_debug_id]) Return list of log_debug_id for this references
#
#------------------------
sub log_debug_get(){
local($ref_or_debug_id) = @_;
local(%hash,$tmp,$tmp1,$tmp2,$sql,$out);
local($buf,$debug_id);
#
# check if we have a log_debug_id or ref and get log_debug_id if its a ref
if ($ref_or_debug_id eq "") {return ""}
if (&clean_int($ref_or_debug_id) eq $ref_or_debug_id) {
$debug_id = $ref_or_debug_id
} else {
$debug_id = &log_debug_search_log_debug_id_by_ref($ref_or_debug_id);
}
if ($debug_id eq "") {return ""}
#
# get and retun text
$sql = &database_scape_sql("select 1,1,text from log_debug where id='%d' ",$debug_id);
%hash = database_select_as_hash($sql,"flag,text");
return $hash{1}{text};
}
sub log_debug_add(){
local($ref,%data) = @_;
local(%hash,$tmp,$tmp1,$tmp2,$sql,$out,$type);
local($text,$debug_id);
#
#===================================
# Switch to text storage...
#===================================
# get type
$type = "UNKNOWN";
foreach $tmp (split(/\,/,$ref)){
$tmp = &trim($tmp);
($tmp1,$tmp2) = split(/\=/,$tmp);
$tmp1 = &trim($tmp1);
$tmp2 = &trim($tmp2);
if ($tmp1 eq "type") {$type = &clean_str("\U$tmp2","-_","MINIMAL"); last}
}
$text = &log_debug_convert_hash_to_text(%data);
open(LOG,">>$app_root/data/logs/log_debug_ad_$type");
print LOG "\n\n\n============================\ntime=".time."\n$text";
close(LOG);
return;
#
#===================================
# old database way...
#===================================
local($ref,%data) = @_;
local(%hash,$tmp,$tmp1,$tmp2,$sql,$out);
local($text,$debug_id);
#
# hack: right now, to make fast, we always assume we got a hash
#
# check if we have a hash or text. if hash, convert to text
$text = &log_debug_convert_hash_to_text(%data);
#
# add text to database and get id
$sql = &database_scape_sql("insert into log_debug (date,text) values (now(),\"%s\")",$text);
$debug_id = &database_do_insert($sql);
if ($debug_id eq "") {return ""}
#
# add each reference to ref table
foreach $tmp (split(/\,/,$ref)){
$tmp = &trim($tmp);
($tmp1,$tmp2) = split(/\=/,$tmp);
$tmp1 = &trim($tmp1);
$tmp2 = &trim($tmp2);
$tmp1 = "\L$tmp1";
$sql = &database_scape_sql("insert into log_debug_reference (log_debug_id,reference_name,reference_value) values ('%d','%s','%s')",$debug_id,$tmp1,$tmp2);
&database_do($sql);
}
#
# return
return $debug_id;
}
sub log_debug_convert_hash_to_text(){
local(%hash) = @_;
local($buf);
$buf = "";
foreach (sort keys %hash) {
if ($_ eq "debug") {next}
if ($_ eq "cc_number") {$hash{$_} = substr($hash{$_},-4,4);}
$buf .= "$_ = $hash{$_}\n";
}
foreach (split(/\!/,$hash{debug})) {$buf .= "debug = $_\n";}
return $buf;
}
sub log_debug_convert_hash_to_array(){
local(%hash) = @_;
local(@buf);
$buf = "";
foreach (sort keys %hash) {if ($_ eq "debug") {next} @buf = (@buf,"$_ = $hash{$_}");}
foreach (split(/\!/,$hash{debug})) { @buf = (@buf,"debug = $_");}
return @buf;
}
sub log_debug_search_log_debug_id_by_ref(){
local($ref) = @_;
local(%hash,$sql,$tmp,$tmp1,$tmp2,$sql,$out);
local($ref_name,$ref_value);
($ref_name,$ref_value) = split(/\=/,$ref);
$sql = &database_scape_sql(
"
select 1,1,log_debug_id
from log_debug_reference
where reference_name='%s' and reference_value='%s'
order by id desc
limit 0,1
",
$ref_name,$ref_value
);
%hash = database_select_as_hash($sql,"flag,value");
if ($hash{1}{flag} eq 1) {
return $hash{1}{value};
}
return "";
}
#------------------------
#
#------------------------
# action_history
#
# better log_action subsystem
# create same work as log_debug (log_action_add, log_action_get)
# create tables in new format
# create script to migrate data fom old tables to new tables
# change action_history to get old format data, transform into new format and send to log_action_add
# change pages that use actions to use new database format
# slowly change all action_history calls into log_action_add (or not)
#
#------------------------
sub dddd_action_history_get_info(){
local($log_ids,$flags) = @_;
local(%out,$sql,$tmp,$tmp1,$tmp2,%hash,%logs,$icon,$title,$text,$user,%adm_users,$by,%extra);
$flags = "\L$flags";
$flags = "\Lno_user,no_date";
#
# prepara lista de logs a se verificar
$sql = "
SELECT
app_action_log.id,
unix_timestamp(app_action_log.date),
app_action_log.type,
app_action_log_type.group,
app_action_log_type.title,
app_action_log_type.description,
app_action_log.value_old,
app_action_log.value_new,
app_action_log.adm_user_id,
app_action_log.call_log_id,
app_action_log.service_id,
app_action_log.credit_id,
app_action_log.commission_id,
app_action_log.commission_invoice_id
FROM
app_action_log,
app_action_log_type
WHERE
app_action_log.id in ($log_ids) and
app_action_log.type=app_action_log_type.id
";
%logs = &database_select_as_hash($sql, "date,type,group,title,description,value_old,value_new,adm_user_id,call_debug_id,service_id,credit_id,commission_id,commission_invoice_id,coupon_stock_id,cupon_type_id");
%adm_users = &database_select_as_hash("select id,web_user,name from adm_users","web_user,name");
#
# pega lista de ids de tabelas de juda
foreach $log_id (keys %logs) {
if ($logs{$log_id}{credit_id} ne "") {$extra{credit}{ids} .= "$logs{$log_id}{credit_id},"}
}
if ($extra{credit}{ids} ne "") {
$extra{credit}{ids} = substr($extra{credit}{ids},0,-1);
%hash = &database_select_as_hash("select id,credit,text from credit where id in($extra{credit}{ids}) ","credit,text");
$extra{credit}{id} = {%hash};
}
#
# monta a saida
foreach $log_id (keys %logs) {
$user = (exists($adm_users{$logs{$log_id}{adm_user_id}})) ? "$adm_users{$logs{$log_id}{adm_user_id}}{web_user} ($adm_users{$logs{$log_id}{adm_user_id}}{name})" : "";
$by = ($user eq "") ? "" : "by $user";
$out{$log_id}{icon} = "application_go.png";
$out{$log_id}{title_full} = "$logs{$log_id}{group} : $logs{$log_id}{title}";
$out{$log_id}{title} = $logs{$log_id}{title};
$out{$log_id}{group} = $logs{$log_id}{group};
$out{$log_id}{text} = $logs{$log_id}{title};
if ($logs{$log_id}{description} ne ""){
$out{$log_id}{text} = $logs{$log_id}{description};
$tmp1="#1"; $tmp2=$logs{$log_id}{value_old}; $out{$log_id}{text} =~ s/$tmp1/$tmp2/eg;
$tmp1="#2"; $tmp2=$logs{$log_id}{value_new}; $out{$log_id}{text} =~ s/$tmp1/$tmp2/eg;
} else {
$tmp = "";
$tmp .= ($logs{$log_id}{value_old} ne "") ? "'$logs{$log_id}{value_old}' " : "";
$tmp .= ($logs{$log_id}{value_new} ne "") ? "'$logs{$log_id}{value_new}'" : "";
$out{$log_id}{text} .= ($tmp eq "") ? "" : " (data $tmp)";
}
$out{$log_id}{text} .= ( (index($flags,"no_user") eq -1) && ($user ne "") ) ? " by $user" : "";
$out{$log_id}{text} .= (index($flags,"no_date") eq -1) ? " at ".format_time_gap($logs{$log_id}{date}) : "";
$out{$log_id}{text_long} = $out{$log_id}{text};
$out{$log_id}{text_simple} = $out{$log_id}{text};
$out{$log_id}{date} = &format_time_gap($logs{$log_id}{date});
$out{$log_id}{date_timestamp} = $logs{$log_id}{date};
$out{$log_id}{user} = $user;
$out{$log_id}{detail} = 0;
if ($logs{$log_id}{type} eq "noc:service:commission") {
$out{$log_id}{detail} = 1;
$out{$log_id}{icon} = "calculator_add.png";
} elsif ( ($logs{$log_id}{type} eq "noc:service:email:change") || ($logs{$log_id}{type} eq "noc:service:name:change") ) {
$out{$log_id}{detail} = 1;
$out{$log_id}{icon} = "vcard_edit.png";
} elsif (index($logs{$log_id}{type},"suspicious:") eq 0) {
$out{$log_id}{detail} = 1;
$out{$log_id}{icon} = "exclamation.png";
} elsif ($logs{$log_id}{type} eq "noc:service:status:change") {
$out{$log_id}{detail} = 1;
$out{$log_id}{icon} = "award_star_gold_3.png";
} elsif ($logs{$log_id}{type} eq "noc:service:credit:free") {
$out{$log_id}{detail} = 1;
$out{$log_id}{icon} = "calculator_add.png";
} elsif ($logs{$log_id}{type} eq "noc:service:credit:cash") {
$out{$log_id}{detail} = 1;
$out{$log_id}{icon} = "calculator_add.png";
}
}
#
# return
return %out;
}
sub action_history_get_info(){
local($log_ids,$flags) = @_;
local(%out,$sql,$tmp,$tmp1,$tmp2,%hash,%logs,$icon,$title,$text,$user,%adm_users,$by,%extra);
$flags = "\L$flags";
$flags = "\Lno_user,no_date";
#
# prepara lista de logs a se verificar
$sql = "
SELECT
action_log.id,
unix_timestamp(action_log.date),
action_log.type,
action_log_type.group,
action_log_type.title,
action_log_type.description,
action_log.value_old,
action_log.value_new,
action_log.adm_user_id,
action_log.call_log_id,
action_log.service_id,
action_log.credit_id,
action_log.commission_id,
action_log.commission_invoice_id
FROM
action_log,
action_log_type
WHERE
action_log.id in ($log_ids) and
action_log.type=action_log_type.id
";
%logs = database_select_as_hash($sql, "date,type,group,title,description,value_old,value_new,adm_user_id,call_debug_id,service_id,credit_id,commission_id,commission_invoice_id,coupon_stock_id,cupon_type_id");
%adm_users = database_select_as_hash("select id,web_user,name from adm_users","web_user,name");
#
# pega lista de ids de tabelas de juda
foreach $log_id (keys %logs) {
if ($logs{$log_id}{credit_id} ne "") {$extra{credit}{ids} .= "$logs{$log_id}{credit_id},"}
}
if ($extra{credit}{ids} ne "") {
$extra{credit}{ids} = substr($extra{credit}{ids},0,-1);
%hash = database_select_as_hash("select id,credit,text from credit where id in($extra{credit}{ids}) ","credit,text");
$extra{credit}{id} = {%hash};
}
#
# monta a saida
foreach $log_id (keys %logs) {
$user = (exists($adm_users{$logs{$log_id}{adm_user_id}})) ? "$adm_users{$logs{$log_id}{adm_user_id}}{web_user} ($adm_users{$logs{$log_id}{adm_user_id}}{name})" : "";
$by = ($user eq "") ? "" : "by $user";
$out{$log_id}{icon} = "application_go.png";
$out{$log_id}{title_full} = "$logs{$log_id}{group} : $logs{$log_id}{title}";
$out{$log_id}{title} = $logs{$log_id}{title};
$out{$log_id}{group} = $logs{$log_id}{group};
$out{$log_id}{text} = $logs{$log_id}{title};
if ($logs{$log_id}{description} ne ""){
$out{$log_id}{text} = $logs{$log_id}{description};
$tmp1="#1"; $tmp2=$logs{$log_id}{value_old}; $out{$log_id}{text} =~ s/$tmp1/$tmp2/eg;
$tmp1="#2"; $tmp2=$logs{$log_id}{value_new}; $out{$log_id}{text} =~ s/$tmp1/$tmp2/eg;
} else {
$tmp = "";
$tmp .= ($logs{$log_id}{value_old} ne "") ? "'$logs{$log_id}{value_old}' " : "";
$tmp .= ($logs{$log_id}{value_new} ne "") ? "'$logs{$log_id}{value_new}'" : "";
$out{$log_id}{text} .= ($tmp eq "") ? "" : " (data $tmp)";
}
$out{$log_id}{text_long} = $out{$log_id}{text};
$out{$log_id}{text_simple} = $out{$log_id}{text};
$out{$log_id}{text_extra} = $out{$log_id}{text};
$out{$log_id}{date} = &format_time_gap($logs{$log_id}{date});
$out{$log_id}{date_timestamp} = $logs{$log_id}{date};
$out{$log_id}{user} = $user;
$out{$log_id}{by} = $by;
$out{$log_id}{detail} = 0;
}
#
# return
return %out;
}
sub action_history(){
local($id,%data) = @_;
local($out,$sql,$tmp,$names,$values);
$names = "";
$values= "";
foreach $tmp (("coupon_stock_id","coupon_type_id","value_new","value_old","service_id","signin_id","adm_user_id","credit_id","commission_id","call_log_id","commission_invoice_id")){
if (exists($data{$tmp})){
if ($data{$tmp} ne ""){
$names .= "$tmp, ";
$values .= "'$data{$tmp}', ";
}
}
}
# TODO: add adm user id automatic
if ($data{adm_user_id} eq "") {
if ($app{session_cookie_u} ne "") {
if ($app{users_table} eq "adm_users"){
$names .= "adm_user_id, ";
$values .= "'$app{session_cookie_u}', ";
}
}
}
$sql = "insert action_log (date, $names type) values (now(), $values '$id' ) ";
# adiciona
database_do($sql);
}
#------------------------
#
#------------------------
# data item
#------------------------
sub dataitem_initialize(){
my $d = shift @_;
local($sql,%hash,$i,$tmp,$tmp1,$tmp2);
#
# load slect with sql fields
foreach $i (keys %{$$d{config}{items}}) {
if ($$d{config}{items}{$i}{type} eq "SELECT") {
if ($$d{config}{items}{$i}{options_sql} ne "") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{options_sql},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
%{$$d{config}{items}{$i}{options}} = ();
foreach $tmp (sort{$a <=> $b} keys %{$hash{DATA}}){
$$d{config}{items}{$i}{options}{$tmp}{value} = $hash{DATA}{$tmp}{0};
$$d{config}{items}{$i}{options}{$tmp}{title} = $hash{DATA}{$tmp}{1};
}
}
}
if ($$d{config}{items}{$i}{type} eq "MULTISELECT") {
if ($$d{config}{items}{$i}{options_sql} ne "") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{options_sql},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
%{$$d{config}{items}{$i}{options}} = ();
foreach $tmp (sort{$a <=> $b} keys %{$hash{DATA}}){
$$d{config}{items}{$i}{options}{$tmp}{value} = $hash{DATA}{$tmp}{0};
$$d{config}{items}{$i}{options}{$tmp}{title} = $hash{DATA}{$tmp}{1};
}
}
}
}
return 1;
}
sub dataitem_add(){
my $d = shift @_;
local($sql,%hash,$i,$tmp,$tmp1,$tmp2);
#
# check basic
if ($$d{config}{sql_add} eq "") {$$d{status_message} = "No sql_add"; return 0;}
#
# check if key exists
if ($$d{config}{key_mode} eq "MANUAL") {
$tmp1 = $$d{config}{key_item};
$tmp2 = $$d{data}{items}{$tmp1}{value};
if ($tmp2 eq "") {$$d{status_message} = "No manual key to check duplicate"; return 0;}
$sql = &dataitem_tools_sql_parse($$d{config}{key_duplicate_sql},('KEY'=>$tmp2,'VALUE'=>$tmp2));
%hash = database_select($sql);
if ($hash{DATA}{0}{0} eq 1) {$$d{status_message} = "duplicate key"; return 0;}
}
#
# add data
if ($$d{config}{key_mode} eq "MANUAL") {
$tmp1 = $$d{config}{key_item};
$tmp2 = $$d{data}{items}{$tmp1}{value};
if ($tmp2 eq "") {$$d{status_message} = "No manual key to add"; return 0;}
$$d{data}{key} = $tmp2;
$sql = &dataitem_tools_sql_parse($$d{config}{sql_add},('KEY'=>$tmp2,'VALUE'=>$tmp2));
&database_do($sql);
} else {
$sql = &dataitem_tools_sql_parse($$d{config}{sql_add});
$$d{data}{key} = &database_do_insert($sql);
}
#
# update data
&dataitem_set($d);
#
# return
return 1;
}
sub dataitem_get(){
my $d = shift @_;
local($sql,%hash,$i,$tmp,$tmp1,$tmp2);
#
# check basic
if ($$d{config}{sql_key} eq "") {$$d{status_message} = "No sql_key"; return 0;}
if ($$d{data}{key} eq "") {$$d{status_message} = "No key"; return 0;}
#
# check if key exists
$sql = &dataitem_tools_sql_parse($$d{config}{sql_key},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
if ($hash{DATA}{0}{0} ne 1) {$$d{status_message} = "key not found"; return 0;}
#
# load all fields
foreach $i (keys %{$$d{config}{items}}) {
if ($$d{config}{items}{$i}{sql_get} eq "") {next}
if ($$d{config}{items}{$i}{type} eq "NUMBER") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_get},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
$$d{data}{items}{$i}{value} = $hash{DATA}{0}{0};
}
if ($$d{config}{items}{$i}{type} eq "STRING") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_get},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
$$d{data}{items}{$i}{value} = $hash{DATA}{0}{0};
}
if ($$d{config}{items}{$i}{type} eq "SELECT") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_get},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
$$d{data}{items}{$i}{value} = $hash{DATA}{0}{0};
}
if ($$d{config}{items}{$i}{type} eq "MULTISELECT") {
foreach $tmp (keys %{$$d{config}{items}{$i}{options}}) {
$tmp1 = $$d{config}{items}{$i}{options}{$tmp}{value};
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_get},('KEY'=>$$d{data}{key},'OPTIONID'=>$tmp1));
%hash = database_select($sql);
if ($hash{DATA}{0}{0} eq 1) {
$$d{data}{items}{$i}{value} .= ($$d{data}{items}{$i}{value} ne "") ? ",$tmp1" : $tmp1;
}
}
}
}
#
# return
$$d{status_message} = "OK";
return 1;
}
sub dataitem_set(){
my $d = shift @_;
local($sql,%hash,$value,$i,$tmp,$tmp1,$tmp2);
#
# check basic
if ($$d{config}{sql_key} eq "") {$$d{status_message} = "No sql_key"; return 0;}
if ($$d{data}{key} eq "") {$$d{status_message} = "No key"; return 0;}
#
# check if key exists
$sql = &dataitem_tools_sql_parse($$d{config}{sql_key},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
if ($hash{DATA}{0}{0} ne 1) {$$d{status_message} = "key not found"; return 0;}
#
# update all fields
foreach $i (keys %{$$d{config}{items}}) {
if ($$d{config}{items}{$i}{sql_set} eq "") {next}
if ($$d{config}{items}{$i}{sql_get} eq "") {next}
if ($$d{config}{items}{$i}{type} eq "STRING") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_set},('KEY'=>$$d{data}{key},'VALUE'=>$$d{data}{items}{$i}{value}));
&database_do($sql);
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_get},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
$$d{data}{items}{$i}{value} = $hash{DATA}{0}{0};
}
if ($$d{config}{items}{$i}{type} eq "NUMBER") {
if ($$d{data}{items}{$i}{value} eq "") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_set},('KEY'=>$$d{data}{key},'VALUE'=>"NULL"));
} else {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_set},('KEY'=>$$d{data}{key},'VALUE'=>$$d{data}{items}{$i}{value}));
}
&database_do($sql);
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_get},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
$$d{data}{items}{$i}{value} = $hash{DATA}{0}{0};
}
if ($$d{config}{items}{$i}{type} eq "SELECT") {
if ($$d{config}{items}{$i}{sql_before_all_sets} ne "") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_before_all_sets},('KEY'=>$$d{data}{key}));
&database_do($sql);
}
if ($$d{data}{items}{$i}{value} eq "") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_set},('KEY'=>$$d{data}{key},'VALUE'=>"NULL"));
} else {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_set},('KEY'=>$$d{data}{key},'VALUE'=>$$d{data}{items}{$i}{value}));
}
&database_do($sql);
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_get},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
$$d{data}{items}{$i}{value} = $hash{DATA}{0}{0};
}
if ($$d{config}{items}{$i}{type} eq "MULTISELECT") {
foreach $tmp (keys %{$$d{config}{items}{$i}{options}}) {
$tmp1 = $$d{config}{items}{$i}{options}{$tmp}{value};
if (index(",$$d{data}{items}{$i}{value},",",$tmp1,") ne -1) {
if ($$d{config}{items}{$i}{sql_set} ne "") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_set},('KEY'=>$$d{data}{key},'OPTIONID'=>$tmp1));
database_do($sql);
}
} else {
if ($$d{config}{items}{$i}{sql_unset} ne "") {
$sql = &dataitem_tools_sql_parse($$d{config}{items}{$i}{sql_unset},('KEY'=>$$d{data}{key},'OPTIONID'=>$tmp1));
database_do($sql);
}
}
}
}
}
#
# update dataitem
if ($$d{config}{sql_edit} ne "") {
$sql = &dataitem_tools_sql_parse($$d{config}{sql_edit},('KEY'=>$$d{data}{key}));
&database_do($sql);
}
#
# return
$$d{status_message} = "OK";
return 1;
}
sub dataitem_del(){
my $d = shift @_;
local($sql,%hash,$value,$i,$tmp,$tmp1,$tmp2);
#
# check basic
if ($$d{config}{sql_key} eq "") {$$d{status_message} = "No sql_key"; return 0;}
if ($$d{config}{sql_del} eq "") {$$d{status_message} = "No sql_del"; return 0;}
if ($$d{data}{key} eq "") {$$d{status_message} = "No key"; return 0;}
#
# check if key exists
$sql = &dataitem_tools_sql_parse($$d{config}{sql_key},('KEY'=>$$d{data}{key}));
%hash = database_select($sql);
if ($hash{DATA}{0}{0} ne 1) {$$d{status_message} = "key not found"; return 0;}
#