67f02d7aff127bfe14c7dee2532f4a20e2d1094a
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 return($ret);
119 }else{
120 echo "not connected";
121 return(false);
122 }
123 }
125 /* System types
126 Update a system type
127 */
128 function updateSystemType($name,$id)
129 {
131 if($this->is_connected){
132 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
133 if(isset($tmp[0])){
134 return($this->query("UPDATE glpi_type_computers SET name='".$name."' WHERE ID=".$id.";"));
135 }else{
136 echo "can't update not existing entry";
137 return(false);
138 }
139 }else{
140 echo "not connected";
141 return(false);
142 }
143 }
145 /* System types
146 Add one entry to the system types
147 */
148 function addSystemType($name)
149 {
150 if($this->is_connected){
151 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE name='".$name."';");
152 if(isset($tmp[0])){
153 echo "such an entry already exists";
154 return(false);
155 }else{
156 return($this->query("INSERT INTO glpi_type_computers (name) VALUES ('".$name."');"));
157 }
158 }else{
159 echo "not connected";
160 return(false);
161 }
162 }
164 /* System types
165 Remove one entry from the system types (specified by ID=$id)
166 */
167 function removeSystemType($id)
168 {
169 if($this->is_connected){
170 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
171 if(isset($tmp[0])){
172 return($this->query("DELETE FROM glpi_type_computers WHERE ID=".$id.";"));
173 }else{
174 echo "can't remove not existing entry";
175 return(false);
176 }
177 }else{
178 echo "not connected";
179 return(false);
180 }
181 }
183 /* Manufacturer
184 Returns all defined manufacturers
185 */
186 function getEnterprises()
187 {
188 if($this->is_connected){
189 $ret = array();
190 $tmp = $this->query("SELECT * FROM glpi_enterprises;");
191 foreach($tmp as $t){
192 $ret[$t['ID']]=$t['name'];
193 }
194 return($ret);
195 }else{
196 echo "not connected";
197 return(false);
198 }
199 }
201 /* Manufacturer
202 Returns single manufacturer
203 */
204 function getEnterprise($id)
205 {
206 if($this->is_connected){
207 $ret = array();
208 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
209 return($tmp);
210 }else{
211 echo "not connected";
212 return(false);
213 }
214 }
216 /* Manufacturer
217 Updates already existing manufacturer
218 */
219 function updateEnterprise($array,$id)
220 {
221 if(!is_array($array)){
222 echo "updateEnterprisesType: first paraeter must be an array";
223 }elseif($this->is_connected){
224 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID='".$id."';");
225 if(isset($tmp[0])){
226 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
228 $v = "";
229 foreach($atr as $at){
230 if(isset($array[$at])){
231 $v .= " ".$at."='".$array[$at]."', ";
232 }
233 }
234 if(empty($v)){
235 echo "updateEnterprisesType: no attributes given ";
236 return(false);
237 }else{
238 $v = preg_replace("/, $/","",$v);
239 return($this->query("UPDATE glpi_enterprises SET ".$v." WHERE ID='".$id."';"));
240 }
241 }else{
242 echo "can't update not existing entry";
243 return(false);
244 }
245 }else{
246 echo "not connected";
247 return(false);
248 }
249 }
251 /* Manufacturer
252 Add new manufacturer
253 */
254 function addEnterprise($array)
255 {
256 if(!is_array($array)){
257 echo "addUser: first paraeter must be an array";
258 }elseif($this->is_connected){
259 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
260 $v = "";
261 $a = "";
262 foreach($atr as $at){
263 if(isset($array[$at])){
264 $a .= $at.", ";
265 $v .= "'".$array[$at]."', ";
266 }
267 }
268 if(empty($v)){
269 echo "addUser: no attributes given ";
270 return(false);
271 }else{
272 $a = preg_replace("/, $/","",$a);
273 $v = preg_replace("/, $/","",$v);
274 return($this->query("INSERT INTO glpi_enterprises (".$a.") VALUES (".$v.");"));
275 }
277 }else{
278 echo "not connected";
279 return(false);
280 }
282 }
284 /* Manufacturer
285 remove manufacturer
286 */
287 function removeEnterprise($id)
288 {
289 if($this->is_connected){
290 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
291 if(isset($tmp[0])){
292 return($this->query("DELETE FROM glpi_enterprises WHERE ID=".$id.";"));
293 }else{
294 echo "can't remove not existing entry";
295 return(false);
296 }
297 }else{
298 echo "not connected";
299 return(false);
300 }
301 }
303 /* Operating systems
304 Returns all OSs
305 */
306 function getOSTypes()
307 {
308 if($this->is_connected){
309 $ret = array();
310 $tmp=($this->query("SELECT * FROM glpi_dropdown_os;"));
312 foreach($tmp as $t){
313 $ret[$t['ID']]=$t['name'];
314 }
316 return($ret);
318 }else{
319 echo "not connected";
320 return(false);
321 }
322 }
324 /* Operating systems
325 Add a new operating system to the dropdown menus
326 */
327 function addOS($name)
328 {
329 if($this->is_connected){
330 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE name='".$name."';");
331 if(isset($tmp[0])){
332 echo "such an entry already exists";
333 return(false);
334 }else{
335 return($this->query("INSERT INTO glpi_dropdown_os (name) VALUES ('".$name."');"));
336 }
337 }else{
338 echo "not connected";
339 return(false);
340 }
341 }
343 /* Operating systems
344 remove one OS entry
345 */
346 function removeOS_byID($id)
347 {
348 if($this->is_connected){
349 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
350 if(is_array($tmp[0])){
351 return($this->query("DELETE FROM glpi_dropdown_os WHERE ID=".$id.";"));
352 }else{
353 echo "can't remove not existing entry";
354 return(false);
355 }
356 }else{
357 echo "not connected";
358 return(false);
359 }
360 }
362 /* Operating systems
363 Update existing OS entry
364 */
365 function updateOS($name,$id)
366 {
368 if($this->is_connected){
369 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
370 if(isset($tmp[0])){
371 return($this->query("UPDATE glpi_dropdown_os SET name='".$name."' WHERE ID=".$id.";"));
372 }else{
373 echo "can't update not existing entry";
374 return(false);
375 }
376 }else{
377 echo "not connected";
378 return(false);
379 }
380 }
382 /* This returns all available glpi users
383 */
384 function getUsers()
385 {
386 if($this->is_connected){
387 $ret = array();
388 $tmp = ($this->query("SELECT * FROM glpi_users"));
389 foreach($tmp as $user){
390 $ret[$user['ID']]=$user['name'];
391 }
392 return($ret);
394 }else{
395 echo "not connected";
396 return(false);
397 }
398 }
400 /* this function adds a new glpi user
401 */
402 function addUser($array,$dn)
403 {
404 if(!is_array($array)){
405 echo "addUser: first paraeter must be an array";
406 }elseif($this->is_connected){
407 $array['name']=$dn;
408 $atr = array("name","phone","email");
409 $v = "";
410 $a = "";
411 foreach($atr as $at){
412 if(isset($array[$at])){
413 $a .= $at.", ";
414 $v .= "'".$array[$at]."', ";
415 }
416 }
417 if(empty($v)){
418 echo "addUser: no attributes given ";
419 return(false);
420 }else{
421 $a = preg_replace("/, $/","",$a);
422 $v = preg_replace("/, $/","",$v);
423 return($this->query("INSERT INTO glpi_users (".$a.") VALUES (".$v.");"));
424 }
426 }else{
427 echo "not connected";
428 return(false);
429 }
431 }
433 /* This function updates a glpi user
434 with the given data
435 */
436 function updateUser($array,$dn)
437 {
438 if(!is_array($array)){
439 echo "updateUser: first paraeter must be an array";
440 }elseif($this->is_connected){
441 $tmp = $this->query("SELECT * FROM glpi_users WHERE name='".$dn."';");
442 if(isset($tmp[0])){
444 $atr = array("name","phone","email");
445 $v = "";
446 foreach($atr as $at){
447 if(isset($array[$at])){
448 $v .= " ".$at."='".$array[$at]."', ";
449 }
450 }
451 if(empty($v)){
452 echo "UpdateUser: no attributes given ";
453 return(false);
454 }else{
455 $v = preg_replace("/, $/","",$v);
456 return($this->query("UPDATE glpi_users SET ".$v." WHERE name='".$dn."';"));
457 }
458 }else{
459 echo "can't update not existing entry";
460 return(false);
461 }
462 }else{
463 echo "not connected";
464 return(false);
465 }
467 }
469 /* This function returns all available data
470 from a specified dn
471 */
472 function getComputerInformations($name)
473 {
474 if($this->is_connected){
475 $ret = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
476 return($ret);
477 }else{
478 echo "not connected";
479 return(false);
480 }
481 }
483 /* This fucntions updates an already existing entry
484 */
485 function updateComputerInformations($array,$name)
486 {
487 if(!is_array($array)){
488 echo "updateComputerInformations: first paraeter must be an array";
489 }elseif($this->is_connected){
490 $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
491 if(isset($tmp[0])){
493 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
494 "tech_num","comments","date_mod","os","location","domain","network",
495 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
496 $v = "";
497 foreach($atr as $at){
498 if(isset($array[$at])){
499 $v .= " ".$at."='".$array[$at]."', ";
500 }
501 }
502 if(empty($v)){
503 echo "updateComputerInformations: no attributes given ";
504 return(false);
505 }else{
506 $v = preg_replace("/, $/","",$v);
507 return($this->query("UPDATE glpi_computers SET ".$v." WHERE name='".$name."';"));
508 }
509 }else{
510 echo "can't update not existing entry";
511 return(false);
512 }
513 }else{
514 echo "not connected";
515 return(false);
516 }
518 }
520 /* This function adds a new inventory device (computer phone etc)
521 */
522 function addComputerInformations($array)
523 {
524 if(!is_array($array)){
525 echo "updateComputerInformations: first paraeter must be an array";
526 }elseif($this->is_connected){
527 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
528 "tech_num","comments","date_mod","os","location","domain","network",
529 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
530 $v = "";
531 $a = "";
532 foreach($atr as $at){
533 if(isset($array[$at])){
534 $a .= $at.", ";
535 $v .= "'".$array[$at]."', ";
536 }
537 }
538 if(empty($v)){
539 echo "updateComputerInformations: no attributes given ";
540 return(false);
541 }else{
542 $a = preg_replace("/, $/","",$a);
543 $v = preg_replace("/, $/","",$v);
544 return($this->query("INSERT INTO glpi_computers (".$a.") VALUES (".$v.");"));
545 }
547 }else{
548 echo "not connected";
549 return(false);
550 }
552 }
554 /* this functions checks if the given Device
555 * already exists
556 */
557 function deviceExists($attr)
558 {
559 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
560 if($this->is_connected){
561 $arr = array_flip($deviceMappingGOsaGlpi);
563 $tbl_name = $arr[$attr['device_type']];
564 if(!isset($attr['ID'])){
565 return(false);
566 }else{
567 $qry = "SELECT * FROM ".$tbl_name." WHERE ID=".$attr['ID'].";";
568 $res = $this->query($qry);
569 if(count($res) != 0){
570 return(true);
571 }
572 }
573 }else{
574 echo "not connected";
575 return(false);
576 }
578 return(false);
579 }
581 /* This functions deletes a specified entry
582 * from our device tables
583 */
584 function deleteDevice($attr)
585 {
586 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
587 if($this->is_connected){
588 $arr = array_flip($deviceMappingGOsaGlpi);
590 $device_type = $attr['device_type'];
591 unset($attr['device_type']);
593 $tbl_name = $arr[$device_type];
595 $this->query("DELETE FROM ".$tbl_name." WHERE ID=".$attr['ID'].";");
596 }else{
597 echo "not connected";
598 return(false);
599 }
600 }
602 /* This funtions updated an already existing device
603 */
604 function updateDevices($attr)
605 {
606 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
607 if($this->is_connected){
608 $arr = array_flip($deviceMappingGOsaGlpi);
610 $device_type = $attr['device_type'];
611 unset($attr['device_type']);
613 $tbl_name = $arr[$device_type];
615 $str = "UPDATE ".$tbl_name." SET ";
616 foreach($attr as $name => $value){
617 $str.=$name."='".$value."', ";
618 }
619 $str = preg_replace("/, $/","",$str);
620 $str .= " WHERE ID=".$attr['ID'].";";
621 $this->query($str);
622 }else{
623 echo "not connected";
624 return(false);
625 }
626 }
628 /* Returns all possible RAM types
629 * like SDRAM , DIMM .....
630 */
631 function getRAMTypes()
632 {
633 if($this->is_connected){
634 $ret = array();
635 $tmp = ($this->query("SELECT * FROM glpi_dropdown_ram_type;"));
636 foreach($tmp as $t){
637 $ret[$t['ID']]=$t['name'];
638 }
639 return($ret);
640 }else{
641 echo "not connected";
642 return(false);
643 }
644 }
646 /* Returns all possible HDD connection types
647 * like IDE SCSI ...
648 */
649 function getGlpiDeviceControlTypes()
650 {
651 if($this->is_connected){
652 $ret = array();
653 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
654 foreach($tmp as $t){
655 $ret[$t['ID']]=$t['name'];
656 }
657 return($ret);
658 }else{
659 echo "not connected";
660 return(false);
661 }
662 }
664 /* Returns all possible gfx card connection types
665 * like PCI-X PCI AGP ....
666 */
667 function getGlpiGfxControlTypes()
668 {
669 if($this->is_connected){
670 $ret = array();
671 $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
672 foreach($tmp as $t){
673 $ret[$t['ID']]=$t['name'];
674 }
675 return($ret);
676 }else{
677 echo "not connected";
678 return(false);
679 }
680 }
682 /* Devices
683 Adds a new single device to our db
684 */
685 function addDevice($attr)
686 {
687 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
688 if($this->is_connected){
689 $arr = array_flip($deviceMappingGOsaGlpi);
691 $device_type = $attr['device_type'];
692 unset($attr['device_type']);
694 $tbl_name = $arr[$device_type];
695 $v = "";
696 $a = "";
697 foreach($attr as $name => $value){
698 $a .= $name.", ";
699 $v .= "'".$value."', ";
700 }
701 if(empty($v)){
702 echo "addDevice: no attributes given ";
703 return(false);
704 }else{
705 $a = preg_replace("/, $/","",$a);
706 $v = preg_replace("/, $/","",$v);
707 return($this->query("INSERT INTO ".$tbl_name." (".$a.") VALUES (".$v.");"));
708 }
710 }else{
711 echo "not connected";
712 return(false);
713 }
714 }
716 /* Return all available devices
717 */
718 function getDevices()
719 {
720 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
721 if($this->is_connected){
722 $arr = $deviceMappingGOsaGlpi;
724 $res = array();
725 foreach($arr as $glpi => $gosa){
726 $qry = "SELECT * FROM ".$glpi.";";
727 $ret = $this->query($qry);
728 foreach($ret as $id => $entry){
729 $entry['device_type'] = $gosa;
731 if(isset($entry['designation'])){
732 $res[$entry['designation']."-".$gosa] = $entry;
733 }else{
734 $res[$entry['name']."-".$gosa] = $entry;
735 }
736 }
737 }
738 return($res);
739 }else{
740 echo "not connected";
741 return(false);
742 }
743 }
745 /* This function returns all used devices
746 */
747 function getUsedDevices($computerID)
748 {
749 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
750 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
752 if($this->is_connected){
753 $qry = "SELECT * FROM glpi_computer_device WHERE FK_computers=".$computerID.";";
754 $res = $this->query($qry);
756 $ret = array();
757 foreach($deviceMappingGOsaGlpi as $GOsa => $glpi){
758 $ret[$GOsa] = array();
759 }
761 $tbls = array_flip($deviceMappingTableNameID);
763 foreach($res as $device){
764 $devtype = $tbls[$device['device_type']];
765 $tbl_name = $deviceMappingGOsaGlpi[$devtype];
766 $qry = ("SELECT * FROM ".$tbl_name." WHERE ID=".$device['FK_device'].";");
767 $res2 = $this->query($qry);
768 if(count($res2)!=0){
769 $ret[$devtype][$res2[0]['designation']]=$res2[0];
770 }
772 $qry = "SELECT * FROM glpi_connect_wire WHERE type=4 AND end2=".$computerID.";";
773 $res2 = $this->query($qry);
774 foreach($res2 as $monitor){
775 $qry = "SELECT * FROM glpi_monitors WHERE ID=".$monitor['end1'].";";
776 $res3 = $this->query($qry);
777 foreach($res3 as $moni){
778 $ret['monitor'][$moni['name']]=$moni;
779 }
780 }
784 }
785 return($ret);
786 }else{
787 echo "not connected";
788 return(false);
789 }
790 }
792 /* This function removes all given devices from a computer, specified by $id
793 In the next step all devices specified by devices will be added.
794 */
795 function addDevicesToComputer($devices, $id)
796 {
797 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
798 $deviceMappingTableNameID = $this->deviceMappingTableNameID;
800 if(($id == "" )||(!is_numeric($id))){
801 return (false);
802 }
803 if($this->is_connected){
804 $qry = "DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";";
805 $this->query($qry);
807 foreach($devices as $type => $entries){
808 foreach($entries as $entry){
809 if($type=="monitor"){
810 $str = "INSERT INTO glpi_connect_wire (end1,end2,type)
811 VALUES (".$entry['ID'].",".$id.",4);";
812 }else{
813 $str = "INSERT INTO glpi_computer_device (device_type,FK_device,FK_computers)
814 VALUES (".$deviceMappingTableNameID[$type].",".$entry['ID'].",".$id.");";
815 }
816 $this->query($str);
817 }
818 }
821 }else{
822 echo "not connected";
823 return(false);
824 }
826 }
828 function removeComputerInformations($name)
829 {
830 if($this->is_connected){
831 $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
832 if(isset($tmp[0])){
833 $id = $tmp[0]['ID'];
834 $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
835 $this->query("DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";");
836 return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
837 }else{
838 echo "can't remove not existing entry";
839 return(false);
840 }
841 }else{
842 echo "not connected";
843 return(false);
844 }
845 }
847 function is_connected()
848 {
849 return($this->is_connected);
850 }
852 function addAttachmentsToComputer($attr,$id)
853 {
854 if(($id == "" )||(!is_numeric($id))){
855 return (false);
856 }
857 if($this->is_connected){
858 $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=1);";
859 $this->query($qry);
861 foreach($attr as $aid => $entry){
862 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template)
863 VALUES
864 ($aid,$id,1,'0');";
865 $this->query($str);
866 }
867 }else{
868 echo "not connected";
869 return(false);
870 }
871 }
873 function getAssignAttachments($id)
874 {
876 if($this->is_connected){
877 $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=1) AND (FK_device=".$id.");";
878 $ret = $this->query($qry);
879 return($ret);
880 }else{
881 echo "not connected";
882 return(false);
883 }
884 }
886 function deleteAttachment($id)
887 {
888 if($this->is_connected){
889 $qry = "DELETE FROM glpi_docs WHERE ID=".$id."";
890 $this->query($qry);
891 }else{
892 echo "not connected";
893 return(false);
894 }
895 }
897 function getAttachments()
898 {
899 if($this->is_connected){
900 $qry = "SELECT * FROM glpi_docs WHERE name!='';";
901 $ret = $this->query($qry);
902 return($ret);
903 }else{
904 echo "not connected";
905 return(false);
906 }
907 }
909 function saveAttachments($attrs,$id = -1)
910 {
911 if($this->is_connected){
912 $atr = array("name","filename","rubrique","mime","date_mod","comment","deleted","link");
913 $tmp = array();
914 foreach($atr as $at){
915 if(isset($attrs[$at])){
916 $tmp[$at] = $attrs[$at];
917 }
918 }
919 if(count($tmp)==0){
920 return(false);
921 }else{
923 // Add
924 if($id == -1){
925 $str = "INSERT INTO glpi_docs ";
926 $namen = "";
927 $values= "";
928 foreach($tmp as $name => $value){
929 $namen .= $name.", ";
930 if(is_numeric($value)){
931 $values .= $value.", ";
932 }else{
933 $values .= "'".$value."', ";
934 }
935 }
936 $values = preg_replace("/, $/","",$values);
937 $namen = preg_replace("/, $/","",$namen);
938 $str .= "(".$namen.") VALUES (".$values.");";
939 print $str;
940 }else{
941 $str = "UPDATE glpi_docs SET ";
942 foreach($tmp as $name => $value){
943 $str .= $name."= ";
944 if(is_numeric($value)){
945 $str .= $value.", ";
946 }else{
947 $str .= "'".$value."', ";
948 }
949 }
950 $str = preg_replace("/, $/","",$str);
951 $str .= " WHERE ID=".$id.";";
952 }
953 $this->query($str);
954 }
955 }else{
956 echo "not connected";
957 return(false);
958 }
959 }
962 /* Monitor handling
963 */
964 function getMonitors()
965 {
966 if($this->is_connected){
967 $qry= "SELECT * FROM glpi_monitors;";
968 return($this->query($qry));
970 }else{
971 echo "not connected";
972 return(false);
973 }
974 }
976 function updatedMonitor()
977 {
978 if($this->is_connected){
979 // $qry= "SELECT * FROM glpi_monitors;";
980 // return($this->query($qry));
982 }else{
983 echo "not connected";
984 return(false);
985 }
986 }
988 function addMonitor()
989 {
990 if($this->is_connected){
991 // $qry= "SELECT * FROM glpi_monitors;";
992 // return($this->query($qry));
994 }else{
995 echo "not connected";
996 return(false);
997 }
998 }
1000 function removeMonitor($id)
1001 {
1002 if($this->is_connected){
1003 $qry= "DELETE FROM glpi_monitors WHERE ID=".$id.";";
1004 $this->query($qry);
1005 }else{
1006 echo "not connected";
1007 return(false);
1008 }
1009 }
1011 function getMonitorTypes()
1012 {
1013 if($this->is_connected){
1014 $qry= "SELECT * FROM glpi_type_monitors;";
1015 return($this->query($qry));
1017 }else{
1018 echo "not connected";
1019 return(false);
1020 }
1021 }
1023 function getLocationTypes()
1024 {
1025 if($this->is_connected){
1026 $qry= "SELECT * FROM glpi_dropdown_locations;";
1027 return($this->query($qry));
1029 }else{
1030 echo "not connected";
1031 return(false);
1032 }
1033 }
1035 function getStateTypes()
1036 {
1037 if($this->is_connected){
1038 $qry= "SELECT * FROM glpi_dropdown_state;";
1039 return($this->query($qry));
1040 }else{
1041 echo "not connected";
1042 return(false);
1043 }
1044 }
1047 /* Printer functions
1048 */
1051 /* This functions checks if the selected computer/network
1052 device is already available in the db
1053 */
1054 function is_printer_account($dn)
1055 {
1056 if(!$this->is_connected){
1057 $this->lasterror ="Can't query anything, if we aren't connected.";
1058 return(false);
1059 }else{
1060 $qry = "SELECT * FROM glpi_printers WHERE name='".$dn."';";
1061 $res = $this->query($qry);
1062 if(count($res)==0){
1063 return(false);
1064 }else{
1065 return(true);
1066 }
1067 }
1068 }
1070 /* This function returns all available data
1071 from a specified dn
1072 */
1073 function getPrinterInformations($name)
1074 {
1075 if($this->is_connected){
1076 $ret = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1077 return($ret);
1078 }else{
1079 echo "not connected";
1080 return(false);
1081 }
1082 }
1084 /* Get Printer attachments
1085 */
1086 function getAssignPrinterAttachments($id)
1087 {
1089 if($this->is_connected){
1090 $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=3) AND (FK_device=".$id.");";
1091 $ret = $this->query($qry);
1092 return($ret);
1093 }else{
1094 echo "not connected";
1095 return(false);
1096 }
1097 }
1099 /* Printer types
1100 Returns all defined printer types
1101 */
1102 function getPrinterTypes()
1103 {
1104 if($this->is_connected){
1105 $ret = array();
1106 $tmp = ($this->query("SELECT * FROM glpi_type_printers;"));
1107 foreach($tmp as $t){
1108 $ret[$t['ID']]=$t['name'];
1109 }
1110 return($ret);
1111 }else{
1112 echo "not connected";
1113 return(false);
1114 }
1115 }
1117 /* Add pritner types
1118 Add one entry to the printer types
1119 */
1120 function addPrinterType($name)
1121 {
1122 if($this->is_connected){
1123 $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE name='".$name."';");
1124 if(isset($tmp[0])){
1125 //echo "such an entry already exists";
1126 return(false);
1127 }else{
1128 return($this->query("INSERT INTO glpi_type_printers (name) VALUES ('".$name."');"));
1129 }
1130 }else{
1131 echo "not connected";
1132 return(false);
1133 }
1134 }
1136 /* remove printer types
1137 Remove one entry from the printer types (specified by ID=$id)
1138 */
1139 function removePrinterType($id)
1140 {
1141 if($this->is_connected){
1142 $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1143 if(isset($tmp[0])){
1144 return($this->query("DELETE FROM glpi_type_printers WHERE ID=".$id.";"));
1145 }else{
1146 echo "can't remove not existing entry";
1147 return(false);
1148 }
1149 }else{
1150 echo "not connected";
1151 return(false);
1152 }
1153 }
1155 /* Update printer types
1156 Update a printer type
1157 */
1158 function updatePrinterType($name,$id)
1159 {
1161 if($this->is_connected){
1162 $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1163 if(isset($tmp[0])){
1164 return($this->query("UPDATE glpi_type_printers SET name='".$name."' WHERE ID=".$id.";"));
1165 }else{
1166 echo "can't update not existing entry";
1167 return(false);
1168 }
1169 }else{
1170 echo "not connected";
1171 return(false);
1172 }
1173 }
1176 /* This fucntions updates an already existing entry
1177 */
1178 function updatePrinterInformations($array,$name)
1179 {
1180 if(!is_array($array)){
1181 echo "updatePrinterInformations: first paraeter must be an array";
1182 }elseif($this->is_connected){
1183 $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1184 if(isset($tmp[0])){
1186 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
1187 "tech_num","comments","date_mod","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1188 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1189 $v = "";
1190 foreach($atr as $at){
1191 if(isset($array[$at])){
1192 $v .= " ".$at."='".$array[$at]."', ";
1193 }
1194 }
1195 if(empty($v)){
1196 echo "updateSystemInformations: no attributes given ";
1197 return(false);
1198 }else{
1199 $v = preg_replace("/, $/","",$v);
1200 return($this->query("UPDATE glpi_printers SET ".$v." WHERE name='".$name."';"));
1201 }
1202 }else{
1203 echo "can't update not existing entry";
1204 return(false);
1205 }
1206 }else{
1207 echo "not connected";
1208 return(false);
1209 }
1211 }
1213 /* This function adds a new inventory settings for printers
1214 */
1215 function addPrinterInformations($array)
1216 {
1217 if(!is_array($array)){
1218 echo "updateComputerInformations: first paraeter must be an array";
1219 }elseif($this->is_connected){
1220 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
1221 "tech_num","comments","date_mod","os","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1222 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1223 $v = "";
1224 $a = "";
1225 foreach($atr as $at){
1226 if(isset($array[$at])){
1227 $a .= $at.", ";
1228 $v .= "'".$array[$at]."', ";
1229 }
1230 }
1231 if(empty($v)){
1232 echo "updateComputerInformations: no attributes given ";
1233 return(false);
1234 }else{
1235 $a = preg_replace("/, $/","",$a);
1236 $v = preg_replace("/, $/","",$v);
1237 return($this->query("INSERT INTO glpi_printers (".$a.") VALUES (".$v.");"));
1238 }
1240 }else{
1241 echo "not connected";
1242 return(false);
1243 }
1244 }
1246 /* add atachment to given printer */
1247 function addAttachmentsToPrinter($attr,$id)
1248 {
1249 if(($id == "" )||(!is_numeric($id))){
1250 return (false);
1251 }
1252 if($this->is_connected){
1253 $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=3);";
1254 $this->query($qry);
1256 foreach($attr as $aid => $entry){
1257 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template)
1258 VALUES
1259 ($aid,$id,3,'0');";
1260 $this->query($str);
1261 }
1262 }else{
1263 echo "not connected";
1264 return(false);
1265 }
1266 }
1268 function removePrinterInformations($name)
1269 {
1270 if($this->is_connected){
1271 $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1272 if(isset($tmp[0])){
1273 $id = $tmp[0]['ID'];
1274 // $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
1275 $this->query("DELETE FROM glpi_doc_device WHERE FK_device=".$id." AND device_type=3;");
1276 return($this->query("DELETE FROM glpi_printers WHERE ID=".$id.";"));
1277 }else{
1278 echo "can't remove not existing entry";
1279 return(false);
1280 }
1281 }else{
1282 echo "not connected";
1283 return(false);
1284 }
1285 }
1287 }
1288 //$s = new glpiDB("vserver-01","glpi","tester","glpi");
1289 //print_r($s->query("SELECT * FROM glpi_computers"));
1290 //$s->getComputerInformations("1 OR (c.ID<10000)");
1291 ?>