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 system is used ? */
343 function is_osUsed($ID){
344 if($this->is_connected){
345 $ret = array();
346 $qry="SELECT name,type FROM glpi_computers WHERE os=".$ID." LIMIT 3;";
347 $res = $this->query($qry);
348 foreach($res as $val){
349 $ret[$val['name']] = $val['name'];
350 }
351 return($ret);
352 }else{
353 echo "not connected";
354 return(false);
355 }
356 }
359 /* Operating systems
360 Add a new operating system to the dropdown menus
361 */
362 function addOS($name)
363 {
364 if($this->is_connected){
365 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE name='".$name."';");
366 if(isset($tmp[0])){
367 echo "such an entry already exists";
368 return(false);
369 }else{
370 return($this->query("INSERT INTO glpi_dropdown_os (name) VALUES ('".$name."');"));
371 }
372 }else{
373 echo "not connected";
374 return(false);
375 }
376 }
378 /* Operating systems
379 remove one OS entry
380 */
381 function removeOS_byID($id)
382 {
383 if($this->is_connected){
384 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
385 if(is_array($tmp[0])){
386 return($this->query("DELETE FROM glpi_dropdown_os WHERE ID=".$id.";"));
387 }else{
388 echo "can't remove not existing entry";
389 return(false);
390 }
391 }else{
392 echo "not connected";
393 return(false);
394 }
395 }
397 /* Operating systems
398 Update existing OS entry
399 */
400 function updateOS($name,$id)
401 {
403 if($this->is_connected){
404 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
405 if(isset($tmp[0])){
406 return($this->query("UPDATE glpi_dropdown_os SET name='".$name."' WHERE ID=".$id.";"));
407 }else{
408 echo "can't update not existing entry";
409 return(false);
410 }
411 }else{
412 echo "not connected";
413 return(false);
414 }
415 }
417 /* This returns all available glpi users
418 */
419 function getUsers()
420 {
421 if($this->is_connected){
422 $ret = array();
423 $tmp = ($this->query("SELECT * FROM glpi_users"));
424 foreach($tmp as $user){
425 $ret[$user['ID']]=$user['name'];
426 }
427 return($ret);
429 }else{
430 echo "not connected";
431 return(false);
432 }
433 }
435 /* this function adds a new glpi user
436 */
437 function addUser($array,$dn)
438 {
439 if(!is_array($array)){
440 echo "addUser: first paraeter must be an array";
441 }elseif($this->is_connected){
442 $array['name']=$dn;
443 $atr = array("name","phone","email");
444 $v = "";
445 $a = "";
446 foreach($atr as $at){
447 if(isset($array[$at])){
448 $a .= $at.", ";
449 $v .= "'".$array[$at]."', ";
450 }
451 }
452 if(empty($v)){
453 echo "addUser: no attributes given ";
454 return(false);
455 }else{
456 $a = preg_replace("/, $/","",$a);
457 $v = preg_replace("/, $/","",$v);
458 return($this->query("INSERT INTO glpi_users (".$a.") VALUES (".$v.");"));
459 }
461 }else{
462 echo "not connected";
463 return(false);
464 }
466 }
468 /* This function updates a glpi user
469 with the given data
470 */
471 function updateUser($array,$dn)
472 {
473 if(!is_array($array)){
474 echo "updateUser: first paraeter must be an array";
475 }elseif($this->is_connected){
476 $tmp = $this->query("SELECT * FROM glpi_users WHERE name='".$dn."';");
477 if(isset($tmp[0])){
479 $atr = array("name","phone","email");
480 $v = "";
481 foreach($atr as $at){
482 if(isset($array[$at])){
483 $v .= " ".$at."='".$array[$at]."', ";
484 }
485 }
486 if(empty($v)){
487 echo "UpdateUser: no attributes given ";
488 return(false);
489 }else{
490 $v = preg_replace("/, $/","",$v);
491 return($this->query("UPDATE glpi_users SET ".$v." WHERE name='".$dn."';"));
492 }
493 }else{
494 echo "can't update not existing entry";
495 return(false);
496 }
497 }else{
498 echo "not connected";
499 return(false);
500 }
502 }
504 /* This function returns all available data
505 from a specified dn
506 */
507 function getComputerInformations($name)
508 {
509 if($this->is_connected){
510 $ret = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
511 return($ret);
512 }else{
513 echo "not connected";
514 return(false);
515 }
516 }
518 /* This fucntions updates an already existing entry
519 */
520 function updateComputerInformations($array,$name)
521 {
522 if(!is_array($array)){
523 echo "updateComputerInformations: first paraeter must be an array";
524 }elseif($this->is_connected){
525 $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
526 if(isset($tmp[0])){
528 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
529 "tech_num","comments","date_mod","os","location","domain","network",
530 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
531 $v = "";
532 foreach($atr as $at){
533 if(isset($array[$at])){
534 $v .= " ".$at."='".$array[$at]."', ";
535 }
536 }
537 if(empty($v)){
538 echo "updateComputerInformations: no attributes given ";
539 return(false);
540 }else{
541 $v = preg_replace("/, $/","",$v);
542 return($this->query("UPDATE glpi_computers SET ".$v." WHERE name='".$name."';"));
543 }
544 }else{
545 echo "can't update not existing entry";
546 return(false);
547 }
548 }else{
549 echo "not connected";
550 return(false);
551 }
553 }
555 /* This function adds a new inventory device (computer phone etc)
556 */
557 function addComputerInformations($array)
558 {
559 if(!is_array($array)){
560 echo "updateComputerInformations: first paraeter must be an array";
561 }elseif($this->is_connected){
562 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
563 "tech_num","comments","date_mod","os","location","domain","network",
564 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
565 $v = "";
566 $a = "";
567 foreach($atr as $at){
568 if(isset($array[$at])){
569 $a .= $at.", ";
570 $v .= "'".$array[$at]."', ";
571 }
572 }
573 if(empty($v)){
574 echo "updateComputerInformations: no attributes given ";
575 return(false);
576 }else{
577 $a = preg_replace("/, $/","",$a);
578 $v = preg_replace("/, $/","",$v);
579 return($this->query("INSERT INTO glpi_computers (".$a.") VALUES (".$v.");"));
580 }
582 }else{
583 echo "not connected";
584 return(false);
585 }
587 }
589 /* this functions checks if the given Device
590 * already exists
591 */
592 function deviceExists($attr)
593 {
594 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
595 if($this->is_connected){
596 $arr = array_flip($deviceMappingGOsaGlpi);
598 $tbl_name = $arr[$attr['device_type']];
599 if(!isset($attr['ID'])){
600 return(false);
601 }else{
602 $qry = "SELECT * FROM ".$tbl_name." WHERE ID=".$attr['ID'].";";
603 $res = $this->query($qry);
604 if(count($res) != 0){
605 return(true);
606 }
607 }
608 }else{
609 echo "not connected";
610 return(false);
611 }
613 return(false);
614 }
617 /* Check if given device is used by some accounts
618 * (helpfull to avoid removement of used devices)
619 */
620 function is_deviceUsed($item)
621 {
622 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
623 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
624 if($this->is_connected){
625 $tablename = $deviceMappingGOsaGlpi[$item['device_type']];
626 $type = $item['device_type'];
628 $ret = array();
630 if($type=="monitor"){
631 $str = "SELECT c.name FROM glpi_connect_wire as w, glpi_computers as c WHERE w.end1=".$item['ID']." AND w.end2 = c.ID AND w.type=4;";
632 }else{
633 $str = "SELECT c.name FROM glpi_computer_device as d, glpi_computers as c WHERE d.FK_computers=c.ID AND FK_device=".$item['ID']." AND device_type=".$deviceMappingTableNameID[$type]." ;";
634 }
636 $res = $this->query($str);
638 foreach($res as $val){
639 $ret[$val['name']] = $val['name'];
640 }
642 return($ret);//count($this->query($str)));
643 }else{
644 echo "not connected";
645 return(false);
646 }
648 }
651 /* This functions deletes a specified entry
652 * from our device tables
653 */
654 function deleteDevice($attr)
655 {
656 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
657 if($this->is_connected){
658 $arr = array_flip($deviceMappingGOsaGlpi);
660 $device_type = $attr['device_type'];
661 unset($attr['device_type']);
663 $tbl_name = $arr[$device_type];
665 $this->query("DELETE FROM ".$tbl_name." WHERE ID=".$attr['ID'].";");
666 }else{
667 echo "not connected";
668 return(false);
669 }
670 }
672 /* This funtions updated an already existing device
673 */
674 function updateDevices($attr)
675 {
676 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
677 if($this->is_connected){
678 $arr = array_flip($deviceMappingGOsaGlpi);
680 $device_type = $attr['device_type'];
681 unset($attr['device_type']);
683 $tbl_name = $arr[$device_type];
685 $str = "UPDATE ".$tbl_name." SET ";
686 foreach($attr as $name => $value){
687 $str.=$name."='".$value."', ";
688 }
689 $str = preg_replace("/, $/","",$str);
690 $str .= " WHERE ID=".$attr['ID'].";";
691 $this->query($str);
692 }else{
693 echo "not connected";
694 return(false);
695 }
696 }
698 /* Returns all possible RAM types
699 * like SDRAM , DIMM .....
700 */
701 function getRAMTypes()
702 {
703 if($this->is_connected){
704 $ret = array();
705 $tmp = ($this->query("SELECT * FROM glpi_dropdown_ram_type;"));
706 foreach($tmp as $t){
707 $ret[$t['ID']]=$t['name'];
708 }
709 return($ret);
710 }else{
711 echo "not connected";
712 return(false);
713 }
714 }
716 /* Returns all possible HDD connection types
717 * like IDE SCSI ...
718 */
719 function getGlpiDeviceControlTypes()
720 {
721 if($this->is_connected){
722 $ret = array();
723 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
724 foreach($tmp as $t){
725 $ret[$t['ID']]=$t['name'];
726 }
727 return($ret);
728 }else{
729 echo "not connected";
730 return(false);
731 }
732 }
734 /* Returns all possible gfx card connection types
735 * like PCI-X PCI AGP ....
736 */
737 function getGlpiGfxControlTypes()
738 {
739 if($this->is_connected){
740 $ret = array();
741 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
742 foreach($tmp as $t){
743 $ret[$t['ID']]=$t['name'];
744 }
745 return($ret);
746 }else{
747 echo "not connected";
748 return(false);
749 }
750 }
752 /* Devices
753 Adds a new single device to our db
754 */
755 function addDevice($attr)
756 {
757 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
758 if($this->is_connected){
759 $arr = array_flip($deviceMappingGOsaGlpi);
761 $device_type = $attr['device_type'];
762 unset($attr['device_type']);
764 $tbl_name = $arr[$device_type];
765 $v = "";
766 $a = "";
767 foreach($attr as $name => $value){
768 $a .= $name.", ";
769 $v .= "'".$value."', ";
770 }
771 if(empty($v)){
772 echo "addDevice: no attributes given ";
773 return(false);
774 }else{
775 $a = preg_replace("/, $/","",$a);
776 $v = preg_replace("/, $/","",$v);
777 return($this->query("INSERT INTO ".$tbl_name." (".$a.") VALUES (".$v.");"));
778 }
780 }else{
781 echo "not connected";
782 return(false);
783 }
784 }
786 /* Return all available devices
787 */
788 function getDevices()
789 {
790 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
791 if($this->is_connected){
792 $arr = $deviceMappingGOsaGlpi;
794 $res = array();
795 foreach($arr as $glpi => $gosa){
796 $qry = "SELECT * FROM ".$glpi.";";
797 $ret = $this->query($qry);
798 foreach($ret as $id => $entry){
799 $entry['device_type'] = $gosa;
801 if(isset($entry['designation'])){
802 $res[$entry['designation']."-".$gosa] = $entry;
803 }else{
804 $res[$entry['name']."-".$gosa] = $entry;
805 }
806 }
807 }
808 return($res);
809 }else{
810 echo "not connected";
811 return(false);
812 }
813 }
815 /* This function returns all used devices
816 */
817 function getUsedDevices($computerID)
818 {
819 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
820 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
822 if($this->is_connected){
823 $qry = "SELECT * FROM glpi_computer_device WHERE FK_computers=".$computerID.";";
824 $res = $this->query($qry);
826 $ret = array();
827 foreach($deviceMappingGOsaGlpi as $GOsa => $glpi){
828 $ret[$GOsa] = array();
829 }
831 $tbls = array_flip($deviceMappingTableNameID);
833 foreach($res as $device){
834 $devtype = $tbls[$device['device_type']];
835 $tbl_name = $deviceMappingGOsaGlpi[$devtype];
836 $qry = ("SELECT * FROM ".$tbl_name." WHERE ID=".$device['FK_device'].";");
837 $res2 = $this->query($qry);
838 if(count($res2)!=0){
839 $ret[$devtype][$res2[0]['designation']]=$res2[0];
840 }
842 $qry = "SELECT * FROM glpi_connect_wire WHERE type=4 AND end2=".$computerID.";";
843 $res2 = $this->query($qry);
844 foreach($res2 as $monitor){
845 $qry = "SELECT * FROM glpi_monitors WHERE ID=".$monitor['end1'].";";
846 $res3 = $this->query($qry);
847 foreach($res3 as $moni){
848 $ret['monitor'][$moni['name']]=$moni;
849 }
850 }
854 }
855 return($ret);
856 }else{
857 echo "not connected";
858 return(false);
859 }
860 }
862 /* This function removes all given devices from a computer, specified by $id
863 In the next step all devices specified by devices will be added.
864 */
865 function addDevicesToComputer($devices, $id)
866 {
867 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
868 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
870 if(($id == "" )||(!is_numeric($id))){
871 return (false);
872 }
873 if($this->is_connected){
874 $qry = "DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";";
875 $this->query($qry);
877 foreach($devices as $type => $entries){
878 foreach($entries as $entry){
879 if($type=="monitor"){
880 $str = "INSERT INTO glpi_connect_wire (end1,end2,type)
881 VALUES (".$entry['ID'].",".$id.",4);";
882 }else{
883 $str = "INSERT INTO glpi_computer_device (device_type,FK_device,FK_computers)
884 VALUES (".$deviceMappingTableNameID[$type].",".$entry['ID'].",".$id.");";
885 }
886 $this->query($str);
887 }
888 }
891 }else{
892 echo "not connected";
893 return(false);
894 }
896 }
898 function removeComputerInformations($name)
899 {
900 if($this->is_connected){
901 $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
902 if(isset($tmp[0])){
903 $id = $tmp[0]['ID'];
904 $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
905 $this->query("DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";");
906 return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
907 }else{
908 echo "can't remove not existing entry";
909 return(false);
910 }
911 }else{
912 echo "not connected";
913 return(false);
914 }
915 }
917 function is_connected()
918 {
919 return($this->is_connected);
920 }
922 function addAttachmentsToComputer($attr,$id)
923 {
924 if(($id == "" )||(!is_numeric($id))){
925 return (false);
926 }
927 if($this->is_connected){
928 $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=1);";
929 $this->query($qry);
931 foreach($attr as $aid => $entry){
932 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template)
933 VALUES
934 ($aid,$id,1,'0');";
935 $this->query($str);
936 }
937 }else{
938 echo "not connected";
939 return(false);
940 }
941 }
943 function getAssignAttachments($id)
944 {
946 if($this->is_connected){
947 $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=1) AND (FK_device=".$id.");";
948 $ret = $this->query($qry);
949 return($ret);
950 }else{
951 echo "not connected";
952 return(false);
953 }
954 }
956 function deleteAttachment($id)
957 {
958 if($this->is_connected){
959 $qry = "DELETE FROM glpi_docs WHERE ID=".$id."";
960 $this->query($qry);
961 }else{
962 echo "not connected";
963 return(false);
964 }
965 }
967 function getAttachments()
968 {
969 $ret = array();
970 if($this->is_connected){
971 $qry = "SELECT * FROM glpi_docs WHERE name!='';";
972 $re = $this->query($qry);
974 foreach($re as $entry){
975 $ret[$entry['ID']]=$entry;
976 }
978 return($ret);
979 }else{
980 echo "not connected";
981 return(false);
982 }
983 }
985 function saveAttachments($attrs,$id = -1)
986 {
987 if($this->is_connected){
988 $atr = array("name","filename","rubrique","mime","date_mod","comment","deleted","link");
989 $tmp = array();
990 foreach($atr as $at){
991 if(isset($attrs[$at])){
992 $tmp[$at] = $attrs[$at];
993 }
994 }
995 if(count($tmp)==0){
996 return(false);
997 }else{
999 // Add
1000 if($id == -1){
1001 $str = "INSERT INTO glpi_docs ";
1002 $namen = "";
1003 $values= "";
1004 foreach($tmp as $name => $value){
1005 $namen .= $name.", ";
1006 if(is_numeric($value)){
1007 $values .= $value.", ";
1008 }else{
1009 $values .= "'".$value."', ";
1010 }
1011 }
1012 $values = preg_replace("/, $/","",$values);
1013 $namen = preg_replace("/, $/","",$namen);
1014 $str .= "(".$namen.") VALUES (".$values.");";
1015 }else{
1016 $str = "UPDATE glpi_docs SET ";
1017 foreach($tmp as $name => $value){
1018 $str .= $name."= ";
1019 if(is_numeric($value)){
1020 $str .= $value.", ";
1021 }else{
1022 $str .= "'".$value."', ";
1023 }
1024 }
1025 $str = preg_replace("/, $/","",$str);
1026 $str .= " WHERE ID=".$id.";";
1027 }
1028 $this->query($str);
1029 }
1030 }else{
1031 echo "not connected";
1032 return(false);
1033 }
1034 }
1037 /* Check if given attachment id is used in any Device
1038 ( - avoid removing of used attachments)
1039 */
1040 function is_attachmentUsed($id)
1041 {
1042 if($this->is_connected){
1043 $ret = array();
1044 $qry = "SELECT t.name FROM glpi_computers as t, glpi_doc_device WHERE t.ID = glpi_doc_device.FK_device AND FK_doc =".$id." LIMIT 3;";
1045 $res = $this->query($qry);
1046 foreach($res as $val){
1047 $ret[$val['name']] = $val['name'];
1048 }
1049 return($ret);
1050 }else{
1051 echo "not connected";
1052 return(false);
1053 }
1054 }
1057 /* Monitor handling
1058 */
1059 function getMonitors()
1060 {
1061 if($this->is_connected){
1062 $qry= "SELECT * FROM glpi_monitors;";
1063 return($this->query($qry));
1065 }else{
1066 echo "not connected";
1067 return(false);
1068 }
1069 }
1071 function updatedMonitor()
1072 {
1073 if($this->is_connected){
1074 // $qry= "SELECT * FROM glpi_monitors;";
1075 // return($this->query($qry));
1077 }else{
1078 echo "not connected";
1079 return(false);
1080 }
1081 }
1083 function addMonitor()
1084 {
1085 if($this->is_connected){
1086 // $qry= "SELECT * FROM glpi_monitors;";
1087 // return($this->query($qry));
1089 }else{
1090 echo "not connected";
1091 return(false);
1092 }
1093 }
1095 function removeMonitor($id)
1096 {
1097 if($this->is_connected){
1098 $qry= "DELETE FROM glpi_monitors WHERE ID=".$id.";";
1099 $this->query($qry);
1100 }else{
1101 echo "not connected";
1102 return(false);
1103 }
1104 }
1106 function getMonitorTypes()
1107 {
1108 if($this->is_connected){
1109 $qry= "SELECT * FROM glpi_type_monitors;";
1110 return($this->query($qry));
1112 }else{
1113 echo "not connected";
1114 return(false);
1115 }
1116 }
1118 function getLocationTypes()
1119 {
1120 if($this->is_connected){
1121 $qry= "SELECT * FROM glpi_dropdown_locations;";
1122 return($this->query($qry));
1124 }else{
1125 echo "not connected";
1126 return(false);
1127 }
1128 }
1130 function getStateTypes()
1131 {
1132 if($this->is_connected){
1133 $qry= "SELECT * FROM glpi_dropdown_state;";
1134 return($this->query($qry));
1135 }else{
1136 echo "not connected";
1137 return(false);
1138 }
1139 }
1142 /* Printer functions
1143 */
1146 /* This functions checks if the selected computer/network
1147 device is already available in the db
1148 */
1149 function is_printer_account($dn)
1150 {
1151 if(!$this->is_connected){
1152 $this->lasterror ="Can't query anything, if we aren't connected.";
1153 return(false);
1154 }else{
1155 $qry = "SELECT * FROM glpi_printers WHERE name='".$dn."';";
1156 $res = $this->query($qry);
1157 if(count($res)==0){
1158 return(false);
1159 }else{
1160 return(true);
1161 }
1162 }
1163 }
1165 /* This function returns all available data
1166 from a specified dn
1167 */
1168 function getPrinterInformations($name)
1169 {
1170 if($this->is_connected){
1171 $ret = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1172 return($ret);
1173 }else{
1174 echo "not connected";
1175 return(false);
1176 }
1177 }
1179 /* Get Printer attachments
1180 */
1181 function getAssignPrinterAttachments($id)
1182 {
1184 if($this->is_connected){
1185 $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=3) AND (FK_device=".$id.");";
1186 $ret = $this->query($qry);
1187 return($ret);
1188 }else{
1189 echo "not connected";
1190 return(false);
1191 }
1192 }
1194 /* Printer types
1195 Returns all defined printer types
1196 */
1197 function getPrinterTypes()
1198 {
1199 if($this->is_connected){
1200 $ret = array();
1201 $tmp = ($this->query("SELECT * FROM glpi_type_printers;"));
1202 foreach($tmp as $t){
1203 $ret[$t['ID']]=$t['name'];
1204 }
1205 return($ret);
1206 }else{
1207 echo "not connected";
1208 return(false);
1209 }
1210 }
1212 /* Add pritner types
1213 Add one entry to the printer types
1214 */
1215 function addPrinterType($name)
1216 {
1217 if($this->is_connected){
1218 $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE name='".$name."';");
1219 if(isset($tmp[0])){
1220 //echo "such an entry already exists";
1221 return(false);
1222 }else{
1223 return($this->query("INSERT INTO glpi_type_printers (name) VALUES ('".$name."');"));
1224 }
1225 }else{
1226 echo "not connected";
1227 return(false);
1228 }
1229 }
1231 /* remove printer types
1232 Remove one entry from the printer types (specified by ID=$id)
1233 */
1234 function removePrinterType($id)
1235 {
1236 if($this->is_connected){
1237 $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1238 if(isset($tmp[0])){
1239 return($this->query("DELETE FROM glpi_type_printers WHERE ID=".$id.";"));
1240 }else{
1241 echo "can't remove not existing entry";
1242 return(false);
1243 }
1244 }else{
1245 echo "not connected";
1246 return(false);
1247 }
1248 }
1250 /* Update printer types
1251 Update a printer type
1252 */
1253 function updatePrinterType($name,$id)
1254 {
1256 if($this->is_connected){
1257 $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1258 if(isset($tmp[0])){
1259 return($this->query("UPDATE glpi_type_printers SET name='".$name."' WHERE ID=".$id.";"));
1260 }else{
1261 echo "can't update not existing entry";
1262 return(false);
1263 }
1264 }else{
1265 echo "not connected";
1266 return(false);
1267 }
1268 }
1271 /* This fucntions updates an already existing entry
1272 */
1273 function updatePrinterInformations($array,$name)
1274 {
1275 if(!is_array($array)){
1276 echo "updatePrinterInformations: first paraeter must be an array";
1277 }elseif($this->is_connected){
1278 $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1279 if(isset($tmp[0])){
1281 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
1282 "tech_num","comments","date_mod","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1283 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1284 $v = "";
1285 foreach($atr as $at){
1286 if(isset($array[$at])){
1287 $v .= " ".$at."='".$array[$at]."', ";
1288 }
1289 }
1290 if(empty($v)){
1291 echo "updateSystemInformations: no attributes given ";
1292 return(false);
1293 }else{
1294 $v = preg_replace("/, $/","",$v);
1295 return($this->query("UPDATE glpi_printers SET ".$v." WHERE name='".$name."';"));
1296 }
1297 }else{
1298 echo "can't update not existing entry";
1299 return(false);
1300 }
1301 }else{
1302 echo "not connected";
1303 return(false);
1304 }
1306 }
1308 /* This function adds a new inventory settings for printers
1309 */
1310 function addPrinterInformations($array)
1311 {
1312 if(!is_array($array)){
1313 echo "updateComputerInformations: first paraeter must be an array";
1314 }elseif($this->is_connected){
1315 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
1316 "tech_num","comments","date_mod","os","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1317 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1318 $v = "";
1319 $a = "";
1320 foreach($atr as $at){
1321 if(isset($array[$at])){
1322 $a .= $at.", ";
1323 $v .= "'".$array[$at]."', ";
1324 }
1325 }
1326 if(empty($v)){
1327 echo "updateComputerInformations: no attributes given ";
1328 return(false);
1329 }else{
1330 $a = preg_replace("/, $/","",$a);
1331 $v = preg_replace("/, $/","",$v);
1332 return($this->query("INSERT INTO glpi_printers (".$a.") VALUES (".$v.");"));
1333 }
1335 }else{
1336 echo "not connected";
1337 return(false);
1338 }
1339 }
1341 /* add atachment to given printer */
1342 function addAttachmentsToPrinter($attr,$id)
1343 {
1344 if(($id == "" )||(!is_numeric($id))){
1345 return (false);
1346 }
1347 if($this->is_connected){
1348 $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=3);";
1349 $this->query($qry);
1351 foreach($attr as $aid => $entry){
1352 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template)
1353 VALUES
1354 ($aid,$id,3,'0');";
1355 $this->query($str);
1356 }
1357 }else{
1358 echo "not connected";
1359 return(false);
1360 }
1361 }
1363 function removePrinterInformations($name)
1364 {
1365 if($this->is_connected){
1366 $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1367 if(isset($tmp[0])){
1368 $id = $tmp[0]['ID'];
1369 // $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
1370 $this->query("DELETE FROM glpi_doc_device WHERE FK_device=".$id." AND device_type=3;");
1371 return($this->query("DELETE FROM glpi_printers WHERE ID=".$id.";"));
1372 }else{
1373 echo "can't remove not existing entry";
1374 return(false);
1375 }
1376 }else{
1377 echo "not connected";
1378 return(false);
1379 }
1380 }
1383 /* Cartridges
1384 */
1386 /* return all assigned cartridges */
1387 function getUsedCartridges($printerID)
1388 {
1389 if($this->is_connected){
1390 $ret = array();
1391 $qry = "SELECT
1392 c.date_use as date_use,
1393 c.ID as ID,
1394 t.ID as type_ID,
1395 t.name as name,
1396 c.FK_glpi_printers as FK_glpi_printers,
1397 d.name as type_name
1398 FROM
1399 glpi_dropdown_cartridge_type as d,
1400 glpi_cartridges as c,
1401 glpi_cartridges_type as t
1402 WHERE c.FK_glpi_cartridges_type = t.ID
1403 AND t.type = d.ID
1404 AND c.FK_glpi_printers = ".$printerID.";";
1405 $res = $this->query($qry);
1406 foreach($res as $entry){
1407 $ret[$entry['ID']] = $entry;
1408 }
1409 return($ret);
1410 }else{
1411 echo "not connected";
1412 return(false);
1413 }
1414 }
1416 /* return all assigned cartridges */
1417 function getAvailableCartridgeTypes($printerTypeID)
1418 {
1419 if($this->is_connected){
1420 $ret = array();
1421 $qry= "
1422 SELECT
1423 ct.ID as cartridgeID,
1424 ct.name as cartridgeName,
1425 pt.ID as printerTypeID,
1426 pt.name as printerTypeName,
1427 ct.type as cartridgeTypeID,
1428 dt.name as cartridgeTypeName
1429 FROM
1430 glpi_type_printers as pt,
1431 glpi_cartridges_type as ct,
1432 glpi_dropdown_cartridge_type as dt,
1433 glpi_cartridges_assoc as ac
1434 WHERE
1435 ac.FK_glpi_type_printer = pt.ID
1436 AND ac.FK_glpi_cartridges_type = ct.ID
1437 AND ct.type=dt.ID
1438 AND pt.ID=".$printerTypeID.";";
1439 $res = $this->query($qry);
1440 foreach($res as $entry){
1441 $ret[$entry['cartridgeID']] = $entry;
1442 }
1443 return($ret);
1444 }else{
1445 echo "not connected";
1446 return(false);
1447 }
1448 }
1450 function removeCartridgeFromPrinter($cartridgeID)
1451 {
1452 if($this->is_connected){
1453 $qry = "DELETE FROM glpi_cartridges WHERE ID=".$cartridgeID.";";
1454 return($this->query($qry));
1455 }else{
1456 echo "not connected";
1457 return(false);
1458 }
1459 }
1461 function addCartridgeFromPrinter($printerID,$cartridgeID)
1462 {
1463 if($this->is_connected){
1464 $qry ="INSERT INTO
1465 glpi_cartridges (FK_glpi_cartridges_type,FK_glpi_printers,date_in,date_use)
1466 VALUES
1467 (".$cartridgeID.",".$printerID.",'".date("Y-m-d")."','".date("Y-m-d")."');";
1468 return($this->query($qry));
1469 }else{
1470 echo "not connected";
1471 return(false);
1472 }
1473 }
1475 function getCartridgeTypeInformations($id = "all"){
1476 if($this->is_connected){
1477 $ret = array();
1478 if($id != "all"){
1479 $qry = "SELECT * FROM glpi_cartridges_type WHERE ID = ".$id.";";
1480 }else{
1481 $qry = "SELECT * FROM glpi_cartridges_type;";
1482 }
1484 $res = ($this->query($qry));
1485 foreach($res as $entry){
1486 $ret[$entry['ID']] = $entry;
1487 }
1488 return($ret);
1490 }else{
1491 echo "not connected";
1492 return(false);
1493 }
1494 }
1496 function getCartridgeTypes(){
1497 if($this->is_connected){
1498 $ret = array();
1499 $qry = "SELECT * FROM glpi_dropdown_cartridge_type;";
1500 $res = ($this->query($qry));
1501 foreach($res as $entry){
1502 $ret[$entry['ID']] = $entry['name'];
1503 }
1504 return($ret);
1506 }else{
1507 echo "not connected";
1508 return(false);
1509 }
1510 }
1513 /* Manufacturer
1514 Updates already existing manufacturer
1515 */
1516 function Add_UpdateCatrigdeType($array,$array_printer_types)
1517 {
1518 if(!is_array($array)){
1519 echo "Add_UpdateCatrigdeType: first paraeter must be an array";
1520 }elseif($this->is_connected){
1523 $atr = array("name","ref","location","type","FK_glpi_enterprise","tech_num","deleted","comments","alarm");
1525 /* Entry was edited */
1526 if($array['ID']>0){
1527 $qry = "DELETE FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type=".$array['ID'].";";
1529 $v = "";
1530 foreach($atr as $at){
1531 if(isset($array[$at])){
1532 $v .= " ".$at."='".$array[$at]."', ";
1533 }
1534 }
1535 if(empty($v)){
1536 echo "Add_UpdateCatrigdeType: no attributes given ";
1537 return(false);
1538 }else{
1539 $v = preg_replace("/, $/","",$v);
1540 $qry = "UPDATE glpi_cartridges_type SET ".$v." WHERE ID='".$array['ID']."';";
1541 $this->query($qry);
1542 }
1543 }else{
1545 /* skip if name is in use*/
1546 $qry = "SELECT * FROM glpi_cartridges_type WHERE name='".$array['name']."';";
1547 if(count($this->query($qry))){
1548 return;
1549 }
1551 $str = "INSERT INTO glpi_cartridges_type ";
1552 $namen = "";
1553 $values= "";
1554 foreach($array as $name => $value){
1555 $namen .= $name.", ";
1556 if(is_numeric($value)){
1557 $values .= $value.", ";
1558 }else{
1559 $values .= "'".$value."', ";
1560 }
1561 }
1562 $values = preg_replace("/, $/","",$values);
1563 $namen = preg_replace("/, $/","",$namen);
1564 $str .= "(".$namen.") VALUES (".$values.");";
1565 $this->query($str);
1566 $IDs = $this->query("SELECT ID FROM glpi_cartridges_type WHERE name='".$array['name']."';");
1567 if(count($IDs) > 1){
1568 echo "internal db error";
1569 return;
1570 }
1571 $array['ID'] = $IDs[0]['ID'];
1572 }
1574 foreach($array_printer_types as $id){
1575 $qry = "INSERT INTO glpi_cartridges_assoc
1576 (FK_glpi_cartridges_type,FK_glpi_type_printer)
1577 VALUES
1578 (".$array['ID'].",".$id.")";
1580 $this->query($qry);
1581 }
1582 }else{
1583 echo "not connected";
1584 return(false);
1585 }
1586 }
1588 function getSupportedPrinterTypeIDsForCartridge($cid)
1589 {
1590 if($this->is_connected){
1591 $ret = array();
1592 $qry = "SELECT FK_glpi_type_printer FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type = ".$cid.";";
1593 $res = $this->query($qry);
1595 foreach($res as $entry => $value){
1596 $ret[$value['FK_glpi_type_printer']] = $value['FK_glpi_type_printer'];
1597 }
1598 return($ret);
1599 }else{
1600 echo "not connected";
1601 return(false);
1602 }
1603 }
1605 function removeCartridgeType($id){
1606 if($this->is_connected){
1607 $qry = "DELETE FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type=".$id.";";
1608 $this->query($qry);
1609 $qry = "DELETE FROM glpi_cartridges_type WHERE ID=".$id.";";
1610 return($this->query($qry));
1611 }else{
1612 echo "not connected";
1613 return(false);
1614 }
1615 }
1617 function getCartridgesWhichUseThisType($id)
1618 {
1619 if($this->is_connected){
1620 $qry = "SELECT * FROM glpi_cartridges WHERE FK_glpi_cartridges_type=".$id.";";
1621 $ret = $this->query($qry);
1622 return($ret);
1623 }else{
1624 echo "not connected";
1625 return(false);
1626 }
1627 }
1630 /* Add pritner types
1631 Add one entry to the cartridgeType types
1632 */
1633 function addCartridgeDropdownType($name)
1634 {
1635 if($this->is_connected){
1636 $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE name='".$name."';");
1637 if(isset($tmp[0])){
1638 //echo "such an entry already exists";
1639 return(false);
1640 }else{
1641 return($this->query("INSERT INTO glpi_dropdown_cartridge_type (name) VALUES ('".$name."');"));
1642 }
1643 }else{
1644 echo "not connected";
1645 return(false);
1646 }
1647 }
1649 /* remove cartridgeType types
1650 Remove one entry from the cartridgeType types (specified by ID=$id)
1651 */
1652 function removeCartridgeDropdownType($id)
1653 {
1654 if($this->is_connected){
1655 $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";");
1656 if(isset($tmp[0])){
1657 return($this->query("DELETE FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";"));
1658 }else{
1659 echo "can't remove not existing entry";
1660 return(false);
1661 }
1662 }else{
1663 echo "not connected";
1664 return(false);
1665 }
1666 }
1668 /* Update cartridgeType
1669 Update a cartridgeType
1670 */
1671 function updateCartridgeDropdownType($name,$id)
1672 {
1674 if($this->is_connected){
1675 $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";");
1676 if(isset($tmp[0])){
1677 return($this->query("UPDATE glpi_dropdown_cartridge_type SET name='".$name."' WHERE ID=".$id.";"));
1678 }else{
1679 echo "can't update not existing entry";
1680 return(false);
1681 }
1682 }else{
1683 echo "not connected";
1684 return(false);
1685 }
1686 }
1688 function getUsedDropdownTypes($id=false)
1689 {
1690 if($this->is_connected){
1691 if($id){
1692 $qry = "SELECT distinct(type) FROM glpi_cartridges_type WHERE type = ".$id.";";
1693 }else{
1694 $qry = "SELECT distinct(type) FROM glpi_cartridges_type;";
1695 }
1696 return($this->query($qry));
1697 }else{
1698 echo "not connected";
1699 return(false);
1700 }
1701 }
1703 }
1704 //$s = new glpiDB("vserver-01","glpi","tester","glpi");
1705 //print_r($s->query("SELECT * FROM glpi_computers"));
1706 //$s->getComputerInformations("1 OR (c.ID<10000)");
1707 ?>