6c390cd43f1bd7571d445809b17135501547aeea
1 <?php
4 class glpiDB{
6 var $user ="";
7 var $password ="";
8 var $server ="";
9 var $db ="";
11 var $is_connected = 0;
12 var $handle = NULL;
14 var $lasterror ="";
16 var $deviceMappingGOsaGlpi;
17 var $deviceMappingTableNameID;
19 function glpiDB($server,$user,$pwd,$db){
20 $this->server = $server;
21 $this->user = $user;
22 $this->password = $pwd;
23 $this->db = $db;
25 $this->handle = @mysql_connect($this->server,$this->user,$this->password);
27 if($this->handle){
28 $this->is_connected = true;
29 $this->SelectDB($this->db);
30 }
31 $this->deviceMappingGOsaGlpi = array(
32 "glpi_device_case" => "case",
33 "glpi_device_control" => "control",
34 "glpi_device_drive" => "drive",
35 "glpi_device_gfxcard" => "gfxcard",
36 "glpi_device_hdd" => "hdd",
37 "glpi_device_iface" => "iface",
38 "glpi_device_moboard" => "moboard",
39 "glpi_device_pci" => "pci",
40 "glpi_device_power" => "power",
41 "glpi_device_processor" => "processor",
42 "glpi_device_ram" => "ram",
43 "glpi_device_sndcard" => "sndcard");
45 $this->deviceMappingTableNameID = array( "moboard" => 1,
46 "processor" => 2,
47 "ram" => 3,
48 "hdd" => 4,
49 "iface" => 5,
50 "drive" => 6,
51 "control" => 7,
52 "gfxcard" => 8,
53 "sndcard" => 9,
54 "pci" => 10,
55 "case" => 11,
56 "power" => 12);
59 }
61 function SelectDB()
62 {
63 if($this->is_connected){
64 mysql_select_db($this->db,$this->handle);
65 }
66 }
69 /* This functions checks if the selected computer/network
70 device is already available in the db
71 */
72 function is_account($dn)
73 {
74 if(!$this->is_connected){
75 $this->lasterror ="Can't query anything, if we aren't connected.";
76 return(false);
77 }else{
78 $qry = "SELECT * FROM glpi_computers WHERE name='".$dn."';";
79 $res = $this->query($qry);
80 if(count($res)==0){
81 return(false);
82 }else{
83 return(true);
84 }
85 }
86 }
88 /* this function queries everything
89 */
90 function query($qry)
91 {
92 if(!$this->is_connected){
93 $this->lasterror ="Can't query anything, if we aren't connected.";
94 return(false);
95 }else{
96 $ret =array();
97 $res = mysql_query($qry,$this->handle);
99 while($rs = @mysql_fetch_array($res,MYSQL_ASSOC)){
100 $ret[]=$rs;
101 }
102 return($ret);
103 }
104 }
106 /* System types
107 Returns all defined system types
108 */
109 function getSystemTypes()
110 {
111 if($this->is_connected){
112 $ret = array();
113 $tmp = ($this->query("SELECT * FROM glpi_type_computers;"));
114 foreach($tmp as $t){
115 $ret[$t['ID']]=$t['name'];
116 }
117 return($ret);
118 }else{
119 echo "not connected";
120 return(false);
121 }
122 }
124 /* System types
125 Update a system type
126 */
127 function updateSystemType($name,$id)
128 {
130 if($this->is_connected){
131 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
132 if(isset($tmp[0])){
133 return($this->query("UPDATE glpi_type_computers SET name='".$name."' WHERE ID=".$id.";"));
134 }else{
135 echo "can't update not existing entry";
136 return(false);
137 }
138 }else{
139 echo "not connected";
140 return(false);
141 }
142 }
144 /* System types
145 Add one entry to the system types
146 */
147 function addSystemType($name)
148 {
149 if($this->is_connected){
150 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE name='".$name."';");
151 if(isset($tmp[0])){
152 echo "such an entry already exists";
153 return(false);
154 }else{
155 return($this->query("INSERT INTO glpi_type_computers (name) VALUES ('".$name."');"));
156 }
157 }else{
158 echo "not connected";
159 return(false);
160 }
161 }
163 /* System types
164 Remove one entry from the system types (specified by ID=$id)
165 */
166 function removeSystemType($id)
167 {
168 if($this->is_connected){
169 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
170 if(isset($tmp[0])){
171 return($this->query("DELETE FROM glpi_type_computers WHERE ID=".$id.";"));
172 }else{
173 echo "can't remove not existing entry";
174 return(false);
175 }
176 }else{
177 echo "not connected";
178 return(false);
179 }
180 }
182 /* Manufacturer
183 Returns all defined manufacturers
184 */
185 function getEnterprises()
186 {
187 if($this->is_connected){
188 $ret = array();
189 $tmp = $this->query("SELECT * FROM glpi_enterprises;");
190 foreach($tmp as $t){
191 $ret[$t['ID']]=$t['name'];
192 }
193 return($ret);
194 }else{
195 echo "not connected";
196 return(false);
197 }
198 }
200 /* Manufacturer
201 Returns single manufacturer
202 */
203 function getEnterprise($id)
204 {
205 if($this->is_connected){
206 $ret = array();
207 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
208 return($tmp);
209 }else{
210 echo "not connected";
211 return(false);
212 }
213 }
215 /* Manufacturer
216 Updates already existing manufacturer
217 */
218 function updateEnterprise($array,$id)
219 {
220 if(!is_array($array)){
221 echo "updateEnterprisesType: first paraeter must be an array";
222 }elseif($this->is_connected){
223 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID='".$id."';");
224 if(isset($tmp[0])){
225 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
227 $v = "";
228 foreach($atr as $at){
229 if(isset($array[$at])){
230 $v .= " ".$at."='".$array[$at]."', ";
231 }
232 }
233 if(empty($v)){
234 echo "updateEnterprisesType: no attributes given ";
235 return(false);
236 }else{
237 $v = preg_replace("/, $/","",$v);
238 return($this->query("UPDATE glpi_enterprises SET ".$v." WHERE ID='".$id."';"));
239 }
240 }else{
241 echo "can't update not existing entry";
242 return(false);
243 }
244 }else{
245 echo "not connected";
246 return(false);
247 }
248 }
250 /* Manufacturer
251 Add new manufacturer
252 */
253 function addEnterprise($array)
254 {
255 if(!is_array($array)){
256 echo "addUser: first paraeter must be an array";
257 }elseif($this->is_connected){
258 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
259 $v = "";
260 $a = "";
261 foreach($atr as $at){
262 if(isset($array[$at])){
263 $a .= $at.", ";
264 $v .= "'".$array[$at]."', ";
265 }
266 }
267 if(empty($v)){
268 echo "addUser: no attributes given ";
269 return(false);
270 }else{
271 $a = preg_replace("/, $/","",$a);
272 $v = preg_replace("/, $/","",$v);
273 return($this->query("INSERT INTO glpi_enterprises (".$a.") VALUES (".$v.");"));
274 }
276 }else{
277 echo "not connected";
278 return(false);
279 }
281 }
283 /* Manufacturer
284 remove manufacturer
285 */
286 function removeEnterprise($id)
287 {
288 if($this->is_connected){
289 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
290 if(isset($tmp[0])){
291 return($this->query("DELETE FROM glpi_enterprises WHERE ID=".$id.";"));
292 }else{
293 echo "can't remove not existing entry";
294 return(false);
295 }
296 }else{
297 echo "not connected";
298 return(false);
299 }
300 }
302 /* Operating systems
303 Returns all OSs
304 */
305 function getOSTypes()
306 {
307 if($this->is_connected){
308 $ret = array();
309 $tmp=($this->query("SELECT * FROM glpi_dropdown_os;"));
311 foreach($tmp as $t){
312 $ret[$t['ID']]=$t['name'];
313 }
315 return($ret);
317 }else{
318 echo "not connected";
319 return(false);
320 }
321 }
323 /* Operating systems
324 Add a new operating system to the dropdown menus
325 */
326 function addOS($name)
327 {
328 if($this->is_connected){
329 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE name='".$name."';");
330 if(isset($tmp[0])){
331 echo "such an entry already exists";
332 return(false);
333 }else{
334 return($this->query("INSERT INTO glpi_dropdown_os (name) VALUES ('".$name."');"));
335 }
336 }else{
337 echo "not connected";
338 return(false);
339 }
340 }
342 /* Operating systems
343 remove one OS entry
344 */
345 function removeOS_byID($id)
346 {
347 if($this->is_connected){
348 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
349 if(is_array($tmp[0])){
350 return($this->query("DELETE FROM glpi_dropdown_os WHERE ID=".$id.";"));
351 }else{
352 echo "can't remove not existing entry";
353 return(false);
354 }
355 }else{
356 echo "not connected";
357 return(false);
358 }
359 }
361 /* Operating systems
362 Update existing OS entry
363 */
364 function updateOS($name,$id)
365 {
367 if($this->is_connected){
368 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
369 if(isset($tmp[0])){
370 return($this->query("UPDATE glpi_dropdown_os SET name='".$name."' WHERE ID=".$id.";"));
371 }else{
372 echo "can't update not existing entry";
373 return(false);
374 }
375 }else{
376 echo "not connected";
377 return(false);
378 }
379 }
381 /* This returns all available glpi users
382 */
383 function getUsers()
384 {
385 if($this->is_connected){
386 $ret = array();
387 $tmp = ($this->query("SELECT * FROM glpi_users"));
388 foreach($tmp as $user){
389 $ret[$user['ID']]=$user['name'];
390 }
391 return($ret);
393 }else{
394 echo "not connected";
395 return(false);
396 }
397 }
399 /* this function adds a new glpi user
400 */
401 function addUser($array,$dn)
402 {
403 if(!is_array($array)){
404 echo "addUser: first paraeter must be an array";
405 }elseif($this->is_connected){
406 $array['name']=$dn;
407 $atr = array("name","phone","email");
408 $v = "";
409 $a = "";
410 foreach($atr as $at){
411 if(isset($array[$at])){
412 $a .= $at.", ";
413 $v .= "'".$array[$at]."', ";
414 }
415 }
416 if(empty($v)){
417 echo "addUser: no attributes given ";
418 return(false);
419 }else{
420 $a = preg_replace("/, $/","",$a);
421 $v = preg_replace("/, $/","",$v);
422 return($this->query("INSERT INTO glpi_users (".$a.") VALUES (".$v.");"));
423 }
425 }else{
426 echo "not connected";
427 return(false);
428 }
430 }
432 /* This function updates a glpi user
433 with the given data
434 */
435 function updateUser($array,$dn)
436 {
437 if(!is_array($array)){
438 echo "updateUser: first paraeter must be an array";
439 }elseif($this->is_connected){
440 $tmp = $this->query("SELECT * FROM glpi_users WHERE name='".$dn."';");
441 if(isset($tmp[0])){
443 $atr = array("name","phone","email");
444 $v = "";
445 foreach($atr as $at){
446 if(isset($array[$at])){
447 $v .= " ".$at."='".$array[$at]."', ";
448 }
449 }
450 if(empty($v)){
451 echo "UpdateUser: no attributes given ";
452 return(false);
453 }else{
454 $v = preg_replace("/, $/","",$v);
455 return($this->query("UPDATE glpi_users SET ".$v." WHERE name='".$dn."';"));
456 }
457 }else{
458 echo "can't update not existing entry";
459 return(false);
460 }
461 }else{
462 echo "not connected";
463 return(false);
464 }
466 }
468 /* This function returns all available data
469 from a specified dn
470 */
471 function getComputerInformations($name)
472 {
473 if($this->is_connected){
474 $ret = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
475 return($ret);
476 }else{
477 echo "not connected";
478 return(false);
479 }
480 }
482 /* This fucntions updates an already existing entry
483 */
484 function updateComputerInformations($array,$name)
485 {
486 if(!is_array($array)){
487 echo "updateComputerInformations: first paraeter must be an array";
488 }elseif($this->is_connected){
489 $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
490 if(isset($tmp[0])){
492 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
493 "tech_num","comments","date_mod","os","location","domain","network",
494 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
495 $v = "";
496 foreach($atr as $at){
497 if(isset($array[$at])){
498 $v .= " ".$at."='".$array[$at]."', ";
499 }
500 }
501 if(empty($v)){
502 echo "updateComputerInformations: no attributes given ";
503 return(false);
504 }else{
505 $v = preg_replace("/, $/","",$v);
506 return($this->query("UPDATE glpi_computers SET ".$v." WHERE name='".$name."';"));
507 }
508 }else{
509 echo "can't update not existing entry";
510 return(false);
511 }
512 }else{
513 echo "not connected";
514 return(false);
515 }
517 }
519 /* This function adds a new inventory device (computer phone etc)
520 */
521 function addComputerInformations($array)
522 {
523 if(!is_array($array)){
524 echo "updateComputerInformations: first paraeter must be an array";
525 }elseif($this->is_connected){
526 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
527 "tech_num","comments","date_mod","os","location","domain","network",
528 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
529 $v = "";
530 $a = "";
531 foreach($atr as $at){
532 if(isset($array[$at])){
533 $a .= $at.", ";
534 $v .= "'".$array[$at]."', ";
535 }
536 }
537 if(empty($v)){
538 echo "updateComputerInformations: no attributes given ";
539 return(false);
540 }else{
541 $a = preg_replace("/, $/","",$a);
542 $v = preg_replace("/, $/","",$v);
543 return($this->query("INSERT INTO glpi_computers (".$a.") VALUES (".$v.");"));
544 }
546 }else{
547 echo "not connected";
548 return(false);
549 }
551 }
553 /* this functions checks if the given Device
554 * already exists
555 */
556 function deviceExists($attr)
557 {
558 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
559 if($this->is_connected){
560 $arr = array_flip($deviceMappingGOsaGlpi);
562 $tbl_name = $arr[$attr['device_type']];
563 if(!isset($attr['ID'])){
564 return(false);
565 }else{
566 $qry = "SELECT * FROM ".$tbl_name." WHERE ID=".$attr['ID'].";";
567 $res = $this->query($qry);
568 if(count($res) != 0){
569 return(true);
570 }
571 }
572 }else{
573 echo "not connected";
574 return(false);
575 }
577 return(false);
578 }
580 /* This functions deletes a specified entry
581 * from our device tables
582 */
583 function deleteDevice($attr)
584 {
585 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
586 if($this->is_connected){
587 $arr = array_flip($deviceMappingGOsaGlpi);
589 $device_type = $attr['device_type'];
590 unset($attr['device_type']);
592 $tbl_name = $arr[$device_type];
594 $this->query("DELETE FROM ".$tbl_name." WHERE ID=".$attr['ID'].";");
595 }else{
596 echo "not connected";
597 return(false);
598 }
599 }
601 /* This funtions updated an already existing device
602 */
603 function updateDevices($attr)
604 {
605 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
606 if($this->is_connected){
607 $arr = array_flip($deviceMappingGOsaGlpi);
609 $device_type = $attr['device_type'];
610 unset($attr['device_type']);
612 $tbl_name = $arr[$device_type];
614 $str = "UPDATE ".$tbl_name." SET ";
615 foreach($attr as $name => $value){
616 $str.=$name."='".$value."', ";
617 }
618 $str = preg_replace("/, $/","",$str);
619 $str .= " WHERE ID=".$attr['ID'].";";
620 $this->query($str);
621 }else{
622 echo "not connected";
623 return(false);
624 }
625 }
627 /* Returns all possible RAM types
628 * like SDRAM , DIMM .....
629 */
630 function getRAMTypes()
631 {
632 if($this->is_connected){
633 $ret = array();
634 $tmp = ($this->query("SELECT * FROM glpi_dropdown_ram_type;"));
635 foreach($tmp as $t){
636 $ret[$t['ID']]=$t['name'];
637 }
638 return($ret);
639 }else{
640 echo "not connected";
641 return(false);
642 }
643 }
645 /* Returns all possible HDD connection types
646 * like IDE SCSI ...
647 */
648 function getGlpiDeviceControlTypes()
649 {
650 if($this->is_connected){
651 $ret = array();
652 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
653 foreach($tmp as $t){
654 $ret[$t['ID']]=$t['name'];
655 }
656 return($ret);
657 }else{
658 echo "not connected";
659 return(false);
660 }
661 }
663 /* Returns all possible gfx card connection types
664 * like PCI-X PCI AGP ....
665 */
666 function getGlpiGfxControlTypes()
667 {
668 if($this->is_connected){
669 $ret = array();
670 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
671 foreach($tmp as $t){
672 $ret[$t['ID']]=$t['name'];
673 }
674 return($ret);
675 }else{
676 echo "not connected";
677 return(false);
678 }
679 }
681 /* Devices
682 Adds a new single device to our db
683 */
684 function addDevice($attr)
685 {
686 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
687 if($this->is_connected){
688 $arr = array_flip($deviceMappingGOsaGlpi);
690 $device_type = $attr['device_type'];
691 unset($attr['device_type']);
693 $tbl_name = $arr[$device_type];
694 $v = "";
695 $a = "";
696 foreach($attr as $name => $value){
697 $a .= $name.", ";
698 $v .= "'".$value."', ";
699 }
700 if(empty($v)){
701 echo "addDevice: no attributes given ";
702 return(false);
703 }else{
704 $a = preg_replace("/, $/","",$a);
705 $v = preg_replace("/, $/","",$v);
706 return($this->query("INSERT INTO ".$tbl_name." (".$a.") VALUES (".$v.");"));
707 }
709 }else{
710 echo "not connected";
711 return(false);
712 }
713 }
715 /* Return all available devices
716 */
717 function getDevices()
718 {
719 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
720 if($this->is_connected){
721 $arr = $deviceMappingGOsaGlpi;
723 $res = array();
724 foreach($arr as $glpi => $gosa){
725 $qry = "SELECT * FROM ".$glpi.";";
726 $ret = $this->query($qry);
727 foreach($ret as $id => $entry){
728 $entry['device_type'] = $gosa;
729 $res[$entry['designation']."-".$gosa] = $entry;
730 }
731 }
732 return($res);
733 }else{
734 echo "not connected";
735 return(false);
736 }
737 }
739 /* This function returns all used devices
740 */
741 function getUsedDevices($computerID)
742 {
743 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
744 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
746 if($this->is_connected){
747 $qry = "SELECT * FROM glpi_computer_device WHERE FK_computers=".$computerID.";";
748 $res = $this->query($qry);
750 $ret = array();
751 foreach($deviceMappingGOsaGlpi as $GOsa => $glpi){
752 $ret[$GOsa] = array();
753 }
755 $tbls = array_flip($deviceMappingTableNameID);
757 foreach($res as $device){
758 $devtype = $tbls[$device['device_type']];
759 $tbl_name = $deviceMappingGOsaGlpi[$devtype];
760 $qry = ("SELECT * FROM ".$tbl_name." WHERE ID=".$device['FK_device'].";");
761 $res2 = $this->query($qry);
762 if(count($res2)!=0){
763 $ret[$devtype][$res2[0]['designation']]=$res2[0];
764 }
765 }
766 return($ret);
767 }else{
768 echo "not connected";
769 return(false);
770 }
771 }
773 /* This function removes all given devices from a computer, specified by $id
774 In the next step all devices specified by devices will be added.
775 */
776 function addDevicesToComputer($devices, $id)
777 {
778 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
779 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
781 if(($id == "" )||(!is_numeric($id))){
782 return (false);
783 }
784 if($this->is_connected){
785 $qry = "DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";";
786 $this->query($qry);
788 foreach($devices as $type => $entries){
789 foreach($entries as $entry){
790 $str = "INSERT INTO glpi_computer_device (device_type,FK_device,FK_computers)
791 VALUES (".$deviceMappingTableNameID[$type].",".$entry['ID'].",".$id.");";
792 $this->query($str);
793 }
794 }
797 }else{
798 echo "not connected";
799 return(false);
800 }
802 }
804 function removeComputerInformations($id)
805 {
806 /*
807 if($this->is_connected){
808 $tmp = $this->query("SELECT * FROM glpi_computers WHERE ID=".$id.";");
809 if(isset($tmp[0])){
810 return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
811 }else{
812 echo "can't remove not existing entry";
813 return(false);
814 }
815 }else{
816 echo "not connected";
817 return(false);
818 }
819 */
820 }
822 function is_connected()
823 {
824 return($this->is_connected);
825 }
827 }
828 //$s = new glpiDB("vserver-01","glpi","tester","glpi");
829 //print_r($s->query("SELECT * FROM glpi_computers"));
830 //$s->getComputerInformations("1 OR (c.ID<10000)");
831 ?>