a779d7552ab9029111ff48a44ef7b9beaf781736
1 <?php
3 class glpiDB{
5 var $user ="";
6 var $password ="";
7 var $server ="";
8 var $db ="";
10 var $is_connected = 0;
11 var $handle = NULL;
13 var $lasterror ="";
15 function glpiDB($server,$user,$pwd,$db){
16 $this->server = $server;
17 $this->user = $user;
18 $this->password = $pwd;
19 $this->db = $db;
21 $this->handle = @mysql_connect($this->server,$this->user,$this->password);
23 if($this->handle){
24 $this->is_connected = true;
25 $this->SelectDB($this->db);
26 }
27 }
29 function SelectDB()
30 {
31 if($this->is_connected){
32 mysql_select_db($this->db,$this->handle);
33 }
34 }
36 function is_account($dn)
37 {
38 if(!$this->is_connected){
39 $this->lasterror ="Can't query anything, if we aren't connected.";
40 return(false);
41 }else{
42 $qry = "SELECT * FROM glpi_computers WHERE name='".$dn."';";
43 $res = $this->query($qry);
44 if(count($res)==0){
45 return(false);
46 }else{
47 return(true);
48 }
49 }
50 }
52 function query($qry)
53 {
54 if(!$this->is_connected){
55 $this->lasterror ="Can't query anything, if we aren't connected.";
56 return(false);
57 }else{
58 $ret =array();
59 $res = mysql_query($qry,$this->handle);
61 while($rs = @mysql_fetch_array($res,MYSQL_ASSOC)){
62 $ret[]=$rs;
63 }
64 return($ret);
65 }
66 }
68 /* System types */
69 function getSystemTypes()
70 {
71 if($this->is_connected){
72 $ret = array();
73 $tmp = ($this->query("SELECT * FROM glpi_type_computers;"));
74 foreach($tmp as $t){
75 $ret[$t['ID']]=$t['name'];
76 }
77 return($ret);
78 }else{
79 echo "not connected";
80 return(false);
81 }
82 }
84 /* System types */
85 function updateSystemType($name,$id)
86 {
88 if($this->is_connected){
89 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
90 if(isset($tmp[0])){
91 return($this->query("UPDATE glpi_type_computers SET name='".$name."' WHERE ID=".$id.";"));
92 }else{
93 echo "can't update not existing entry";
94 return(false);
95 }
96 }else{
97 echo "not connected";
98 return(false);
99 }
100 }
102 /* System types */
103 function addSystemType($name)
104 {
105 if($this->is_connected){
106 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE name='".$name."';");
107 if(isset($tmp[0])){
108 echo "such an entry already exists";
109 return(false);
110 }else{
111 return($this->query("INSERT INTO glpi_type_computers (name) VALUES ('".$name."');"));
112 }
113 }else{
114 echo "not connected";
115 return(false);
116 }
117 }
119 /* System types */
120 function removeSystemType_byID($id)
121 {
122 if($this->is_connected){
123 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
124 if(isset($tmp[0])){
125 return($this->query("DELETE FROM glpi_type_computers WHERE ID=".$id.";"));
126 }else{
127 echo "can't remove not existing entry";
128 return(false);
129 }
130 }else{
131 echo "not connected";
132 return(false);
133 }
134 }
136 /* Manufacturer */
137 function getEnterprisesTypes()
138 {
139 if($this->is_connected){
140 $ret = array();
141 $tmp = $this->query("SELECT * FROM glpi_enterprises;");
142 foreach($tmp as $t){
143 $ret[$t['ID']]=$t['name'];
144 }
145 return($ret);
146 }else{
147 echo "not connected";
148 return(false);
149 }
150 }
152 /* Manufacturer */
153 function getEnterprise($id)
154 {
155 if($this->is_connected){
156 $ret = array();
157 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
158 return($tmp);
159 }else{
160 echo "not connected";
161 return(false);
162 }
163 }
165 /* System types */
166 function updateEnterprisesType($array,$id)
167 {
168 if(!is_array($array)){
169 echo "updateEnterprisesType: first paraeter must be an array";
170 }elseif($this->is_connected){
171 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID='".$id."';");
172 if(isset($tmp[0])){
173 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
175 $v = "";
176 foreach($atr as $at){
177 if(isset($array[$at])){
178 $v .= " ".$at."='".$array[$at]."', ";
179 }
180 }
181 if(empty($v)){
182 echo "updateEnterprisesType: no attributes given ";
183 return(false);
184 }else{
185 $v = preg_replace("/, $/","",$v);
186 return($this->query("UPDATE glpi_enterprises SET ".$v." WHERE ID='".$id."';"));
187 }
188 }else{
189 echo "can't update not existing entry";
190 return(false);
191 }
192 }else{
193 echo "not connected";
194 return(false);
195 }
196 }
198 function addEnterprisesType($array)
199 {
200 if(!is_array($array)){
201 echo "addUser: first paraeter must be an array";
202 }elseif($this->is_connected){
203 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
204 $v = "";
205 $a = "";
206 foreach($atr as $at){
207 if(isset($array[$at])){
208 $a .= $at.", ";
209 $v .= "'".$array[$at]."', ";
210 }
211 }
212 if(empty($v)){
213 echo "addUser: no attributes given ";
214 return(false);
215 }else{
216 $a = preg_replace("/, $/","",$a);
217 $v = preg_replace("/, $/","",$v);
218 return($this->query("INSERT INTO glpi_enterprises (".$a.") VALUES (".$v.");"));
219 }
221 }else{
222 echo "not connected";
223 return(false);
224 }
226 }
230 function removeEnterprisesType($id)
231 {
232 if($this->is_connected){
233 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
234 if(isset($tmp[0])){
235 return($this->query("DELETE FROM glpi_enterprises WHERE ID=".$id.";"));
236 }else{
237 echo "can't remove not existing entry";
238 return(false);
239 }
240 }else{
241 echo "not connected";
242 return(false);
243 }
244 }
247 /* Operating systems */
248 function getOSTypes()
249 {
250 if($this->is_connected){
251 $ret = array();
252 $tmp=($this->query("SELECT * FROM glpi_dropdown_os;"));
254 foreach($tmp as $t){
255 $ret[$t['ID']]=$t['name'];
256 }
258 return($ret);
260 }else{
261 echo "not connected";
262 return(false);
263 }
264 }
267 /* os */
268 function addOS($name)
269 {
270 if($this->is_connected){
271 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE name='".$name."';");
272 if(isset($tmp[0])){
273 echo "such an entry already exists";
274 return(false);
275 }else{
276 return($this->query("INSERT INTO glpi_dropdown_os (name) VALUES ('".$name."');"));
277 }
278 }else{
279 echo "not connected";
280 return(false);
281 }
282 }
284 /* System types */
285 function removeOS_byID($id)
286 {
287 if($this->is_connected){
288 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
289 if(is_array($tmp[0])){
290 return($this->query("DELETE FROM glpi_dropdown_os WHERE ID=".$id.";"));
291 }else{
292 echo "can't remove not existing entry";
293 return(false);
294 }
295 }else{
296 echo "not connected";
297 return(false);
298 }
299 }
303 /* System types */
304 function updateOS($name,$id)
305 {
307 if($this->is_connected){
308 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
309 if(isset($tmp[0])){
310 return($this->query("UPDATE glpi_dropdown_os SET name='".$name."' WHERE ID=".$id.";"));
311 }else{
312 echo "can't update not existing entry";
313 return(false);
314 }
315 }else{
316 echo "not connected";
317 return(false);
318 }
319 }
322 /* Technical / Responsible person / glpi users */
323 function getUsers()
324 {
325 if($this->is_connected){
326 $ret = array();
327 $tmp = ($this->query("SELECT * FROM glpi_users"));
328 foreach($tmp as $user){
329 $ret[$user['ID']]=$user['name'];
330 }
331 return($ret);
333 }else{
334 echo "not connected";
335 return(false);
336 }
337 }
340 function addUser($array,$dn)
341 {
342 if(!is_array($array)){
343 echo "addUser: first paraeter must be an array";
344 }elseif($this->is_connected){
345 $array['name']=$dn;
346 $atr = array("name","phone","email");
347 $v = "";
348 $a = "";
349 foreach($atr as $at){
350 if(isset($array[$at])){
351 $a .= $at.", ";
352 $v .= "'".$array[$at]."', ";
353 }
354 }
355 if(empty($v)){
356 echo "addUser: no attributes given ";
357 return(false);
358 }else{
359 $a = preg_replace("/, $/","",$a);
360 $v = preg_replace("/, $/","",$v);
361 return($this->query("INSERT INTO glpi_users (".$a.") VALUES (".$v.");"));
362 }
364 }else{
365 echo "not connected";
366 return(false);
367 }
369 }
371 function updateUser($array,$dn)
372 {
373 if(!is_array($array)){
374 echo "updateUser: first paraeter must be an array";
375 }elseif($this->is_connected){
376 $tmp = $this->query("SELECT * FROM glpi_users WHERE name='".$dn."';");
377 if(isset($tmp[0])){
379 $atr = array("name","phone","email");
380 $v = "";
381 foreach($atr as $at){
382 if(isset($array[$at])){
383 $v .= " ".$at."='".$array[$at]."', ";
384 }
385 }
386 if(empty($v)){
387 echo "UpdateUser: no attributes given ";
388 return(false);
389 }else{
390 $v = preg_replace("/, $/","",$v);
391 return($this->query("UPDATE glpi_users SET ".$v." WHERE name='".$dn."';"));
392 }
393 }else{
394 echo "can't update not existing entry";
395 return(false);
396 }
397 }else{
398 echo "not connected";
399 return(false);
400 }
402 }
405 function getComputerInformations($name)
406 {
407 if($this->is_connected){
408 /*
409 $ret = $this->query( '
410 SELECT
411 c.ID,
412 c.name as "Name",
413 c.comments as "Commets",
414 d.name as "OS",
415 e.name as "Manufacturer",
416 dc.name as "Type",
417 u.name as "Technical responsible"
418 FROM
419 glpi_computers as c
420 left join glpi_dropdown_os as d on (c.os = d.ID)
421 left join glpi_enterprises as e on (c.FK_glpi_enterprise = e.ID)
422 left join glpi_type_computers as dc on (c.type = dc.ID)
423 left join glpi_users as u on (c.tech_num = u.ID)
425 WHERE c.Name!="" AND c.ID='.$id.';');
426 */
427 $ret = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
428 return($ret);
429 }else{
430 echo "not connected";
431 return(false);
432 }
433 }
435 function updateComputerInformations($array,$name)
436 {
437 if(!is_array($array)){
438 echo "updateComputerInformations: first paraeter must be an array";
439 }elseif($this->is_connected){
440 $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
441 if(isset($tmp[0])){
443 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
444 "tech_num","comments","date_mod","os","location","domain","network",
445 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
446 $v = "";
447 foreach($atr as $at){
448 if(isset($array[$at])){
449 $v .= " ".$at."='".$array[$at]."', ";
450 }
451 }
452 if(empty($v)){
453 echo "updateComputerInformations: no attributes given ";
454 return(false);
455 }else{
456 $v = preg_replace("/, $/","",$v);
457 return($this->query("UPDATE glpi_computers SET ".$v." WHERE name='".$name."';"));
458 }
459 }else{
460 echo "can't update not existing entry";
461 return(false);
462 }
463 }else{
464 echo "not connected";
465 return(false);
466 }
468 }
470 function addComputerInformations($array)
471 {
472 if(!is_array($array)){
473 echo "updateComputerInformations: first paraeter must be an array";
474 }elseif($this->is_connected){
475 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
476 "tech_num","comments","date_mod","os","location","domain","network",
477 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
478 $v = "";
479 $a = "";
480 foreach($atr as $at){
481 if(isset($array[$at])){
482 $a .= $at.", ";
483 $v .= "'".$array[$at]."', ";
484 }
485 }
486 if(empty($v)){
487 echo "updateComputerInformations: no attributes given ";
488 return(false);
489 }else{
490 $a = preg_replace("/, $/","",$a);
491 $v = preg_replace("/, $/","",$v);
492 return($this->query("INSERT INTO glpi_computers (".$a.") VALUES (".$v.");"));
493 }
495 }else{
496 echo "not connected";
497 return(false);
498 }
500 }
502 function deviceExists($attr)
503 {
504 if($this->is_connected){
505 $arr = array_flip(array( "glpi_device_case" => "case",
506 "glpi_device_control" => "control",
507 "glpi_device_drive" => "drive",
508 "glpi_device_gfxcard" => "gfxcard",
509 "glpi_device_hdd" => "hdd",
510 "glpi_device_iface" => "iface",
511 "glpi_device_moboard" => "moboard",
512 "glpi_device_pci" => "pci",
513 "glpi_device_power" => "power",
514 "glpi_device_processor" => "processor",
515 "glpi_device_ram" => "ram",
516 "glpi_device_sndcard" => "sndcard"));
518 $tbl_name = $arr[$attr['device_type']];
519 if(!isset($attr['ID'])){
520 return(false);
521 }else{
522 $qry = "SELECT * FROM ".$tbl_name." WHERE ID=".$attr['ID'].";";
523 $res = $this->query($qry);
524 if(count($res) != 0){
525 return(true);
526 }
527 }
528 }else{
529 echo "not connected";
530 return(false);
531 }
533 return(false);
534 }
536 function deleteDevice($attr)
537 {
538 if($this->is_connected){
539 $arr = array_flip(array( "glpi_device_case" => "case",
540 "glpi_device_control" => "control",
541 "glpi_device_drive" => "drive",
542 "glpi_device_gfxcard" => "gfxcard",
543 "glpi_device_hdd" => "hdd",
544 "glpi_device_iface" => "iface",
545 "glpi_device_moboard" => "moboard",
546 "glpi_device_pci" => "pci",
547 "glpi_device_power" => "power",
548 "glpi_device_processor" => "processor",
549 "glpi_device_ram" => "ram",
550 "glpi_device_sndcard" => "sndcard"));
552 $device_type = $attr['device_type'];
553 unset($attr['device_type']);
555 $tbl_name = $arr[$device_type];
557 $this->query("DELETE FROM ".$tbl_name." WHERE ID=".$attr['ID'].";");
558 }else{
559 echo "not connected";
560 return(false);
561 }
562 }
563 function updateDevices($attr)
564 {
565 if($this->is_connected){
566 $arr = array_flip(array( "glpi_device_case" => "case",
567 "glpi_device_control" => "control",
568 "glpi_device_drive" => "drive",
569 "glpi_device_gfxcard" => "gfxcard",
570 "glpi_device_hdd" => "hdd",
571 "glpi_device_iface" => "iface",
572 "glpi_device_moboard" => "moboard",
573 "glpi_device_pci" => "pci",
574 "glpi_device_power" => "power",
575 "glpi_device_processor" => "processor",
576 "glpi_device_ram" => "ram",
577 "glpi_device_sndcard" => "sndcard"));
579 $device_type = $attr['device_type'];
580 unset($attr['device_type']);
582 $tbl_name = $arr[$device_type];
584 $str = "UPDATE ".$tbl_name." SET ";
585 foreach($attr as $name => $value){
586 $str.=$name."='".$value."', ";
587 }
588 $str = preg_replace("/, $/","",$str);
589 $str .= " WHERE ID=".$attr['ID'].";";
590 $this->query($str);
591 }else{
592 echo "not connected";
593 return(false);
594 }
595 }
597 function getRAMTypes()
598 {
599 if($this->is_connected){
600 $ret = array();
601 $tmp = ($this->query("SELECT * FROM glpi_dropdown_ram_type;"));
602 foreach($tmp as $t){
603 $ret[$t['ID']]=$t['name'];
604 }
605 return($ret);
606 }else{
607 echo "not connected";
608 return(false);
609 }
610 }
613 function getGlpiDeviceControlTypes()
614 {
615 if($this->is_connected){
616 $ret = array();
617 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
618 foreach($tmp as $t){
619 $ret[$t['ID']]=$t['name'];
620 }
621 return($ret);
622 }else{
623 echo "not connected";
624 return(false);
625 }
626 }
628 function getGlpiGfxControlTypes()
629 {
630 if($this->is_connected){
631 $ret = array();
632 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
633 foreach($tmp as $t){
634 $ret[$t['ID']]=$t['name'];
635 }
636 return($ret);
637 }else{
638 echo "not connected";
639 return(false);
640 }
641 }
644 function addDevice($attr)
645 {
646 if($this->is_connected){
647 $arr = array_flip(array( "glpi_device_case" => "case",
648 "glpi_device_control" => "control",
649 "glpi_device_drive" => "drive",
650 "glpi_device_gfxcard" => "gfxcard",
651 "glpi_device_hdd" => "hdd",
652 "glpi_device_iface" => "iface",
653 "glpi_device_moboard" => "moboard",
654 "glpi_device_pci" => "pci",
655 "glpi_device_power" => "power",
656 "glpi_device_processor" => "processor",
657 "glpi_device_ram" => "ram",
658 "glpi_device_sndcard" => "sndcard"));
660 $device_type = $attr['device_type'];
661 unset($attr['device_type']);
663 $tbl_name = $arr[$device_type];
664 $v = "";
665 $a = "";
666 foreach($attr as $name => $value){
667 $a .= $name.", ";
668 $v .= "'".$value."', ";
669 }
670 if(empty($v)){
671 echo "addDevice: no attributes given ";
672 return(false);
673 }else{
674 $a = preg_replace("/, $/","",$a);
675 $v = preg_replace("/, $/","",$v);
676 return($this->query("INSERT INTO ".$tbl_name." (".$a.") VALUES (".$v.");"));
677 }
679 }else{
680 echo "not connected";
681 return(false);
682 }
683 }
685 function getDevices()
686 {
687 if($this->is_connected){
688 $arr = array( "glpi_device_case" => "case",
689 "glpi_device_control" => "control",
690 "glpi_device_drive" => "drive",
691 "glpi_device_gfxcard" => "gfxcard",
692 "glpi_device_hdd" => "hdd",
693 "glpi_device_iface" => "iface",
694 "glpi_device_moboard" => "moboard",
695 "glpi_device_pci" => "pci",
696 "glpi_device_power" => "power",
697 "glpi_device_processor" => "processor",
698 "glpi_device_ram" => "ram",
699 "glpi_device_sndcard" => "sndcard");
701 $res = array();
702 foreach($arr as $glpi => $gosa){
703 $qry = "SELECT * FROM ".$glpi.";";
704 $ret = $this->query($qry);
705 foreach($ret as $id => $entry){
706 $entry['device_type'] = $gosa;
707 $res[$entry['designation']."-".$gosa] = $entry;
708 }
709 }
710 return($res);
711 }else{
712 echo "not connected";
713 return(false);
714 }
715 }
717 function removeComputerInformations($id)
718 {
719 /*
720 if($this->is_connected){
721 $tmp = $this->query("SELECT * FROM glpi_computers WHERE ID=".$id.";");
722 if(isset($tmp[0])){
723 return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
724 }else{
725 echo "can't remove not existing entry";
726 return(false);
727 }
728 }else{
729 echo "not connected";
730 return(false);
731 }
732 */
733 }
735 function is_connected()
736 {
737 return($this->is_connected);
738 }
740 }
741 //$s = new glpiDB("vserver-01","glpi","tester","glpi");
742 //print_r($s->query("SELECT * FROM glpi_computers"));
743 //$s->getComputerInformations("1 OR (c.ID<10000)");
744 ?>