forked from xneelo/ipplan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.dbflib.php
More file actions
executable file
·1178 lines (1018 loc) · 43.9 KB
/
class.dbflib.php
File metadata and controls
executable file
·1178 lines (1018 loc) · 43.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// IPplan v4.92b
// Aug 24, 2001
//
// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// workaround for funky behaviour of nested includes with older
// versions of php
require_once(dirname(__FILE__)."/config.php");
define("DBF_API_VER", "1");
// generic errors
//$myError[1] = my_("Could not connect to database");
//$myError[2] = my_("Incorrect parameters");
//$myError[3] = my_("Incorrect API version");
//$myError[50] = my_("Invalid IP address!");
// specific errors for DisplayBase
//$myError[100] =
class IPplanDbf {
public $ds;
// default search expression type
public $expr="RLIKE";
// default size search - larger than
public $size=0;
// default dhcp search - 1 to search for DHCP subnets only
public $dhcp=0;
// open a database connection
public function __construct() {
// create a connection id
$this->ds = ADONewConnection(DBF_TYPE);
$this->ds->debug = DBF_DEBUG;
// some local locales only accept 24 hour date formats - MSSQL?
if (DBF_TYPE=="mssql" or DBF_TYPE=="ado_mssql" or DBF_TYPE=="odbc_mssql") {
$this->ds->fmtTimeStamp = "'Y-m-d H:i:s'";
}
if (DBF_PERSISTENT) {
// Fix issue with Reverse DNS imports that use nested database calls
if (DBF_TYPE=="mysql" or DBF_TYPE=="maxsql" or DBF_TYPE=="postgres7")
$this->ds->autoRollback = FALSE;
if ($this->ds->
PConnect(DBF_HOST, DBF_USER, DBF_PASSWORD, DBF_NAME)) {
$this->ds->SetFetchMode(ADODB_FETCH_ASSOC);
return $this->ds;
}
}
else {
if ($this->ds->
Connect(DBF_HOST, DBF_USER, DBF_PASSWORD, DBF_NAME) != false) {
$this->ds->SetFetchMode(ADODB_FETCH_ASSOC);
return $this->ds;
}
}
// kill connection info if error - probably bogus database name
unset($this->ds);
return false;
}
// start of transaction for transaction aware tables
function DbfTransactionStart() {
if (DBF_TRANSACTIONS)
$this->ds->BeginTrans();
}
// end of transaction for transaction aware tables
function DbfTransactionEnd() {
if (DBF_TRANSACTIONS)
$this->ds->CommitTrans();
}
// rollback of transaction for transaction aware tables
function DbfTransactionRollback() {
if (DBF_TRANSACTIONS)
$this->ds->RollBackTrans();
}
// add a new ipaddress entry in the ipaddr table if it does not already exist
// Else it just updates existing record
function DeleteIP($ipaddr, $baseindex) {
// lock the row for update - at the same time get current row
if($row=$this->GetIPDetails($baseindex, $ipaddr)) {
// log the entire row to the auditlog for history - field
// is too small!
$this->ds->RowLock("ipaddr", "baseindex=$baseindex AND ipaddr=$ipaddr");
}
$row["event"]=132;
$row["user"]=getAuthUsername();
$row["action"]="delete ip details";
$row["baseindex"]=$baseindex;
$row["ip"]=inet_ntoa($ipaddr);
$this->ds->Execute("DELETE FROM ipaddr
WHERE baseindex=$baseindex AND
ipaddr=$ipaddr") and
$this->ds->Execute("DELETE FROM ipaddradd
WHERE baseindex=$baseindex AND
ipaddr=$ipaddr") and
$this->AuditLog($row);
}
// add a new ipaddress entry in the ipaddr table if it does not already exist
// Else it just updates existing record
function AddIP($ipaddr, $baseindex, $user, $location, $telno, $macaddr, $descrip, $hname, $info) {
$userid = getAuthUsername();
// add main data to ipaddr table emulate mysql replace
// (try update, if fails due to non-existant record, them
// insert) to ipaddr table
// change to SELECT, test, then UPDATE or INSERT to prevent key errors
// RE 30/3/2005 - should use FOR UPDATE on SELECT, but don't know compat?
// did away with using Affected_Rows as this returns 0 if no rec to update
// THIS GETS DONE TWICE!!! ONCE FOR AUDITLOG AND AGAIN HERE!
if($this->ds->GetRow("SELECT ipaddr
FROM ipaddr
WHERE baseindex=$baseindex AND
ipaddr=$ipaddr")) { // should have FOR UPDATE here!
$result = $this->ds->Execute("UPDATE ipaddr
SET userinf=".$this->ds->qstr($user).",
location=".$this->ds->qstr($location).",
telno=".$this->ds->qstr($telno).",
macaddr=".$this->ds->qstr($macaddr).",
descrip=".$this->ds->qstr($descrip).",
hname=".$this->ds->qstr($hname).",
lastmod=".$this->ds->DBTimeStamp(time()).",
userid=".$this->ds->qstr($userid)."
WHERE baseindex=$baseindex AND
ipaddr=$ipaddr");
}
else {
$result = $this->ds->Execute("INSERT INTO ipaddr
(userinf, location, telno, macaddr, descrip, hname,
baseindex, ipaddr, lastmod, userid)
VALUES
(".$this->ds->qstr($user).",
".$this->ds->qstr($location).",
".$this->ds->qstr($telno).",
".$this->ds->qstr($macaddr).",
".$this->ds->qstr($descrip).",
".$this->ds->qstr($hname).",
$baseindex,
$ipaddr,
".$this->ds->DBTimeStamp(time()).",
".$this->ds->qstr($userid).")");
}
// always try to update record - record could not exist, which
// is OK, but always add record if info is not blank
// AddIP could be called from import functions or create subnet
// to add DNS or nmap records - do not add record if info is empty - waste?
// add serialized info from userdefined template
// to ipaddradd table
if($this->ds->GetRow("SELECT ipaddr
FROM ipaddradd
WHERE baseindex=$baseindex AND
ipaddr=$ipaddr")) { // should have FOR UPDATE here!
$result = $this->ds->Execute("UPDATE ipaddradd
SET info=".$this->ds->qstr($info)."
WHERE baseindex=$baseindex AND
ipaddr=$ipaddr");
// this generates a "duplicate key" error if no update
// should be OK under normal circumstances, but generates error under
// debug mode turned on
}
else {
if (!empty($info)) {
$result = $this->ds->Execute("INSERT INTO ipaddradd
(info, baseindex, ipaddr)
VALUES
(".$this->ds->qstr($info).",
$baseindex,
$ipaddr)");
}
}
}
// update an existing IP record - only updates relevant field
function UpdateIP($ipaddr, $baseindex, $field, $value) {
$userid = getAuthUsername();
$result = $this->ds->Execute("UPDATE ipaddr
SET $field=".$this->ds->qstr($value).",
lastmod=".$this->ds->DBTimeStamp(time()).",
userid=".$this->ds->qstr($userid)."
WHERE baseindex=$baseindex AND ipaddr=$ipaddr");
// record does not exist, error
return $this->ds->Affected_Rows();
}
// modify a single ip address or netrange
// supply baseindex as arg
// assumes info is valid!!!
function ModifyIP($ipaddr, $baseindex, $user, $location,
$telno, $macaddr, $descrip, $hname, $info) {
if (is_array($ipaddr)) {
// dont use REPLACE here as maybe only one field
// must be updated causing others to be cleared!!!
foreach($ipaddr as $value) {
// lock the row for update - at the same time get current row
if($row=$this->GetIPDetails($baseindex, $value)) {
// log the entire row to the auditlog for history - field
// is too small!
// probably better to rather just insert into a duplicate
// ipaddr table, or to have a trigger event do something
$row["event"]=130;
$row["user"]=getAuthUsername();
$row["action"]="log old row contents";
$row["baseindex"]=$baseindex;
$row["ip"]=inet_ntoa($value);
$this->AuditLog($row);
$this->ds->RowLock("ipaddr", "baseindex=$baseindex AND ipaddr=$value");
}
if ($user) {
if (!$this->
UpdateIP($value, $baseindex, "userinf", $user))
$this->AddIP($value, $baseindex, $user,
$location, $telno, $macaddr, $descrip, $hname, $info);
}
if ($location) {
if (!$this->
UpdateIP($value, $baseindex, "location",
$location))
$this->AddIP($value, $baseindex, $user,
$location, $telno, $macaddr, $descrip, $hname, $info);
}
if ($descrip) {
if (!$this->
UpdateIP($value, $baseindex, "descrip",
$descrip))
$this->AddIP($value, $baseindex, $user,
$location, $telno, $macaddr, $descrip, $hname, $info);
}
if ($telno) {
if (!$this->
UpdateIP($value, $baseindex, "telno", $telno))
$this->AddIP($value, $baseindex, $user,
$location, $telno, $macaddr, $descrip, $hname, $info);
}
if ($macaddr) {
if (!$this->
UpdateIP($value, $baseindex, "macaddr", $macaddr))
$this->AddIP($value, $baseindex, $user,
$location, $telno, $macaddr, $descrip, $hname, $info);
}
// special case - called from DNS scripts to update hostname field
if ($hname) {
if (!$this->
UpdateIP($value, $baseindex, "hname", $hname))
$this->AddIP($value, $baseindex, $user,
$location, $telno, $macaddr, $descrip, $hname, $info);
}
// this does not generate a trigger if multiple recs are updated!!!
$this->
AuditLog(sprintf
(my_
("User %s modified ip details %s index %u"),
getAuthUsername(),
inet_ntoa($value), $baseindex));
}
}
else {
// lock the row for update - at the same time get current row
if($row=$this->GetIPDetails($baseindex, $ipaddr)) {
// log the entire row to the auditlog for history - field
// is too small!
// probably better to rather just insert into a duplicate
// ipaddr table, or to have a trigger event do something
$row["event"]=130;
$row["user"]=getAuthUsername();
$row["action"]="log old row contents";
$row["baseindex"]=$baseindex;
$row["ip"]=inet_ntoa($ipaddr);
$this->AuditLog($row);
$this->ds->RowLock("ipaddr", "baseindex=$baseindex AND ipaddr=$ipaddr");
}
$this->AddIP($ipaddr, $baseindex, $user, $location, $telno, $macaddr,
$descrip, $hname, $info);
$this->AuditLog(array("event"=>131, "action"=>"modify ip details", "ip"=>inet_ntoa($ipaddr),
"user"=>getAuthUsername(), "ipaddr"=>$ipaddr, "baseindex"=>$baseindex,
"userinf"=>$user, "location"=>$location, "telno"=>$telno, "macaddr"=>$macaddr,
"descrip"=>$descrip, "hname"=>$hname));
}
}
// create a new subnet
// baseaddr is an int, not an ip address
// assumes info is valid!!!
// returns the last inserted baseindex, 0 on failure
function CreateSubnet($baseaddr, $subnetsize, $descrip, $cust, $dhcp, $grp) {
$userid = getAuthUsername();
// no other options defined for baseopt, so always make 1 or 0
$result = $this->ds->Execute("INSERT INTO base
(baseaddr, subnetsize, descrip, admingrp,
baseopt, customer, userid, lastmod)
VALUES
($baseaddr, $subnetsize,
".$this->ds->qstr($descrip).",
".$this->ds->qstr($grp).",
$dhcp,
$cust,
".$this->ds->qstr($userid).",
".$this->ds->DBTimeStamp(time()).")");
if (DBF_TYPE == "mysql" or DBF_TYPE == "maxsql") {
return $this->ds->Insert_ID();
}
else {
// emulate getting the last insert_id
$result = $this->ds->Execute("SELECT baseindex
FROM base
WHERE baseaddr=$baseaddr AND customer=$cust");
$temprow = $result->FetchRow();
return $temprow["baseindex"];
}
}
// check if user is part of customer admin group
function TestCustomerGrp($baseindex, $userid) {
// could use GetRow here
$result = $this->ds->Execute("SELECT customer.admingrp AS admingrp
FROM base, customer, usergrp
WHERE base.baseindex=$baseindex AND
base.customer=customer.customer AND
customer.admingrp=usergrp.grp AND
usergrp.userid=".$this->ds->qstr($userid));
if ($row = $result->FetchRow()) {
return $row["admingrp"];
}
else
return 0;
}
// check if user can create/modify customers
function TestCustomerCreate($userid) {
// could use GetRow here
$result = $this->ds->Execute("SELECT usergrp.grp
FROM usergrp, grp
WHERE usergrp.userid=".$this->ds->qstr($userid)." AND
usergrp.grp=grp.grp AND
grp.createcust=".$this->ds->qstr('Y'));
if ($row = $result->FetchRow()) {
return $row["grp"];
}
else
return 0;
}
// find the subnets admin group
function GetBaseGrp($baseindex) {
// could use GetRow here
$result = $this->ds->Execute("SELECT admingrp
FROM base
WHERE baseindex=$baseindex");
if ($row = $result->FetchRow()) {
return $row["admingrp"];
}
else
return 0;
}
// delete a subnet
// supply baseindex as arg
// assumes info is valid!!!
// no need for customer as baseindex makes delete unique!!!
function DeleteSubnet($baseindex) {
$result = $this->ds->Execute("DELETE FROM base
WHERE baseindex=$baseindex") and
$result = $this->ds->Execute("DELETE FROM ipaddr
WHERE baseindex=$baseindex") and
$result = $this->ds->Execute("DELETE FROM baseadd
WHERE baseindex=$baseindex");
return $result;
}
// takes groups array and generates SQL for where clause
function grpSQL($grps) {
$string = " IN (";
foreach($grps as $value) {
$string .= $this->ds->qstr($value).",";
}
return substr($string, 0, -1).")";
}
// test if subnets overlap within a customer only
// no need to pass groups as validity of user to access this customer
// would have been determined already through user auth
function GetDuplicateSubnet($baseaddr, $subnetsize, $cust) {
// get baseaddr and subnetsize so that info is available for
// importbase - we need to check for EXACT matches there
$sqllastmod = $this->ds->SQLDate("M d Y H:i:s", 'lastmod');
$sqlswipmod = $this->ds->SQLDate("M d Y H:i:s", 'swipmod');
return $this->ds->
Execute("SELECT baseaddr, subnetsize, baseindex, descrip,
$sqllastmod AS lastmod, userid, $sqlswipmod AS swipmod
FROM base
WHERE (($baseaddr BETWEEN baseaddr AND
baseaddr + subnetsize - 1) OR
($baseaddr < baseaddr AND
$baseaddr+$subnetsize >
baseaddr + subnetsize - 1)) AND
customer=$cust");
}
// test if bounds overlap, ie. group can interact with subnet
// returns 1 if action allowed (create etc), zero if action not allowed
function TestBounds($boundsaddr, $boundssize, $grp) {
$result = $this->ds->Execute("SELECT count(*) AS cnt
FROM bounds
WHERE grp=".$this->ds->qstr($grp));
$row = $result->FetchRow();
// no bounds, group can do anything
if ($row["cnt"] == 0) {
return 1;
}
$result = $this->ds->Execute("SELECT boundsaddr
FROM bounds
WHERE (($boundsaddr BETWEEN boundsaddr AND
boundsaddr + boundssize - 1) AND
($boundsaddr+$boundssize-1 BETWEEN boundsaddr AND
boundsaddr + boundssize - 1)) AND
grp=".$this->ds->qstr($grp));
if ($result->FetchRow()) {
return 1;
}
return 0;
}
// test if subnets overlap between customers - looks at all customers
// need groups here as some users may not see all customers
function GetDuplicateSubnetAll($baseaddr, $subnetsize, $grps = 0) {
$sql = "";
if (!empty($grps)) {
if (!$this->TestGrpsAdmin($grps)) {
// add groups to search in if required - no index here!!!
$sql .= " AND customer.admingrp ".$this->grpSQL($grps);
}
}
$sqllastmod = $this->ds->SQLDate("M d Y H:i:s", 'base.lastmod');
$sqlswipmod = $this->ds->SQLDate("M d Y H:i:s", 'base.swipmod');
return $this->ds->
Execute("SELECT base.baseaddr, base.subnetsize, base.baseindex,
base.descrip, customer.custdescrip,
customer.customer, $sqllastmod AS lastmod, base.userid,
$sqlswipmod AS swipmod
FROM base, customer
WHERE (($baseaddr BETWEEN base.baseaddr AND
base.baseaddr + base.subnetsize - 1) OR
($baseaddr < base.baseaddr AND
$baseaddr+$subnetsize >
base.baseaddr + base.subnetsize - 1)) AND
base.customer = customer.customer
$sql");
}
// find subnets that are NOT part of any netranges
function GetBaseNoArea($descrip, $cust, $grps = 0) {
$sql = "";
if ($descrip) {
$sql=$this->mySearchSql("base.descrip", $this->expr, $descrip);
}
// does user want to search on subnet size?
if ($this->size > 0) {
$sql.=" AND base.subnetsize > $this->size ";
}
$sqllastmod = $this->ds->SQLDate("M d Y H:i:s", 'base.lastmod');
$sqlswipmod = $this->ds->SQLDate("M d Y H:i:s", 'base.swipmod');
return $this->ds->
Execute("SELECT base.baseindex, base.subnetsize,
base.descrip, base.baseaddr, base.admingrp,
$sqllastmod AS lastmod, base.userid, $sqlswipmod AS swipmod
FROM base
LEFT JOIN netrange ON base.baseaddr BETWEEN netrange.rangeaddr
AND netrange.rangeaddr+netrange.rangesize-1
AND base.customer=netrange.customer
WHERE base.customer=$cust
AND netrange.rangeaddr IS NULL
$sql
ORDER BY
base.baseaddr");
}
// find base address info from an area
// not all fields are used, but get them anyway so function is
// more generic
// if all customer, must filter on groups
function GetBaseFromArea($areaindex, $descrip, $cust, $grps = 0) {
$sql = "";
if ($descrip) {
$sql=$this->mySearchSql("base.descrip", $this->expr, $descrip);
}
// does user want to search on subnet size?
if ($this->size > 0) {
$sql.=" AND base.subnetsize > $this->size ";
}
$sqllastmod = $this->ds->SQLDate("M d Y H:i:s", 'base.lastmod');
$sqlswipmod = $this->ds->SQLDate("M d Y H:i:s", 'base.swipmod');
// if duplicate netranges allowed we need to suppress them
// take care of order here relative to other SQL
if ($cust == 0) {
if (!empty($grps)) {
if (!$this->TestGrpsAdmin($grps)) {
// add groups to search in if required - no index here!!!
// add at begining of SQL due to GROUP by above!
$sql.= " AND customer.admingrp ".$this->grpSQL($grps);
}
}
return $this->ds->
Execute("SELECT DISTINCT base.baseindex, base.subnetsize,
base.descrip, base.baseaddr, base.admingrp,
$sqllastmod AS lastmod, base.userid, $sqlswipmod AS swipmod,
customer.custdescrip, customer.customer
FROM base, netrange, customer
WHERE netrange.areaindex=$areaindex
AND base.baseaddr BETWEEN netrange.rangeaddr
AND netrange.rangeaddr+netrange.rangesize-1
AND base.customer=customer.customer
$sql
ORDER BY
base.baseaddr");
}
else {
return $this->ds->
Execute("SELECT DISTINCT base.baseindex, base.subnetsize,
base.descrip, base.baseaddr, base.admingrp,
$sqllastmod AS lastmod, base.userid, $sqlswipmod AS swipmod
FROM base, netrange
WHERE netrange.areaindex=$areaindex
AND base.baseaddr BETWEEN netrange.rangeaddr
AND netrange.rangeaddr+netrange.rangesize-1
AND base.customer=$cust
AND netrange.customer=$cust
$sql
ORDER BY
base.baseaddr");
}
}
// find base address info
// not all fields are used, but get them anyway so function is
// more generic
function GetBase($startnum, $endnum, $descrip, $cust, $grps = 0) {
$sql = "";
if ($descrip) {
$sql=$this->mySearchSql("base.descrip", $this->expr, $descrip);
}
// does user want to search on subnet size?
if ($this->size > 0) {
$sql.=" AND base.subnetsize > $this->size ";
}
// NOTE: this should be & if there are additional bit masks
if ($this->dhcp > 0) {
$sql.=" AND base.baseopt > 0 ";
}
$sqllastmod = $this->ds->SQLDate("M d Y H:i:s", 'base.lastmod');
$sqlswipmod = $this->ds->SQLDate("M d Y H:i:s", 'base.swipmod');
if ($cust == 0) {
if (!empty($grps)) {
if (!$this->TestGrpsAdmin($grps)) {
// add groups to search in if required - no index here!!!
$sql .= " AND customer.admingrp ".$this->grpSQL($grps);
}
}
return $this->ds->
Execute("SELECT base.baseindex, base.subnetsize,
base.descrip, base.baseaddr, base.admingrp,
$sqllastmod AS lastmod, base.userid, $sqlswipmod AS swipmod,
customer.custdescrip, customer.customer
FROM base, customer
WHERE base.baseaddr BETWEEN $startnum AND $endnum
AND base.customer=customer.customer
$sql
ORDER BY
base.baseaddr");
}
else {
return $this->ds->
Execute("SELECT base.baseindex, base.subnetsize,
base.descrip, base.baseaddr, base.admingrp,
$sqllastmod AS lastmod, base.userid, $sqlswipmod AS swipmod
FROM base
WHERE base.baseaddr BETWEEN $startnum AND $endnum
$sql
AND base.customer=$cust
ORDER BY
base.baseaddr");
}
}
// find base address info from a baseindex
// added customer as field - required as backlink in modifyipform - 13/3/2005
function GetBaseFromIndex($baseindex) {
$sqlswipmod = $this->ds->SQLDate("M d Y H:i:s", 'swipmod');
return $this->ds->Execute("SELECT baseindex, subnetsize,
descrip, baseaddr, $sqlswipmod AS swipmod, customer,
baseopt
FROM base
WHERE baseindex=$baseindex");
}
// find base address info from a ip
function GetBaseFromIP($ip, $cust) {
// query looks odd, but record may not exist, but would still
// like to know which baseaddr it should belong to
return $this->ds->Execute("SELECT baseindex, subnetsize,
descrip, baseaddr
FROM base
WHERE $ip BETWEEN baseaddr AND
baseaddr+subnetsize-1 AND
customer=$cust");
}
// get all the ip records from a subnet based on a baseindex
// could return nothing as there are no records in database for
// entries where all fields are blank
function GetSubnetDetails($baseindex, $sql="") {
// no use converting ipaddr to quad as there may be holes in database
// which needs to be calculated at runtime
$sqllastmod = $this->ds->SQLDate("M d Y H:i:s", 'lastmod');
return $this->ds->Execute("SELECT userinf, location, telno,
descrip, hname, ipaddr, $sqllastmod AS lastmod,
userid, lastpol, macaddr
FROM ipaddr
WHERE baseindex=$baseindex $sql
ORDER BY
ipaddr");
}
// get one ip record from a subnet based on a baseindex and ipaddr
// could return nothing as there are no records in database for
// entries where all fields are blank
function GetIPDetails($baseindex, $ipaddr) {
// no use converting ipaddr to quad as there may be holes in database
// which needs to be calculated at runtime
$sqllastmod = $this->ds->SQLDate("M d Y H:i:s", 'lastmod');
return $this->ds->GetRow("SELECT userinf, location, telno,
descrip, hname, ipaddr, $sqllastmod AS lastmod, userid
FROM ipaddr
WHERE baseindex=$baseindex AND ipaddr=$ipaddr");
}
// update the poll date of the address - will add a record if required
function UpdateIPPoll($baseindex, $ipaddr) {
$result = $this->ds->Execute("UPDATE ipaddr
SET lastpol=".$this->ds->DBTimeStamp(time())."
WHERE baseindex=$baseindex AND
ipaddr=$ipaddr");
if ($this->ds->Affected_Rows() == 0) {
$result = $this->ds->Execute("INSERT INTO ipaddr
(userinf, location, telno, descrip, hname,
baseindex, ipaddr, lastmod, lastpol, userid)
VALUES
(".$this->ds->qstr("").",
".$this->ds->qstr("").",
".$this->ds->qstr("").",
".$this->ds->qstr("Unknown - added by IPplan poller").",
".$this->ds->qstr("").",
$baseindex,
$ipaddr,
".$this->ds->DBTimeStamp(time()).",
".$this->ds->DBTimeStamp(time()).",
".$this->ds->qstr("POLLER").")");
}
}
// check if user has full rights to view everthing
function TestGrpsAdmin($grps) {
if (!empty($grps)) {
$result = $this->GetGrpsInfo($grps);
while ($row = $result->FetchRow()) {
if ($row["grpopt"] & 1)
return TRUE;
}
}
return FALSE;
}
// get all the customer info
function GetGrps() {
return $this->ds->Execute("SELECT grpdescrip, grp
FROM grp
ORDER BY grpdescrip");
}
// get all the info for groups user belongs to
// don't pass empty grps array as you will generate database error
function GetGrpsInfo($grps) {
$sql = $this->grpSQL($grps);
return $this->ds->
Execute("SELECT grpdescrip, grp, createcust, grpopt
FROM grp
WHERE grp $sql");
}
// get all the customers
function GetCustomer($sql="") {
if (empty($sql)) {
return $this->ds->Execute("SELECT custdescrip, customer, admingrp, crm
FROM customer
ORDER BY custdescrip");
}
else {
return $this->ds->Execute("SELECT custdescrip, customer, admingrp, crm
FROM customer
WHERE $sql
ORDER BY custdescrip");
}
}
// get all the customer info
function GetCustomerInfo($cust) {
return $this->ds->Execute("SELECT *
FROM custinfo
WHERE customer=$cust");
}
// get all the customer DNS info
function GetCustomerDNSInfo($cust) {
return $this->ds->Execute("SELECT hname, ipaddr
FROM revdns
WHERE customer=$cust
ORDER BY horder");
}
// get customer description
function GetCustomerDescrip($cust) {
$row = $this->ds->GetRow("SELECT custdescrip
FROM customer
WHERE customer=$cust");
return $row["custdescrip"];
}
// get particular customer group info
// if cust = 0, get info for all customers
// autoincrement field cannot be 0, so database integrity retained
function GetCustomerGrp($cust) {
if ($cust == 0) {
return $this->ds->
Execute("SELECT custdescrip, customer, admingrp
FROM customer
ORDER BY custdescrip");
}
else {
return $this->ds->
Execute("SELECT custdescrip, customer, admingrp
FROM customer
WHERE customer=$cust");
}
}
// get all the netrange info
function GetRangeInArea($cust, $areaindex) {
return $this->ds->
Execute("SELECT rangeaddr, rangesize, descrip, rangeindex
FROM netrange
WHERE areaindex=$areaindex
ORDER BY rangeaddr");
}
// get all the netrange info
function GetRange($cust, $rangeindex) {
if (!$rangeindex) {
return $this->ds->
Execute("SELECT rangeaddr, rangesize, descrip, rangeindex
FROM netrange
WHERE customer=$cust
ORDER BY rangeaddr");
}
else {
return $this->ds->
Execute("SELECT rangeaddr, rangesize, descrip, rangeindex
FROM netrange
WHERE rangeindex=$rangeindex
ORDER BY rangeaddr");
}
}
// get all the area info
function GetArea($cust, $areaindex) {
// all areas for customer
if ($areaindex==0) {
return $this->ds->Execute("SELECT areaaddr, descrip, areaindex
FROM area
WHERE customer=$cust
ORDER BY areaaddr");
}
// only areas that have netranges defined
else if ($areaindex==-1) {
return $this->ds->Execute("SELECT area.areaaddr, area.descrip, area.areaindex
FROM area, netrange
WHERE area.customer=$cust AND
netrange.areaindex=area.areaindex
ORDER BY area.areaaddr");
}
// get specific area
else {
return $this->ds->Execute("SELECT areaaddr, descrip, areaindex
FROM area
WHERE areaindex=$areaindex
ORDER BY areaaddr");
}
}
// get md5 checksum of records to change
// ip is an integer ip address or array of integer ip addresses
function GetMD5($ip, $baseindex) {
$sql="";
if (is_array($ip)) {
foreach($ip as $value) {
$sql .= "$value,";
}
}
else {
$sql = "$ip,";
}
// chop extra , if array was passed
$sql = substr($sql, 0, strlen($sql) - 1);
// optimised for mysql
if (DBF_TYPE == "mysql" or DBF_TYPE == "maxsql") {
$result =
&$this->ds->Execute("SELECT md5(sum(lastmod)) AS md5str
FROM ipaddr
WHERE ipaddr IN($sql) AND baseindex=$baseindex
GROUP BY lastmod");
$row = $result->FetchRow();
return $row["md5str"];
}
else {
$sqllastmod = $this->ds->SQLDate("M d Y H:i:s", 'lastmod');
$result = $this->ds->Execute("SELECT $sqllastmod AS lastmod
FROM ipaddr
WHERE ipaddr IN($sql) AND baseindex=$baseindex");
$md5str="";
while ($row = $result->FetchRow()) {
$md5str .= $row["lastmod"];
}
return md5($md5str);
}
}
// put some stuff in the audit log
// $message is either a string or
// $message is associative array with at least one index called "event"
// if you do not want to send a trigger, use a string as parameter
// events must be unique, user_trigger function is in ipplanlib.php
// eg array("event"=>100)
// eg array("event"=>100, "action"=>"add zone", "domain"=>"example.com")
// see TRIGGERS file for more info
function AuditLog($message) {
if (AUDIT) {
if (is_string($message)) {
$this->ds->Execute("INSERT INTO auditlog
(action, userid, dt)
VALUES
(".$this->ds->qstr(substr($message,0,254)).",
".$this->ds->qstr(getAuthUsername()).",
".$this->ds->DBTimeStamp(time()).")");
}
else if (is_array($message)) {
// step through array
$newmsg="";
foreach ($message as $key=>$value) {
$newmsg .= "$key=$value, ";
}
$newmsg = substr($newmsg, 0, strlen($newmsg)-2);
// message could be long, so wrap over multiple log lines
$msgarr = explode("\n", wordwrap($newmsg, 250, "\n... "));
foreach($msgarr as $value) {
$this->ds->Execute("INSERT INTO auditlog
(action, userid, dt)
VALUES
(".$this->ds->qstr(substr($value,0,254)).",
".$this->ds->qstr(getAuthUsername()).",
".$this->ds->DBTimeStamp(time()).")");
}
// call external trigger function
if (EXT_FUNCTION) {
user_trigger($message);
}
}
}
}
// build an sql query for the search - $var contain field, type is expression,
// search is what to search for
// returns nothing if no search
function mySearchSql($var, $expr, $search, $addand=TRUE) {
$sql="";
if (!empty($search)) {
switch ($expr) {
case "NLIKE":
$sql="$var NOT LIKE ".$this->ds->qstr("%$search%");
break;
case "EXACT":
$sql="$var = ".$this->ds->qstr("$search");
break;
case "RLIKE":
// default is RLIKE, need to protect for DBF's without RLIKE
if (preg_ispreg($search)) { // invalid regex
return "";
}
if (DBF_TYPE=="mysql" or DBF_TYPE=="maxsql") {
$sql="$var RLIKE ".$this->ds->qstr("$search");
break;
}
if (DBF_TYPE=="postgres7") {