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_monitors" => "monitor",
44 "glpi_device_sndcard" => "sndcard");
46 $this->deviceMappingTableNameID = array( "moboard" => 1,
47 "processor" => 2,
48 "ram" => 3,
49 "hdd" => 4,
50 "iface" => 5,
51 "drive" => 6,
52 "control" => 7,
53 "gfxcard" => 8,
54 "sndcard" => 9,
55 "pci" => 10,
56 "case" => 11,
57 "power" => 12);
60 }
62 function SelectDB()
63 {
64 if($this->is_connected){
65 mysql_select_db($this->db,$this->handle);
66 }
67 }
70 /* This functions checks if the selected computer/network
71 device is already available in the db
72 */
73 function is_account($dn)
74 {
75 if(!$this->is_connected){
76 $this->lasterror ="Can't query anything, if we aren't connected.";
77 return(false);
78 }else{
79 $qry = "SELECT * FROM glpi_computers WHERE name='".$dn."';";
80 $res = $this->query($qry);
81 if(count($res)==0){
82 return(false);
83 }else{
84 return(true);
85 }
86 }
87 }
89 /* this function queries everything
90 */
91 function query($qry)
92 {
93 if(!$this->is_connected){
94 $this->lasterror ="Can't query anything, if we aren't connected.";
95 return(false);
96 }else{
97 $ret =array();
98 $res = mysql_query($qry,$this->handle);
100 while($rs = @mysql_fetch_array($res,MYSQL_ASSOC)){
101 $ret[]=$rs;
102 }
103 return($ret);
104 }
105 }
107 /* System types
108 Returns all defined system types
109 */
110 function getSystemTypes()
111 {
112 if($this->is_connected){
113 $ret = array();
114 $tmp = ($this->query("SELECT * FROM glpi_type_computers;"));
115 foreach($tmp as $t){
116 $ret[$t['ID']]=$t['name'];
117 }
118 sort($ret);
119 return($ret);
120 }else{
121 echo "not connected";
122 return(false);
123 }
124 }
126 /* System types
127 Update a system type
128 */
129 function updateSystemType($name,$id)
130 {
132 if($this->is_connected){
133 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
134 if(isset($tmp[0])){
135 return($this->query("UPDATE glpi_type_computers SET name='".$name."' WHERE ID=".$id.";"));
136 }else{
137 echo "can't update not existing entry";
138 return(false);
139 }
140 }else{
141 echo "not connected";
142 return(false);
143 }
144 }
146 /* System types
147 Add one entry to the system types
148 */
149 function addSystemType($name)
150 {
151 if($this->is_connected){
152 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE name='".$name."';");
153 if(isset($tmp[0])){
154 echo "such an entry already exists";
155 return(false);
156 }else{
157 return($this->query("INSERT INTO glpi_type_computers (name) VALUES ('".$name."');"));
158 }
159 }else{
160 echo "not connected";
161 return(false);
162 }
163 }
165 /* System types
166 Remove one entry from the system types (specified by ID=$id)
167 */
168 function removeSystemType($id)
169 {
170 if($this->is_connected){
171 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
172 if(isset($tmp[0])){
173 return($this->query("DELETE FROM glpi_type_computers WHERE ID=".$id.";"));
174 }else{
175 echo "can't remove not existing entry";
176 return(false);
177 }
178 }else{
179 echo "not connected";
180 return(false);
181 }
182 }
184 /* System type is used */
185 function is_systemTypeUsed($ID){
186 if($this->is_connected){
187 $ret = array();
188 $qry="SELECT name,type FROM glpi_computers WHERE type=".$ID." LIMIT 3;";
189 $res = $this->query($qry);
190 foreach($res as $val){
191 $ret[$val['name']] = $val['name'];
192 }
193 return($ret);
194 }else{
195 echo "not connected";
196 return(false);
197 }
198 }
201 /* Manufacturer
202 Returns all defined manufacturers
203 */
204 function getEnterprises()
205 {
206 if($this->is_connected){
207 $ret = array();
208 $tmp = $this->query("SELECT * FROM glpi_enterprises;");
209 foreach($tmp as $t){
210 $ret[$t['ID']]=$t['name'];
211 }
212 return($ret);
213 }else{
214 echo "not connected";
215 return(false);
216 }
217 }
219 /* Manufacturer
220 Returns single manufacturer
221 */
222 function getEnterprise($id)
223 {
224 if($this->is_connected){
225 $ret = array();
226 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
227 return($tmp);
228 }else{
229 echo "not connected";
230 return(false);
231 }
232 }
234 /* Manufacturer
235 Updates already existing manufacturer
236 */
237 function updateEnterprise($array,$id)
238 {
239 if(!is_array($array)){
240 echo "updateEnterprisesType: first paraeter must be an array";
241 }elseif($this->is_connected){
242 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID='".$id."';");
243 if(isset($tmp[0])){
244 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
246 $v = "";
247 foreach($atr as $at){
248 if(isset($array[$at])){
249 $v .= " ".$at."='".$array[$at]."', ";
250 }
251 }
252 if(empty($v)){
253 echo "updateEnterprisesType: no attributes given ";
254 return(false);
255 }else{
256 $v = preg_replace("/, $/","",$v);
257 return($this->query("UPDATE glpi_enterprises SET ".$v." WHERE ID='".$id."';"));
258 }
259 }else{
260 echo "can't update not existing entry";
261 return(false);
262 }
263 }else{
264 echo "not connected";
265 return(false);
266 }
267 }
269 /* Manufacturer
270 Add new manufacturer
271 */
272 function addEnterprise($array)
273 {
274 if(!is_array($array)){
275 echo "addUser: first paraeter must be an array";
276 }elseif($this->is_connected){
277 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
278 $v = "";
279 $a = "";
280 foreach($atr as $at){
281 if(isset($array[$at])){
282 $a .= $at.", ";
283 $v .= "'".$array[$at]."', ";
284 }
285 }
286 if(empty($v)){
287 echo "addUser: no attributes given ";
288 return(false);
289 }else{
290 $a = preg_replace("/, $/","",$a);
291 $v = preg_replace("/, $/","",$v);
292 return($this->query("INSERT INTO glpi_enterprises (".$a.") VALUES (".$v.");"));
293 }
295 }else{
296 echo "not connected";
297 return(false);
298 }
300 }
302 /* Manufacturer
303 remove manufacturer
304 */
305 function removeEnterprise($id)
306 {
307 if($this->is_connected){
308 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
309 if(isset($tmp[0])){
310 return($this->query("DELETE FROM glpi_enterprises WHERE ID=".$id.";"));
311 }else{
312 echo "can't remove not existing entry";
313 return(false);
314 }
315 }else{
316 echo "not connected";
317 return(false);
318 }
319 }
321 /* Operating systems
322 Returns all OSs
323 */
324 function getOSTypes()
325 {
326 if($this->is_connected){
327 $ret = array();
328 $tmp=($this->query("SELECT * FROM glpi_dropdown_os;"));
330 foreach($tmp as $t){
331 $ret[$t['ID']]=$t['name'];
332 }
334 return($ret);
336 }else{
337 echo "not connected";
338 return(false);
339 }
340 }
342 /* Operating systems
343 Add a new operating system to the dropdown menus
344 */
345 function addOS($name)
346 {
347 if($this->is_connected){
348 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE name='".$name."';");
349 if(isset($tmp[0])){
350 echo "such an entry already exists";
351 return(false);
352 }else{
353 return($this->query("INSERT INTO glpi_dropdown_os (name) VALUES ('".$name."');"));
354 }
355 }else{
356 echo "not connected";
357 return(false);
358 }
359 }
361 /* Operating systems
362 remove one OS entry
363 */
364 function removeOS_byID($id)
365 {
366 if($this->is_connected){
367 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
368 if(is_array($tmp[0])){
369 return($this->query("DELETE FROM glpi_dropdown_os WHERE ID=".$id.";"));
370 }else{
371 echo "can't remove not existing entry";
372 return(false);
373 }
374 }else{
375 echo "not connected";
376 return(false);
377 }
378 }
380 /* Operating systems
381 Update existing OS entry
382 */
383 function updateOS($name,$id)
384 {
386 if($this->is_connected){
387 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
388 if(isset($tmp[0])){
389 return($this->query("UPDATE glpi_dropdown_os SET name='".$name."' WHERE ID=".$id.";"));
390 }else{
391 echo "can't update not existing entry";
392 return(false);
393 }
394 }else{
395 echo "not connected";
396 return(false);
397 }
398 }
400 /* This returns all available glpi users
401 */
402 function getUsers()
403 {
404 if($this->is_connected){
405 $ret = array();
406 $tmp = ($this->query("SELECT * FROM glpi_users"));
407 foreach($tmp as $user){
408 $ret[$user['ID']]=$user['name'];
409 }
410 return($ret);
412 }else{
413 echo "not connected";
414 return(false);
415 }
416 }
418 /* this function adds a new glpi user
419 */
420 function addUser($array,$dn)
421 {
422 if(!is_array($array)){
423 echo "addUser: first paraeter must be an array";
424 }elseif($this->is_connected){
425 $array['name']=$dn;
426 $atr = array("name","phone","email");
427 $v = "";
428 $a = "";
429 foreach($atr as $at){
430 if(isset($array[$at])){
431 $a .= $at.", ";
432 $v .= "'".$array[$at]."', ";
433 }
434 }
435 if(empty($v)){
436 echo "addUser: no attributes given ";
437 return(false);
438 }else{
439 $a = preg_replace("/, $/","",$a);
440 $v = preg_replace("/, $/","",$v);
441 return($this->query("INSERT INTO glpi_users (".$a.") VALUES (".$v.");"));
442 }
444 }else{
445 echo "not connected";
446 return(false);
447 }
449 }
451 /* This function updates a glpi user
452 with the given data
453 */
454 function updateUser($array,$dn)
455 {
456 if(!is_array($array)){
457 echo "updateUser: first paraeter must be an array";
458 }elseif($this->is_connected){
459 $tmp = $this->query("SELECT * FROM glpi_users WHERE name='".$dn."';");
460 if(isset($tmp[0])){
462 $atr = array("name","phone","email");
463 $v = "";
464 foreach($atr as $at){
465 if(isset($array[$at])){
466 $v .= " ".$at."='".$array[$at]."', ";
467 }
468 }
469 if(empty($v)){
470 echo "UpdateUser: no attributes given ";
471 return(false);
472 }else{
473 $v = preg_replace("/, $/","",$v);
474 return($this->query("UPDATE glpi_users SET ".$v." WHERE name='".$dn."';"));
475 }
476 }else{
477 echo "can't update not existing entry";
478 return(false);
479 }
480 }else{
481 echo "not connected";
482 return(false);
483 }
485 }
487 /* This function returns all available data
488 from a specified dn
489 */
490 function getComputerInformations($name)
491 {
492 if($this->is_connected){
493 $ret = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
494 return($ret);
495 }else{
496 echo "not connected";
497 return(false);
498 }
499 }
501 /* This fucntions updates an already existing entry
502 */
503 function updateComputerInformations($array,$name)
504 {
505 if(!is_array($array)){
506 echo "updateComputerInformations: first paraeter must be an array";
507 }elseif($this->is_connected){
508 $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
509 if(isset($tmp[0])){
511 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
512 "tech_num","comments","date_mod","os","location","domain","network",
513 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
514 $v = "";
515 foreach($atr as $at){
516 if(isset($array[$at])){
517 $v .= " ".$at."='".$array[$at]."', ";
518 }
519 }
520 if(empty($v)){
521 echo "updateComputerInformations: no attributes given ";
522 return(false);
523 }else{
524 $v = preg_replace("/, $/","",$v);
525 return($this->query("UPDATE glpi_computers SET ".$v." WHERE name='".$name."';"));
526 }
527 }else{
528 echo "can't update not existing entry";
529 return(false);
530 }
531 }else{
532 echo "not connected";
533 return(false);
534 }
536 }
538 /* This function adds a new inventory device (computer phone etc)
539 */
540 function addComputerInformations($array)
541 {
542 if(!is_array($array)){
543 echo "updateComputerInformations: first paraeter must be an array";
544 }elseif($this->is_connected){
545 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
546 "tech_num","comments","date_mod","os","location","domain","network",
547 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
548 $v = "";
549 $a = "";
550 foreach($atr as $at){
551 if(isset($array[$at])){
552 $a .= $at.", ";
553 $v .= "'".$array[$at]."', ";
554 }
555 }
556 if(empty($v)){
557 echo "updateComputerInformations: no attributes given ";
558 return(false);
559 }else{
560 $a = preg_replace("/, $/","",$a);
561 $v = preg_replace("/, $/","",$v);
562 return($this->query("INSERT INTO glpi_computers (".$a.") VALUES (".$v.");"));
563 }
565 }else{
566 echo "not connected";
567 return(false);
568 }
570 }
572 /* this functions checks if the given Device
573 * already exists
574 */
575 function deviceExists($attr)
576 {
577 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
578 if($this->is_connected){
579 $arr = array_flip($deviceMappingGOsaGlpi);
581 $tbl_name = $arr[$attr['device_type']];
582 if(!isset($attr['ID'])){
583 return(false);
584 }else{
585 $qry = "SELECT * FROM ".$tbl_name." WHERE ID=".$attr['ID'].";";
586 $res = $this->query($qry);
587 if(count($res) != 0){
588 return(true);
589 }
590 }
591 }else{
592 echo "not connected";
593 return(false);
594 }
596 return(false);
597 }
600 /* Check if given device is used by some accounts
601 * (helpfull to avoid removement of used devices)
602 */
603 function is_deviceUsed($item)
604 {
606 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
607 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
608 if($this->is_connected){
609 $tablename = $deviceMappingGOsaGlpi[$item['device_type']];
610 $type = $item['device_type'];
612 if($type=="monitor"){
613 $str = "SELECT * FROM glpi_connect_wire WHERE end1=".$item['ID']." AND type=4;";
614 }else{
615 $str = "SELECT * FROM glpi_computer_device WHERE device_type=".$deviceMappingTableNameID[$type]." AND FK_device=".$item['ID'].";";
616 }
617 return(count($this->query($str)));
618 }else{
619 echo "not connected";
620 return(false);
621 }
623 }
626 /* This functions deletes a specified entry
627 * from our device tables
628 */
629 function deleteDevice($attr)
630 {
631 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
632 if($this->is_connected){
633 $arr = array_flip($deviceMappingGOsaGlpi);
635 $device_type = $attr['device_type'];
636 unset($attr['device_type']);
638 $tbl_name = $arr[$device_type];
640 $this->query("DELETE FROM ".$tbl_name." WHERE ID=".$attr['ID'].";");
641 }else{
642 echo "not connected";
643 return(false);
644 }
645 }
647 /* This funtions updated an already existing device
648 */
649 function updateDevices($attr)
650 {
651 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
652 if($this->is_connected){
653 $arr = array_flip($deviceMappingGOsaGlpi);
655 $device_type = $attr['device_type'];
656 unset($attr['device_type']);
658 $tbl_name = $arr[$device_type];
660 $str = "UPDATE ".$tbl_name." SET ";
661 foreach($attr as $name => $value){
662 $str.=$name."='".$value."', ";
663 }
664 $str = preg_replace("/, $/","",$str);
665 $str .= " WHERE ID=".$attr['ID'].";";
666 $this->query($str);
667 }else{
668 echo "not connected";
669 return(false);
670 }
671 }
673 /* Returns all possible RAM types
674 * like SDRAM , DIMM .....
675 */
676 function getRAMTypes()
677 {
678 if($this->is_connected){
679 $ret = array();
680 $tmp = ($this->query("SELECT * FROM glpi_dropdown_ram_type;"));
681 foreach($tmp as $t){
682 $ret[$t['ID']]=$t['name'];
683 }
684 return($ret);
685 }else{
686 echo "not connected";
687 return(false);
688 }
689 }
691 /* Returns all possible HDD connection types
692 * like IDE SCSI ...
693 */
694 function getGlpiDeviceControlTypes()
695 {
696 if($this->is_connected){
697 $ret = array();
698 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
699 foreach($tmp as $t){
700 $ret[$t['ID']]=$t['name'];
701 }
702 return($ret);
703 }else{
704 echo "not connected";
705 return(false);
706 }
707 }
709 /* Returns all possible gfx card connection types
710 * like PCI-X PCI AGP ....
711 */
712 function getGlpiGfxControlTypes()
713 {
714 if($this->is_connected){
715 $ret = array();
716 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
717 foreach($tmp as $t){
718 $ret[$t['ID']]=$t['name'];
719 }
720 return($ret);
721 }else{
722 echo "not connected";
723 return(false);
724 }
725 }
727 /* Devices
728 Adds a new single device to our db
729 */
730 function addDevice($attr)
731 {
732 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
733 if($this->is_connected){
734 $arr = array_flip($deviceMappingGOsaGlpi);
736 $device_type = $attr['device_type'];
737 unset($attr['device_type']);
739 $tbl_name = $arr[$device_type];
740 $v = "";
741 $a = "";
742 foreach($attr as $name => $value){
743 $a .= $name.", ";
744 $v .= "'".$value."', ";
745 }
746 if(empty($v)){
747 echo "addDevice: no attributes given ";
748 return(false);
749 }else{
750 $a = preg_replace("/, $/","",$a);
751 $v = preg_replace("/, $/","",$v);
752 return($this->query("INSERT INTO ".$tbl_name." (".$a.") VALUES (".$v.");"));
753 }
755 }else{
756 echo "not connected";
757 return(false);
758 }
759 }
761 /* Return all available devices
762 */
763 function getDevices()
764 {
765 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
766 if($this->is_connected){
767 $arr = $deviceMappingGOsaGlpi;
769 $res = array();
770 foreach($arr as $glpi => $gosa){
771 $qry = "SELECT * FROM ".$glpi.";";
772 $ret = $this->query($qry);
773 foreach($ret as $id => $entry){
774 $entry['device_type'] = $gosa;
776 if(isset($entry['designation'])){
777 $res[$entry['designation']."-".$gosa] = $entry;
778 }else{
779 $res[$entry['name']."-".$gosa] = $entry;
780 }
781 }
782 }
783 return($res);
784 }else{
785 echo "not connected";
786 return(false);
787 }
788 }
790 /* This function returns all used devices
791 */
792 function getUsedDevices($computerID)
793 {
794 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
795 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
797 if($this->is_connected){
798 $qry = "SELECT * FROM glpi_computer_device WHERE FK_computers=".$computerID.";";
799 $res = $this->query($qry);
801 $ret = array();
802 foreach($deviceMappingGOsaGlpi as $GOsa => $glpi){
803 $ret[$GOsa] = array();
804 }
806 $tbls = array_flip($deviceMappingTableNameID);
808 foreach($res as $device){
809 $devtype = $tbls[$device['device_type']];
810 $tbl_name = $deviceMappingGOsaGlpi[$devtype];
811 $qry = ("SELECT * FROM ".$tbl_name." WHERE ID=".$device['FK_device'].";");
812 $res2 = $this->query($qry);
813 if(count($res2)!=0){
814 $ret[$devtype][$res2[0]['designation']]=$res2[0];
815 }
817 $qry = "SELECT * FROM glpi_connect_wire WHERE type=4 AND end2=".$computerID.";";
818 $res2 = $this->query($qry);
819 foreach($res2 as $monitor){
820 $qry = "SELECT * FROM glpi_monitors WHERE ID=".$monitor['end1'].";";
821 $res3 = $this->query($qry);
822 foreach($res3 as $moni){
823 $ret['monitor'][$moni['name']]=$moni;
824 }
825 }
829 }
830 return($ret);
831 }else{
832 echo "not connected";
833 return(false);
834 }
835 }
837 /* This function removes all given devices from a computer, specified by $id
838 In the next step all devices specified by devices will be added.
839 */
840 function addDevicesToComputer($devices, $id)
841 {
842 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
843 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
845 if(($id == "" )||(!is_numeric($id))){
846 return (false);
847 }
848 if($this->is_connected){
849 $qry = "DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";";
850 $this->query($qry);
852 foreach($devices as $type => $entries){
853 foreach($entries as $entry){
854 if($type=="monitor"){
855 $str = "INSERT INTO glpi_connect_wire (end1,end2,type)
856 VALUES (".$entry['ID'].",".$id.",4);";
857 }else{
858 $str = "INSERT INTO glpi_computer_device (device_type,FK_device,FK_computers)
859 VALUES (".$deviceMappingTableNameID[$type].",".$entry['ID'].",".$id.");";
860 }
861 $this->query($str);
862 }
863 }
866 }else{
867 echo "not connected";
868 return(false);
869 }
871 }
873 function removeComputerInformations($name)
874 {
875 if($this->is_connected){
876 $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
877 if(isset($tmp[0])){
878 $id = $tmp[0]['ID'];
879 $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
880 $this->query("DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";");
881 return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
882 }else{
883 echo "can't remove not existing entry";
884 return(false);
885 }
886 }else{
887 echo "not connected";
888 return(false);
889 }
890 }
892 function is_connected()
893 {
894 return($this->is_connected);
895 }
897 function addAttachmentsToComputer($attr,$id)
898 {
899 if(($id == "" )||(!is_numeric($id))){
900 return (false);
901 }
902 if($this->is_connected){
903 $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=1);";
904 $this->query($qry);
906 foreach($attr as $aid => $entry){
907 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template)
908 VALUES
909 ($aid,$id,1,'0');";
910 $this->query($str);
911 }
912 }else{
913 echo "not connected";
914 return(false);
915 }
916 }
918 function getAssignAttachments($id)
919 {
921 if($this->is_connected){
922 $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=1) AND (FK_device=".$id.");";
923 $ret = $this->query($qry);
924 return($ret);
925 }else{
926 echo "not connected";
927 return(false);
928 }
929 }
931 function deleteAttachment($id)
932 {
933 if($this->is_connected){
934 $qry = "DELETE FROM glpi_docs WHERE ID=".$id."";
935 $this->query($qry);
936 }else{
937 echo "not connected";
938 return(false);
939 }
940 }
942 function getAttachments()
943 {
944 $ret = array();
945 if($this->is_connected){
946 $qry = "SELECT * FROM glpi_docs WHERE name!='';";
947 $re = $this->query($qry);
949 foreach($re as $entry){
950 $ret[$entry['ID']]=$entry;
951 }
953 return($ret);
954 }else{
955 echo "not connected";
956 return(false);
957 }
958 }
960 function saveAttachments($attrs,$id = -1)
961 {
962 if($this->is_connected){
963 $atr = array("name","filename","rubrique","mime","date_mod","comment","deleted","link");
964 $tmp = array();
965 foreach($atr as $at){
966 if(isset($attrs[$at])){
967 $tmp[$at] = $attrs[$at];
968 }
969 }
970 if(count($tmp)==0){
971 return(false);
972 }else{
974 // Add
975 if($id == -1){
976 $str = "INSERT INTO glpi_docs ";
977 $namen = "";
978 $values= "";
979 foreach($tmp as $name => $value){
980 $namen .= $name.", ";
981 if(is_numeric($value)){
982 $values .= $value.", ";
983 }else{
984 $values .= "'".$value."', ";
985 }
986 }
987 $values = preg_replace("/, $/","",$values);
988 $namen = preg_replace("/, $/","",$namen);
989 $str .= "(".$namen.") VALUES (".$values.");";
990 }else{
991 $str = "UPDATE glpi_docs SET ";
992 foreach($tmp as $name => $value){
993 $str .= $name."= ";
994 if(is_numeric($value)){
995 $str .= $value.", ";
996 }else{
997 $str .= "'".$value."', ";
998 }
999 }
1000 $str = preg_replace("/, $/","",$str);
1001 $str .= " WHERE ID=".$id.";";
1002 }
1003 $this->query($str);
1004 }
1005 }else{
1006 echo "not connected";
1007 return(false);
1008 }
1009 }
1012 /* Check if given attachment id is used in any Device
1013 ( - avoid removing of used attachments)
1014 */
1015 function is_attachmentUsed($id)
1016 {
1017 if($this->is_connected){
1018 $qry = "SELECT * FROM glpi_doc_device WHERE FK_doc =".$id." LIMIT 1; ";
1019 return(count($this->query($qry)));
1020 }else{
1021 echo "not connected";
1022 return(false);
1023 }
1024 }
1027 /* Monitor handling
1028 */
1029 function getMonitors()
1030 {
1031 if($this->is_connected){
1032 $qry= "SELECT * FROM glpi_monitors;";
1033 return($this->query($qry));
1035 }else{
1036 echo "not connected";
1037 return(false);
1038 }
1039 }
1041 function updatedMonitor()
1042 {
1043 if($this->is_connected){
1044 // $qry= "SELECT * FROM glpi_monitors;";
1045 // return($this->query($qry));
1047 }else{
1048 echo "not connected";
1049 return(false);
1050 }
1051 }
1053 function addMonitor()
1054 {
1055 if($this->is_connected){
1056 // $qry= "SELECT * FROM glpi_monitors;";
1057 // return($this->query($qry));
1059 }else{
1060 echo "not connected";
1061 return(false);
1062 }
1063 }
1065 function removeMonitor($id)
1066 {
1067 if($this->is_connected){
1068 $qry= "DELETE FROM glpi_monitors WHERE ID=".$id.";";
1069 $this->query($qry);
1070 }else{
1071 echo "not connected";
1072 return(false);
1073 }
1074 }
1076 function getMonitorTypes()
1077 {
1078 if($this->is_connected){
1079 $qry= "SELECT * FROM glpi_type_monitors;";
1080 return($this->query($qry));
1082 }else{
1083 echo "not connected";
1084 return(false);
1085 }
1086 }
1088 function getLocationTypes()
1089 {
1090 if($this->is_connected){
1091 $qry= "SELECT * FROM glpi_dropdown_locations;";
1092 return($this->query($qry));
1094 }else{
1095 echo "not connected";
1096 return(false);
1097 }
1098 }
1100 function getStateTypes()
1101 {
1102 if($this->is_connected){
1103 $qry= "SELECT * FROM glpi_dropdown_state;";
1104 return($this->query($qry));
1105 }else{
1106 echo "not connected";
1107 return(false);
1108 }
1109 }
1112 /* Printer functions
1113 */
1116 /* This functions checks if the selected computer/network
1117 device is already available in the db
1118 */
1119 function is_printer_account($dn)
1120 {
1121 if(!$this->is_connected){
1122 $this->lasterror ="Can't query anything, if we aren't connected.";
1123 return(false);
1124 }else{
1125 $qry = "SELECT * FROM glpi_printers WHERE name='".$dn."';";
1126 $res = $this->query($qry);
1127 if(count($res)==0){
1128 return(false);
1129 }else{
1130 return(true);
1131 }
1132 }
1133 }
1135 /* This function returns all available data
1136 from a specified dn
1137 */
1138 function getPrinterInformations($name)
1139 {
1140 if($this->is_connected){
1141 $ret = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1142 return($ret);
1143 }else{
1144 echo "not connected";
1145 return(false);
1146 }
1147 }
1149 /* Get Printer attachments
1150 */
1151 function getAssignPrinterAttachments($id)
1152 {
1154 if($this->is_connected){
1155 $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=3) AND (FK_device=".$id.");";
1156 $ret = $this->query($qry);
1157 return($ret);
1158 }else{
1159 echo "not connected";
1160 return(false);
1161 }
1162 }
1164 /* Printer types
1165 Returns all defined printer types
1166 */
1167 function getPrinterTypes()
1168 {
1169 if($this->is_connected){
1170 $ret = array();
1171 $tmp = ($this->query("SELECT * FROM glpi_type_printers;"));
1172 foreach($tmp as $t){
1173 $ret[$t['ID']]=$t['name'];
1174 }
1175 return($ret);
1176 }else{
1177 echo "not connected";
1178 return(false);
1179 }
1180 }
1182 /* Add pritner types
1183 Add one entry to the printer types
1184 */
1185 function addPrinterType($name)
1186 {
1187 if($this->is_connected){
1188 $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE name='".$name."';");
1189 if(isset($tmp[0])){
1190 //echo "such an entry already exists";
1191 return(false);
1192 }else{
1193 return($this->query("INSERT INTO glpi_type_printers (name) VALUES ('".$name."');"));
1194 }
1195 }else{
1196 echo "not connected";
1197 return(false);
1198 }
1199 }
1201 /* remove printer types
1202 Remove one entry from the printer types (specified by ID=$id)
1203 */
1204 function removePrinterType($id)
1205 {
1206 if($this->is_connected){
1207 $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1208 if(isset($tmp[0])){
1209 return($this->query("DELETE FROM glpi_type_printers WHERE ID=".$id.";"));
1210 }else{
1211 echo "can't remove not existing entry";
1212 return(false);
1213 }
1214 }else{
1215 echo "not connected";
1216 return(false);
1217 }
1218 }
1220 /* Update printer types
1221 Update a printer type
1222 */
1223 function updatePrinterType($name,$id)
1224 {
1226 if($this->is_connected){
1227 $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1228 if(isset($tmp[0])){
1229 return($this->query("UPDATE glpi_type_printers SET name='".$name."' WHERE ID=".$id.";"));
1230 }else{
1231 echo "can't update not existing entry";
1232 return(false);
1233 }
1234 }else{
1235 echo "not connected";
1236 return(false);
1237 }
1238 }
1241 /* This fucntions updates an already existing entry
1242 */
1243 function updatePrinterInformations($array,$name)
1244 {
1245 if(!is_array($array)){
1246 echo "updatePrinterInformations: first paraeter must be an array";
1247 }elseif($this->is_connected){
1248 $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1249 if(isset($tmp[0])){
1251 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
1252 "tech_num","comments","date_mod","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1253 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1254 $v = "";
1255 foreach($atr as $at){
1256 if(isset($array[$at])){
1257 $v .= " ".$at."='".$array[$at]."', ";
1258 }
1259 }
1260 if(empty($v)){
1261 echo "updateSystemInformations: no attributes given ";
1262 return(false);
1263 }else{
1264 $v = preg_replace("/, $/","",$v);
1265 return($this->query("UPDATE glpi_printers SET ".$v." WHERE name='".$name."';"));
1266 }
1267 }else{
1268 echo "can't update not existing entry";
1269 return(false);
1270 }
1271 }else{
1272 echo "not connected";
1273 return(false);
1274 }
1276 }
1278 /* This function adds a new inventory settings for printers
1279 */
1280 function addPrinterInformations($array)
1281 {
1282 if(!is_array($array)){
1283 echo "updateComputerInformations: first paraeter must be an array";
1284 }elseif($this->is_connected){
1285 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
1286 "tech_num","comments","date_mod","os","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1287 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1288 $v = "";
1289 $a = "";
1290 foreach($atr as $at){
1291 if(isset($array[$at])){
1292 $a .= $at.", ";
1293 $v .= "'".$array[$at]."', ";
1294 }
1295 }
1296 if(empty($v)){
1297 echo "updateComputerInformations: no attributes given ";
1298 return(false);
1299 }else{
1300 $a = preg_replace("/, $/","",$a);
1301 $v = preg_replace("/, $/","",$v);
1302 return($this->query("INSERT INTO glpi_printers (".$a.") VALUES (".$v.");"));
1303 }
1305 }else{
1306 echo "not connected";
1307 return(false);
1308 }
1309 }
1311 /* add atachment to given printer */
1312 function addAttachmentsToPrinter($attr,$id)
1313 {
1314 if(($id == "" )||(!is_numeric($id))){
1315 return (false);
1316 }
1317 if($this->is_connected){
1318 $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=3);";
1319 $this->query($qry);
1321 foreach($attr as $aid => $entry){
1322 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template)
1323 VALUES
1324 ($aid,$id,3,'0');";
1325 $this->query($str);
1326 }
1327 }else{
1328 echo "not connected";
1329 return(false);
1330 }
1331 }
1333 function removePrinterInformations($name)
1334 {
1335 if($this->is_connected){
1336 $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1337 if(isset($tmp[0])){
1338 $id = $tmp[0]['ID'];
1339 // $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
1340 $this->query("DELETE FROM glpi_doc_device WHERE FK_device=".$id." AND device_type=3;");
1341 return($this->query("DELETE FROM glpi_printers WHERE ID=".$id.";"));
1342 }else{
1343 echo "can't remove not existing entry";
1344 return(false);
1345 }
1346 }else{
1347 echo "not connected";
1348 return(false);
1349 }
1350 }
1353 /* Cartridges
1354 */
1356 /* return all assigned cartridges */
1357 function getUsedCartridges($printerID)
1358 {
1359 if($this->is_connected){
1360 $ret = array();
1361 $qry = "SELECT
1362 c.date_use as date_use,
1363 c.ID as ID,
1364 t.ID as type_ID,
1365 t.name as name,
1366 c.FK_glpi_printers as FK_glpi_printers,
1367 d.name as type_name
1368 FROM
1369 glpi_dropdown_cartridge_type as d,
1370 glpi_cartridges as c,
1371 glpi_cartridges_type as t
1372 WHERE c.FK_glpi_cartridges_type = t.ID
1373 AND t.type = d.ID
1374 AND c.FK_glpi_printers = ".$printerID.";";
1375 $res = $this->query($qry);
1376 foreach($res as $entry){
1377 $ret[$entry['ID']] = $entry;
1378 }
1379 return($ret);
1380 }else{
1381 echo "not connected";
1382 return(false);
1383 }
1384 }
1386 /* return all assigned cartridges */
1387 function getAvailableCartridgeTypes($printerTypeID)
1388 {
1389 if($this->is_connected){
1390 $ret = array();
1391 $qry= "
1392 SELECT
1393 ct.ID as cartridgeID,
1394 ct.name as cartridgeName,
1395 pt.ID as printerTypeID,
1396 pt.name as printerTypeName,
1397 ct.type as cartridgeTypeID,
1398 dt.name as cartridgeTypeName
1399 FROM
1400 glpi_type_printers as pt,
1401 glpi_cartridges_type as ct,
1402 glpi_dropdown_cartridge_type as dt,
1403 glpi_cartridges_assoc as ac
1404 WHERE
1405 ac.FK_glpi_type_printer = pt.ID
1406 AND ac.FK_glpi_cartridges_type = ct.ID
1407 AND ct.type=dt.ID
1408 AND pt.ID=".$printerTypeID.";";
1409 $res = $this->query($qry);
1410 foreach($res as $entry){
1411 $ret[$entry['cartridgeID']] = $entry;
1412 }
1413 return($ret);
1414 }else{
1415 echo "not connected";
1416 return(false);
1417 }
1418 }
1420 function removeCartridgeFromPrinter($cartridgeID)
1421 {
1422 if($this->is_connected){
1423 $qry = "DELETE FROM glpi_cartridges WHERE ID=".$cartridgeID.";";
1424 return($this->query($qry));
1425 }else{
1426 echo "not connected";
1427 return(false);
1428 }
1429 }
1431 function addCartridgeFromPrinter($printerID,$cartridgeID)
1432 {
1433 if($this->is_connected){
1434 $qry ="INSERT INTO
1435 glpi_cartridges (FK_glpi_cartridges_type,FK_glpi_printers,date_in,date_use)
1436 VALUES
1437 (".$cartridgeID.",".$printerID.",'".date("Y-m-d")."','".date("Y-m-d")."');";
1438 return($this->query($qry));
1439 }else{
1440 echo "not connected";
1441 return(false);
1442 }
1443 }
1445 function getCartridgeTypeInformations($id = "all"){
1446 if($this->is_connected){
1447 $ret = array();
1448 if($id != "all"){
1449 $qry = "SELECT * FROM glpi_cartridges_type WHERE ID = ".$id.";";
1450 }else{
1451 $qry = "SELECT * FROM glpi_cartridges_type;";
1452 }
1454 $res = ($this->query($qry));
1455 foreach($res as $entry){
1456 $ret[$entry['ID']] = $entry;
1457 }
1458 return($ret);
1460 }else{
1461 echo "not connected";
1462 return(false);
1463 }
1464 }
1466 function getCartridgeTypes(){
1467 if($this->is_connected){
1468 $ret = array();
1469 $qry = "SELECT * FROM glpi_dropdown_cartridge_type;";
1470 $res = ($this->query($qry));
1471 foreach($res as $entry){
1472 $ret[$entry['ID']] = $entry['name'];
1473 }
1474 return($ret);
1476 }else{
1477 echo "not connected";
1478 return(false);
1479 }
1480 }
1483 /* Manufacturer
1484 Updates already existing manufacturer
1485 */
1486 function Add_UpdateCatrigdeType($array,$array_printer_types)
1487 {
1488 if(!is_array($array)){
1489 echo "Add_UpdateCatrigdeType: first paraeter must be an array";
1490 }elseif($this->is_connected){
1493 $atr = array("name","ref","location","type","FK_glpi_enterprise","tech_num","deleted","comments","alarm");
1495 /* Entry was edited */
1496 if($array['ID']>0){
1497 $qry = "DELETE FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type=".$array['ID'].";";
1499 $v = "";
1500 foreach($atr as $at){
1501 if(isset($array[$at])){
1502 $v .= " ".$at."='".$array[$at]."', ";
1503 }
1504 }
1505 if(empty($v)){
1506 echo "Add_UpdateCatrigdeType: no attributes given ";
1507 return(false);
1508 }else{
1509 $v = preg_replace("/, $/","",$v);
1510 $qry = "UPDATE glpi_cartridges_type SET ".$v." WHERE ID='".$array['ID']."';";
1511 $this->query($qry);
1512 }
1513 }else{
1515 /* skip if name is in use*/
1516 $qry = "SELECT * FROM glpi_cartridges_type WHERE name='".$array['name']."';";
1517 if(count($this->query($qry))){
1518 return;
1519 }
1521 $str = "INSERT INTO glpi_cartridges_type ";
1522 $namen = "";
1523 $values= "";
1524 foreach($array as $name => $value){
1525 $namen .= $name.", ";
1526 if(is_numeric($value)){
1527 $values .= $value.", ";
1528 }else{
1529 $values .= "'".$value."', ";
1530 }
1531 }
1532 $values = preg_replace("/, $/","",$values);
1533 $namen = preg_replace("/, $/","",$namen);
1534 $str .= "(".$namen.") VALUES (".$values.");";
1535 $this->query($str);
1536 $IDs = $this->query("SELECT ID FROM glpi_cartridges_type WHERE name='".$array['name']."';");
1537 if(count($IDs) > 1){
1538 echo "internal db error";
1539 return;
1540 }
1541 $array['ID'] = $IDs[0]['ID'];
1542 }
1544 foreach($array_printer_types as $id){
1545 $qry = "INSERT INTO glpi_cartridges_assoc
1546 (FK_glpi_cartridges_type,FK_glpi_type_printer)
1547 VALUES
1548 (".$array['ID'].",".$id.")";
1550 $this->query($qry);
1551 }
1552 }else{
1553 echo "not connected";
1554 return(false);
1555 }
1556 }
1558 function getSupportedPrinterTypeIDsForCartridge($cid)
1559 {
1560 if($this->is_connected){
1561 $ret = array();
1562 $qry = "SELECT FK_glpi_type_printer FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type = ".$cid.";";
1563 $res = $this->query($qry);
1565 foreach($res as $entry => $value){
1566 $ret[$value['FK_glpi_type_printer']] = $value['FK_glpi_type_printer'];
1567 }
1568 return($ret);
1569 }else{
1570 echo "not connected";
1571 return(false);
1572 }
1573 }
1575 function removeCartridgeType($id){
1576 if($this->is_connected){
1577 $qry = "DELETE FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type=".$id.";";
1578 $this->query($qry);
1579 $qry = "DELETE FROM glpi_cartridges_type WHERE ID=".$id.";";
1580 return($this->query($qry));
1581 }else{
1582 echo "not connected";
1583 return(false);
1584 }
1585 }
1587 function getCartridgesWhichUseThisType($id)
1588 {
1589 if($this->is_connected){
1590 $qry = "SELECT * FROM glpi_cartridges WHERE FK_glpi_cartridges_type=".$id.";";
1591 $ret = $this->query($qry);
1592 return($ret);
1593 }else{
1594 echo "not connected";
1595 return(false);
1596 }
1597 }
1600 /* Add pritner types
1601 Add one entry to the cartridgeType types
1602 */
1603 function addCartridgeDropdownType($name)
1604 {
1605 if($this->is_connected){
1606 $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE name='".$name."';");
1607 if(isset($tmp[0])){
1608 //echo "such an entry already exists";
1609 return(false);
1610 }else{
1611 return($this->query("INSERT INTO glpi_dropdown_cartridge_type (name) VALUES ('".$name."');"));
1612 }
1613 }else{
1614 echo "not connected";
1615 return(false);
1616 }
1617 }
1619 /* remove cartridgeType types
1620 Remove one entry from the cartridgeType types (specified by ID=$id)
1621 */
1622 function removeCartridgeDropdownType($id)
1623 {
1624 if($this->is_connected){
1625 $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";");
1626 if(isset($tmp[0])){
1627 return($this->query("DELETE FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";"));
1628 }else{
1629 echo "can't remove not existing entry";
1630 return(false);
1631 }
1632 }else{
1633 echo "not connected";
1634 return(false);
1635 }
1636 }
1638 /* Update cartridgeType
1639 Update a cartridgeType
1640 */
1641 function updateCartridgeDropdownType($name,$id)
1642 {
1644 if($this->is_connected){
1645 $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";");
1646 if(isset($tmp[0])){
1647 return($this->query("UPDATE glpi_dropdown_cartridge_type SET name='".$name."' WHERE ID=".$id.";"));
1648 }else{
1649 echo "can't update not existing entry";
1650 return(false);
1651 }
1652 }else{
1653 echo "not connected";
1654 return(false);
1655 }
1656 }
1658 function getUsedDropdownTypes($id=false)
1659 {
1660 if($this->is_connected){
1661 if($id){
1662 $qry = "SELECT distinct(type) FROM glpi_cartridges_type WHERE type = ".$id.";";
1663 }else{
1664 $qry = "SELECT distinct(type) FROM glpi_cartridges_type;";
1665 }
1666 return($this->query($qry));
1667 }else{
1668 echo "not connected";
1669 return(false);
1670 }
1671 }
1673 }
1674 //$s = new glpiDB("vserver-01","glpi","tester","glpi");
1675 //print_r($s->query("SELECT * FROM glpi_computers"));
1676 //$s->getComputerInformations("1 OR (c.ID<10000)");
1677 ?>