forked from opendcim/openDCIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceTemplate.class.php
781 lines (686 loc) · 23.7 KB
/
DeviceTemplate.class.php
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
<?php
/*
openDCIM
This is the main class library for the openDCIM application, which
is a PHP/Web based data center infrastructure management system.
This application was originally written by Scott A. Milliken while
employed at Vanderbilt University in Nashville, TN, as the
Data Center Manager, and released under the GNU GPL.
Copyright (C) 2011 Scott A. Milliken
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, version 3.
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.
For further details on the license, see http://www.gnu.org/licenses
*/
class DeviceTemplate {
var $TemplateID;
var $ManufacturerID;
var $Model;
var $Height;
var $Weight;
var $Wattage;
var $DeviceType;
var $PSCount;
var $NumPorts;
var $Notes;
var $FrontPictureFile;
var $RearPictureFile;
var $ChassisSlots;
var $RearChassisSlots;
var $SNMPVersion;
var $CustomValues;
var $GlobalID;
var $ShareToRepo;
var $KeepLocal;
public function __construct($dtid=false){
if($dtid){
$this->TemplateID=intval($dtid);
}
return $this;
}
function MakeSafe(){
$validDeviceTypes=array('Server','Appliance','Storage Array','Switch','Chassis','Patch Panel','Physical Infrastructure','CDU','Sensor');
$validSNMPVersions=array(1,'2c',3);
// Instead of defaulting to v2c for snmp we'll default to whatever the system default is
global $config;
$this->TemplateID=intval($this->TemplateID);
$this->ManufacturerID=intval($this->ManufacturerID);
$this->Model=sanitize($this->Model);
$this->Height=intval($this->Height);
$this->Weight=intval($this->Weight);
$this->Wattage=intval($this->Wattage);
$this->DeviceType=(in_array($this->DeviceType, $validDeviceTypes))?$this->DeviceType:'Server';
$this->PSCount=intval($this->PSCount);
$this->NumPorts=intval($this->NumPorts);
$this->Notes=sanitize($this->Notes,false);
$this->FrontPictureFile=sanitize($this->FrontPictureFile);
$this->RearPictureFile=sanitize($this->RearPictureFile);
$this->ChassisSlots=intval($this->ChassisSlots);
$this->RearChassisSlots=intval($this->RearChassisSlots);
$this->SNMPVersion=(in_array($this->SNMPVersion, $validSNMPVersions))?$this->SNMPVersion:$config->ParameterArray["SNMPVersion"];
$this->GlobalID=intval($this->GlobalID);
$this->ShareToRepo=intval($this->ShareToRepo);
$this->KeepLocal=intval($this->KeepLocal);
}
function MakeDisplay(){
$this->Model=stripslashes($this->Model);
$this->Notes=stripslashes($this->Notes);
$this->FrontPictureFile=stripslashes($this->FrontPictureFile);
$this->RearPictureFile=stripslashes($this->RearPictureFile);
}
static function RowToObject($row,$extendmodel=true){
$Template=new DeviceTemplate();
$Template->TemplateID=$row["TemplateID"];
$Template->ManufacturerID=$row["ManufacturerID"];
$Template->Model=$row["Model"];
$Template->Height=$row["Height"];
$Template->Weight=$row["Weight"];
$Template->Wattage=$row["Wattage"];
$Template->DeviceType=$row["DeviceType"];
$Template->PSCount=$row["PSCount"];
$Template->NumPorts=$row["NumPorts"];
$Template->Notes=$row["Notes"];
$Template->FrontPictureFile=$row["FrontPictureFile"];
$Template->RearPictureFile=$row["RearPictureFile"];
$Template->ChassisSlots=$row["ChassisSlots"];
$Template->RearChassisSlots=$row["RearChassisSlots"];
$Template->SNMPVersion=$row["SNMPVersion"];
$Template->GlobalID = $row["GlobalID"];
$Template->ShareToRepo = $row["ShareToRepo"];
$Template->KeepLocal = $row["KeepLocal"];
$Template->MakeDisplay();
$Template->GetCustomValues();
if($extendmodel){
// Extend our device model
if($Template->DeviceType=="CDU"){
$cdut=new CDUTemplate();
$cdut->TemplateID=$Template->TemplateID;
$cdut->GetTemplate();
foreach($cdut as $prop => $val){
$Template->$prop=$val;
}
}
if($Template->DeviceType=="Sensor"){
$st=new SensorTemplate();
$st->TemplateID=$Template->TemplateID;
$st->GetTemplate();
foreach($st as $prop => $val){
$Template->$prop=$val;
}
}
}
return $Template;
}
function query($sql){
global $dbh;
return $dbh->query($sql);
}
function exec($sql){
global $dbh;
return $dbh->exec($sql);
}
function prepare( $sql ) {
global $dbh;
return $dbh->prepare( $sql );
}
function clearShareFlag() {
$st = $this->prepare( "update fac_DeviceTemplate set ShareToRepo=0 where TemplateID=:TemplateID" );
$st->execute( array( ":TemplateID"=>$this->TemplateID ) );
}
function CreateTemplate(){
global $dbh;
$this->MakeSafe();
$sql="INSERT INTO fac_DeviceTemplate SET ManufacturerID=$this->ManufacturerID,
Model=\"$this->Model\", Height=$this->Height, Weight=$this->Weight,
Wattage=$this->Wattage, DeviceType=\"$this->DeviceType\",
PSCount=$this->PSCount, NumPorts=$this->NumPorts, Notes=\"$this->Notes\",
FrontPictureFile=\"$this->FrontPictureFile\", RearPictureFile=\"$this->RearPictureFile\",
ChassisSlots=$this->ChassisSlots, RearChassisSlots=$this->RearChassisSlots, SNMPVersion=\"$this->SNMPVersion\",
GlobalID=$this->GlobalID, ShareToRepo=$this->ShareToRepo, KeepLocal=$this->KeepLocal;";
if(!$dbh->exec($sql)){
error_log( "SQL Error: " . $sql );
return false;
}else{
$this->TemplateID=$dbh->lastInsertId();
if($this->DeviceType=="CDU"){
// If this is a cdu make the corresponding other hidden template
$cdut=new CDUTemplate();
foreach($cdut as $prop => $val){
if(isset($this->$prop)){
$cdut->$prop=$this->$prop;
}
}
$cdut->CreateTemplate($this->TemplateID);
}
if($this->DeviceType=="Sensor"){
// If this is a sensor make the corresponding other hidden template
$st=new SensorTemplate();
foreach($st as $prop => $val){
if(isset($this->$prop)){
$st->$prop=$this->$prop;
}
}
$st->CreateTemplate($this->TemplateID);
}
(class_exists('LogActions'))?LogActions::LogThis($this):'';
$this->MakeDisplay();
return true;
}
}
function UpdateTemplate(){
$this->MakeSafe();
$sql="UPDATE fac_DeviceTemplate SET ManufacturerID=$this->ManufacturerID,
Model=\"$this->Model\", Height=$this->Height, Weight=$this->Weight,
Wattage=$this->Wattage, DeviceType=\"$this->DeviceType\",
PSCount=$this->PSCount, NumPorts=$this->NumPorts, Notes=\"$this->Notes\",
FrontPictureFile=\"$this->FrontPictureFile\", RearPictureFile=\"$this->RearPictureFile\",
ChassisSlots=$this->ChassisSlots, RearChassisSlots=$this->RearChassisSlots, SNMPVersion=\"$this->SNMPVersion\",
GlobalID=$this->GlobalID, ShareToRepo=$this->ShareToRepo, KeepLocal=$this->KeepLocal
WHERE TemplateID=$this->TemplateID;";
$old=new DeviceTemplate();
$old->TemplateID=$this->TemplateID;
$old->GetTemplateByID();
if($old->DeviceType=="CDU" && $this->DeviceType!=$old->DeviceType){
// Template changed from CDU to something else, clean up the mess
$cdut=new CDUTemplate();
$cdut->TemplateID=$this->TemplateID;
$cdut->DeleteTemplate();
}elseif($this->DeviceType=="CDU" && $this->DeviceType!=$old->DeviceType){
// Template changed to CDU from something else, make the extra stuff
$cdut=new CDUTemplate();
$cdut->Model=$this->Model;
$cdut->ManufacturerID=$this->ManufacturerID;
$cdut->CreateTemplate($this->TemplateID);
}
if($old->DeviceType=="Sensor" && $this->DeviceType!=$old->DeviceType){
// Template changed from Sensor to something else, clean up the mess
$st=new SensorTemplate();
$st->TemplateID=$this->TemplateID;
$st->DeleteTemplate();
}elseif($this->DeviceType=="Sensor" && $this->DeviceType!=$old->DeviceType){
// Template changed to Sensor from something else, make the extra stuff
$st=new SensorTemplate();
$st->Model=$this->Model;
$st->ManufacturerID=$this->ManufacturerID;
$st->CreateTemplate($this->TemplateID);
}
if(!$this->query($sql)){
return false;
}else{
(class_exists('LogActions'))?LogActions::LogThis($this,$old):'';
$this->MakeDisplay();
return true;
}
}
function DeleteTemplate(){
$this->MakeSafe();
// If we're removing the template clean up the children
$this->DeleteSlots();
$this->DeletePorts();
$sql="DELETE FROM fac_DeviceTemplate WHERE TemplateID=$this->TemplateID;";
(class_exists('LogActions'))?LogActions::LogThis($this):'';
return $this->exec($sql);
}
function Search($indexedbyid=false,$loose=false){
$o=new stdClass();
// Store any values that have been added before we make them safe
foreach($this as $prop => $val){
if(isset($val)){
$o->$prop=$val;
}
}
// Make everything safe for us to search with
$this->MakeSafe();
// This will store all our extended sql
$sqlextend="";
function findit($prop,$val,&$sql,$loose){
$method=($loose)?" LIKE \"%$val%\"":"=\"$val\"";
$sql.=" AND a.$prop$method";
}
foreach($o as $prop => $val){
findit($prop,$this->$prop,$sqlextend,$loose);
}
// The join is purely to sort the templates by the manufacturer's name
$sql="SELECT a.* FROM fac_DeviceTemplate a, fac_Manufacturer b WHERE
a.ManufacturerID=b.ManufacturerID$sqlextend ORDER BY Name ASC, Model ASC;";
$templateList=array();
foreach($this->query($sql) as $row){
if($indexedbyid){
$templateList[$row["TemplateID"]]=DeviceTemplate::RowToObject($row);
}else{
$templateList[]=DeviceTemplate::RowToObject($row);
}
}
return $templateList;
}
// Make a simple reference to a loose search
function LooseSearch($indexedbyid=false){
return $this->Search($indexedbyid,true);
}
function GetTemplateByID(){
$this->MakeSafe();
$sql="SELECT * FROM fac_DeviceTemplate WHERE TemplateID=$this->TemplateID;";
//JMGA Reset object in case of a lookup failure
$this->ManufacturerID=0;
$this->Model="";
$this->Height=0;
$this->Weight=0;
$this->Wattage=0;
$this->DeviceType='Server';
$this->PSCount=0;
$this->NumPorts=0;
$this->Notes="";
$this->FrontPictureFile="";
$this->RearPictureFile="";
$this->ChassisSlots=0;
$this->RearChassisSlots=0;
$this->SNMPVersion="";
$this->GlobalID=0;
$this->ShareToRepo=false;
$this->KeepLocal=false;
// Reset object in case of a lookup failure
//foreach($this as $prop => $value){
// $value=($prop!='TemplateID')?null:$value;
//}
if($row=$this->query($sql)->fetch()){
foreach(DeviceTemplate::RowToObject($row) as $prop => $value){
$this->$prop=$value;
}
return true;
}else{
return false;
}
}
function GetTemplateList(){
$sql="SELECT * FROM fac_DeviceTemplate a, fac_Manufacturer b WHERE
a.ManufacturerID=b.ManufacturerID ORDER BY Name ASC, Model ASC;";
$templateList=array();
foreach($this->query($sql) as $row){
$templateList[]=DeviceTemplate::RowToObject($row);
}
return $templateList;
}
function GetTemplateListByManufacturer(){
$this->MakeSafe();
$sql="SELECT * FROM fac_DeviceTemplate a, fac_Manufacturer b WHERE
a.ManufacturerID=b.ManufacturerID AND a.ManufacturerID=$this->ManufacturerID
ORDER BY Name ASC, Model ASC;";
$templateList=array();
foreach($this->query($sql) as $row){
$templateList[]=DeviceTemplate::RowToObject($row);
}
return $templateList;
}
function GetTemplateShareList() {
$sql = "select * from fac_DeviceTemplate where ManufacturerID in (select ManufacturerID from fac_Manufacturer where GlobalID>0) and ShareToRepo=true order by ManufacturerID ASC";
$templateList = array();
foreach( $this->query($sql) as $row ) {
$templateList[]=DeviceTemplate::RowToObject($row);
}
return $templateList;
}
/**
* Return a list of the templates indexed by the TemplateID
*
* @return multitype:DeviceTemplate
*/
public static function getTemplateListIndexedbyID() {
global $dbh;
$templateList = array();
$stmt = $dbh->prepare('select * from fac_DeviceTemplate');
$stmt->execute();
while ($row = $stmt->fetch()) {
$devTempl = DeviceTemplate::RowToObject($row);
$templateList[$devTempl->TemplateID] = $devTempl;
}
return $templateList;
}
function GetMissingMfgDates(){
$this->MakeSafe();
$sql="SELECT a.* FROM fac_Device a, fac_DeviceTemplate b WHERE
a.TemplateID=b.TemplateID AND b.ManufacturerID=$this->ManufacturerID AND
a.MfgDate<'1970-01-01'";
$devList=array();
foreach($this->query($sql) as $row){
$devList[]=Device::RowToObject($row);
}
$this->MakeDisplay();
return $devList;
}
function UpdateDevices(){
/* This will cause every device with a TemplateID matching this one to display
the updated values. We are not touching DeviceType or NumPorts at this time
because those have alternate side effects that i'm not sure we really need
to address here
*/
$this->MakeSafe();
$sql="UPDATE fac_Device SET Height=$this->Height, NominalWatts=$this->Wattage,
PowerSupplyCount=$this->PSCount, ChassisSlots=$this->ChassisSlots, Weight=$this->Weight,
RearChassisSlots=$this->RearChassisSlots, SNMPVersion=\"$this->SNMPVersion\" WHERE TemplateID=$this->TemplateID;";
return $this->query($sql);
}
function DeleteSlots(){
$this->MakeSafe();
$sql="DELETE FROM fac_Slots WHERE TemplateID=$this->TemplateID";
if(!$this->query($sql)){
return false;
}
return true;
}
function DeletePorts(){
$this->MakeSafe();
$sql="DELETE FROM fac_TemplatePorts WHERE TemplateID=$this->TemplateID";
if(!$this->query($sql)){
return false;
}
return true;
}
function DeletePowerPorts(){
$this->MakeSafe();
$sql="DELETE FROM fac_TemplatePowerPorts WHERE TemplateID=$this->TemplateID";
if(!$this->query($sql)){
return false;
}
return true;
}
// This was a double of the DeletePorts function need to come back later
// and see if this thing is even being used.
function removePorts(){
return $this->DeletePorts();
}
function ExportTemplate(){
$this->MakeSafe();
//Get manufacturer name
$manufacturer=new Manufacturer();
$manufacturer->ManufacturerID=$this->ManufacturerID;
$manufacturer->GetManufacturerByID();
$fileContent='<?xml version="1.0" encoding="UTF-8"?>
<Template xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="openDCIMdevicetemplate.xsd">
<ManufacturerName>'.$manufacturer->Name.'</ManufacturerName>
<TemplateReg>
<Model>'.$this->Model.'</Model>
<Height>'.$this->Height.'</Height>
<Weight>'.$this->Weight.'</Weight>
<Wattage>'.$this->Wattage.'</Wattage>
<DeviceType>'.$this->DeviceType.'</DeviceType>
<PSCount>'.$this->PSCount.'</PSCount>
<NumPorts>'.$this->NumPorts.'</NumPorts>
<Notes>'.$this->Notes.'</Notes>
<FrontPictureFile>'.$this->FrontPictureFile.'</FrontPictureFile>
<RearPictureFile>'.$this->RearPictureFile.'</RearPictureFile>
<SNMPVersion>'.$this->SNMPVersion.'</SNMPVersion>
<ChassisSlots>'.$this->ChassisSlots.'</ChassisSlots>
<RearChassisSlots>'.$this->RearChassisSlots.'</RearChassisSlots>
</TemplateReg>';
//Slots
for ($i=1; $i<=$this->ChassisSlots;$i++){
$slot=new Slot();
$slot->TemplateID=$this->TemplateID;
$slot->Position=$i;
$slot->BackSide=False;
$slot->GetSlot();
$fileContent.='
<SlotReg>
<Position>'.$slot->Position.'</Position>
<BackSide>0</BackSide>
<X>'.$slot->X.'</X>
<Y>'.$slot->Y.'</Y>
<W>'.$slot->W.'</W>
<H>'.$slot->H.'</H>
</SlotReg>';
}
for ($i=1; $i<=$this->RearChassisSlots;$i++){
$slot=new Slot();
$slot->TemplateID=$this->TemplateID;
$slot->Position=$i;
$slot->BackSide=True;
$slot->GetSlot();
$fileContent.='
<SlotReg>
<Position>'.$slot->Position.'</Position>
<BackSide>1</BackSide>
<X>'.$slot->X.'</X>
<Y>'.$slot->Y.'</Y>
<W>'.$slot->W.'</W>
<H>'.$slot->H.'</H>
</SlotReg>';
}
//Ports
for ($i=1; $i<=$this->NumPorts;$i++){
$tport=new TemplatePorts();
$tport->TemplateID=$this->TemplateID;
$tport->PortNumber=$i;
$tport->GetPort();
//Get media name
$mt=new MediaTypes();
$mt->MediaID=$tport->MediaID;
$mt->GetType();
//Get color name
$cc=new ColorCoding();
$cc->ColorID=$tport->ColorID;
$cc->GetCode();
$fileContent.='
<PortReg>
<PortNumber>'.$tport->PortNumber.'</PortNumber>
<Label>'.$tport->Label.'</Label>
<PortMedia>'.$mt->MediaType.'</PortMedia>
<PortColor>'.$cc->Name.'</PortColor>
<PortNotes>'.$tport->PortNotes.'</PortNotes>
</PortReg>';
}
//Pictures
if ($this->FrontPictureFile!="" && file_exists("pictures/".$this->FrontPictureFile)){
$im=file_get_contents("pictures/".$this->FrontPictureFile);
$fileContent.='
<FrontPicture>
'.base64_encode($im).'
</FrontPicture>';
}
if ($this->RearPictureFile!="" && file_exists("pictures/".$this->RearPictureFile)){
$im=file_get_contents("pictures/".$this->RearPictureFile);
$fileContent.='
<RearPicture>
'.base64_encode($im).'
</RearPicture>';
}
//End of template
$fileContent.='
</Template>';
//download file
download_file_from_string($fileContent, str_replace(' ', '', $manufacturer->Name."-".$this->Model).".xml");
return true;
}
function ImportTemplate($file){
$result=array();
$result["status"]="";
$result["log"]=array();
$ierror=0;
//validate xml template file with openDCIMdevicetemplate.xsd
libxml_use_internal_errors(true);
$xml=new XMLReader();
$xml->open($file);
$resp=$xml->setSchema ("openDCIMdevicetemplate.xsd");
while (@$xml->read()) {}; // empty loop
$errors = libxml_get_errors();
if (count($errors)>0){
$result["status"]=__("No valid file");
foreach ($errors as $error) {
$result["log"][$ierror++]=$error->message;
}
return $result;
}
libxml_clear_errors();
$xml->close();
//read xml template file
$xmltemplate=simplexml_load_file($file);
//manufacturer
$manufacturer=new Manufacturer();
$manufacturer->Name=transform($xmltemplate->ManufacturerName);
if (!$manufacturer->GetManufacturerByName()){
//New Manufacturer
$manufacturer->CreateManufacturer();
}
$template=new DeviceTemplate();
$template->ManufacturerID=$manufacturer->ManufacturerID;
$template->Model=transform($xmltemplate->TemplateReg->Model);
$template->Height=$xmltemplate->TemplateReg->Height;
$template->Weight=$xmltemplate->TemplateReg->Weight;
$template->Wattage=$xmltemplate->TemplateReg->Wattage;
$template->DeviceType=$xmltemplate->TemplateReg->DeviceType;
$template->PSCount=$xmltemplate->TemplateReg->PSCount;
$template->NumPorts=$xmltemplate->TemplateReg->NumPorts;
$template->Notes=trim($xmltemplate->TemplateReg->Notes);
$template->Notes=($template->Notes=="<br>")?"":$template->Notes;
$template->FrontPictureFile=$xmltemplate->TemplateReg->FrontPictureFile;
$template->RearPictureFile=$xmltemplate->TemplateReg->RearPictureFile;
$template->SNMPVersion=$xmltemplate->TemplateReg->SNMPVersion;
$template->ChassisSlots=($template->DeviceType=="Chassis")?$xmltemplate->TemplateReg->ChassisSlots:0;
$template->RearChassisSlots=($template->DeviceType=="Chassis")?$xmltemplate->TemplateReg->RearChassisSlots:0;
//Check if picture files exist
if ($template->FrontPictureFile!="" && file_exists("pictures/".$template->FrontPictureFile)){
$result["status"]=__("Import Error");
$result["log"][0]= __("Front picture file already exists");
return $result;
}
if ($template->RearPictureFile!="" && file_exists("pictures/".$template->RearPictureFile)){
$result["status"]=__("Import Error");
$result["log"][0]= __("Rear picture file already exists");
return $result;
}
//create the template
if (!$template->CreateTemplate()){
$result["status"]=__("Import Error");
$result["log"][0]=__("An error has occurred creating the template.<br>Possibly there is already a template of the same manufacturer and model");
return $result;
}
//get template to this object
$this->TemplateID=$template->TemplateID;
$this->GetTemplateByID();
//slots
foreach ($xmltemplate->SlotReg as $xmlslot){
$slot=new Slot();
$slot->TemplateID=$this->TemplateID;
$slot->Position=intval($xmlslot->Position);
$slot->BackSide=intval($xmlslot->BackSide);
$slot->X=intval($xmlslot->X);
$slot->Y=intval($xmlslot->Y);
$slot->W=intval($xmlslot->W);
$slot->H=intval($xmlslot->H);
if (($slot->Position<=$this->ChassisSlots && !$slot->BackSide) || ($slot->Position<=$this->RearChassisSlots && $slot->BackSide)){
if(!$slot->CreateSlot()){
$result["status"]=__("Import Warning");
$result["log"][$ierror++]=sprintf(__("An error has occurred creating the slot %s"),$slot->Position."-".($slot->BackSide)?__("Rear"):__("Front"));
}
}else{
$result["status"]=__("Import Warning");
$result["log"][$ierror++]=sprintf(__("Ignored slot %s"),$slot->Position."-".($slot->BackSide)?__("Rear"):__("Front"));
}
}
//ports
foreach ($xmltemplate->PortReg as $xmlport){
//media type
$mt=new MediaTypes();
$mt->MediaType=transform($xmlport->PortMedia);
if (!$mt->GetTypeByName()){
//New media type
$mt->CreateType();
}
//color
$cc=new ColorCoding();
$cc->Name=transform($xmlport->PortColor);
if (!$cc->GetCodeByName()){
//New color
$cc->CreateCode();
}
$tport=new TemplatePorts();
$tport->TemplateID=$this->TemplateID;
$tport->PortNumber=intval($xmlport->PortNumber);
$tport->Label=$xmlport->Label;
$tport->MediaID=$mt->MediaID;
$tport->ColorID=$cc->ColorID;
$tport->PortNotes=$xmlport->PortNotes;
if ($tport->PortNumber<=$this->NumPorts){
if(!$tport->CreatePort()){
$result["status"]=__("Import Warning");
$result["log"][$ierror++]=sprintf(__("An error has occurred creating the port %s"),$tport->PortNumber);
}
}else{
$result["status"]=__("Import Warning");
$result["log"][$ierror++]=sprintf(__("Ignored port %s"),$tport->PortNumber);
}
}
//files
if($this->FrontPictureFile!=""){
$im=base64_decode($xmltemplate->FrontPicture);
file_put_contents("pictures/".$this->FrontPictureFile, $im);
}
if($this->RearPictureFile!="" && $this->RearPictureFile!=$this->FrontPictureFile){
$im=base64_decode($xmltemplate->RearPicture);
file_put_contents("pictures/".$this->RearPictureFile, $im);
}
return $result;
}
function GetCustomValues() {
$this->MakeSafe();
$tdca = array();
$sql = "SELECT TemplateID, AttributeID, Required, Value
FROM fac_DeviceTemplateCustomValue
WHERE TemplateID=$this->TemplateID;";
foreach($this->query($sql) as $tdcrow) {
$tdca[$tdcrow["AttributeID"]]["value"]=$tdcrow["Value"];
$tdca[$tdcrow["AttributeID"]]["required"]=$tdcrow["Required"];
}
$this->CustomValues = $tdca;
}
function DeleteCustomValues() {
$this->MakeSafe();
$sql="DELETE FROM fac_DeviceTemplateCustomValue WHERE TemplateID=$this->TemplateID;";
if($this->query($sql)){
$this->GetCustomValues();
return true;
}
return false;
}
function InsertCustomValue($AttributeID, $Value, $Required) {
$this->MakeSafe();
// make the custom attirubte stuff safe
$AttributeID=intval($AttributeID);
$Required=intval($Required);
$Value=sanitize(trim($Value));
$sql="INSERT INTO fac_DeviceTemplateCustomValue
SET TemplateID=$this->TemplateID,
AttributeID=$AttributeID,
Required=$Required,
Value=\"$Value\";";
if($this->query($sql)) {
$this->GetCustomValues();
return true;
}
return false;
}
static function getAvailableImages(){
$array=array();
$path='pictures';
if(preg_match('/api\//',str_replace(DIRECTORY_SEPARATOR, '/',getcwd()))){
$path="../../$path";
}
if(is_dir($path)){
$dir=scandir($path);
foreach($dir as $i => $f){
if(is_file($path.DIRECTORY_SEPARATOR.$f) && ($f!='.' && $f!='..' && $f!='P_ERROR.png')){
@$imageinfo=getimagesize($path.DIRECTORY_SEPARATOR.$f);
if(preg_match('/^image/i', $imageinfo['mime'])){
$array[]=$f;
}
}
}
}
return $array;
}
}
?>