Code

f3de29b24e3dd7ccec5dcfd49992710de322426b
[gosa.git] / include / class_glpi.inc
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                         }
276                 
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);
317                                 
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                         }
425                 
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                         }
546                 
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                 
582         /* Check if given device is used by some accounts
583      *  (helpfull to avoid removement of used devices)
584      */
585         function is_deviceUsed($item)
586         {
588                 $deviceMappingGOsaGlpi          = array_flip($this->deviceMappingGOsaGlpi);
589                 $deviceMappingTableNameID       = $this->deviceMappingTableNameID;
590                 if($this->is_connected){
591                         $tablename =  $deviceMappingGOsaGlpi[$item['device_type']];
592                         $type = $item['device_type'];
594                         if($type=="monitor"){
595                                 $str = "SELECT * FROM glpi_connect_wire WHERE end1=".$item['ID']." AND type=4;";
596                         }else{
597                                 $str = "SELECT * FROM glpi_computer_device WHERE device_type=".$deviceMappingTableNameID[$type]." AND FK_device=".$item['ID'].";";
598                         }
599                         return(count($this->query($str)));
600                 }else{
601                         echo "not connected";
602                         return(false);
603                 }
604                         
605         }
608         /* This functions deletes a specified entry 
609      * from our device tables 
610      */
611         function deleteDevice($attr)
612         {
613                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
614                 if($this->is_connected){
615                         $arr = array_flip($deviceMappingGOsaGlpi);
616                         
617                         $device_type = $attr['device_type'];
618                         unset($attr['device_type']);                    
620                         $tbl_name = $arr[$device_type];
622                         $this->query("DELETE FROM ".$tbl_name." WHERE ID=".$attr['ID'].";");    
623                 }else{
624                         echo "not connected";
625                         return(false);
626                 }
627         }
629         /* This funtions updated an already existing device
630      */
631         function updateDevices($attr)
632         {
633                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
634                 if($this->is_connected){
635                         $arr = array_flip($deviceMappingGOsaGlpi);
636                         
637                         $device_type = $attr['device_type'];
638                         unset($attr['device_type']);                    
640                         $tbl_name = $arr[$device_type];
642                         $str = "UPDATE ".$tbl_name." SET ";
643                         foreach($attr as $name => $value){
644                                 $str.=$name."='".$value."', ";
645                         }
646                         $str = preg_replace("/, $/","",$str);
647                         $str .= " WHERE ID=".$attr['ID'].";";
648                         $this->query($str);     
649                 }else{
650                         echo "not connected";
651                         return(false);
652                 }
653         }
655         /* Returns all possible RAM types 
656      * like SDRAM , DIMM .....
657      */
658         function getRAMTypes()
659         {
660                 if($this->is_connected){
661                         $ret = array();
662                         $tmp = ($this->query("SELECT * FROM glpi_dropdown_ram_type;"));
663                         foreach($tmp as $t){
664                                 $ret[$t['ID']]=$t['name'];
665                         }
666                         return($ret);
667                 }else{
668                         echo "not connected";
669                         return(false);
670                 }
671         }
672         
673         /* Returns all possible HDD connection types 
674      * like IDE SCSI ...
675      */
676         function getGlpiDeviceControlTypes()
677         {
678                 if($this->is_connected){
679                         $ret = array();
680                         $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_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         }
690         
691         /* Returns all possible gfx card connection types
692      * like PCI-X PCI AGP ....
693          */
694         function getGlpiGfxControlTypes()
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         }
708         
709         /* Devices 
710        Adds a new single device to our db
711     */  
712         function addDevice($attr)
713         {
714                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
715                 if($this->is_connected){        
716                         $arr = array_flip($deviceMappingGOsaGlpi);
717                         
718                         $device_type = $attr['device_type'];
719                         unset($attr['device_type']);                    
721                         $tbl_name = $arr[$device_type];
722             $v = "";
723             $a = "";
724             foreach($attr as $name => $value){
725                 $a .= $name.", ";
726                 $v .= "'".$value."', ";
727             }
728             if(empty($v)){
729                 echo "addDevice: no attributes given ";
730                 return(false);
731             }else{
732                 $a = preg_replace("/, $/","",$a);
733                 $v = preg_replace("/, $/","",$v);
734                 return($this->query("INSERT INTO ".$tbl_name." (".$a.") VALUES (".$v.");"));
735             }
737         }else{
738             echo "not connected";
739             return(false);
740         }
741         }
743         /* Return all available devices 
744      */
745         function getDevices()
746         {
747                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
748                 if($this->is_connected){
749                         $arr = $deviceMappingGOsaGlpi; 
750                 
751                         $res = array();
752                         foreach($arr as $glpi => $gosa){
753                                 $qry = "SELECT * FROM ".$glpi.";";
754                                 $ret = $this->query($qry);
755                                 foreach($ret as $id => $entry){
756                                         $entry['device_type'] = $gosa;
757         
758                                         if(isset($entry['designation'])){
759                                                 $res[$entry['designation']."-".$gosa] = $entry;
760                                         }else{
761                                                 $res[$entry['name']."-".$gosa] = $entry;
762                                         }
763                                 }
764                         }
765                         return($res);
766                 }else{
767                         echo "not connected";
768                         return(false);
769                 }
770         }
772         /* This function returns all used devices 
773      */
774         function getUsedDevices($computerID)
775         {
776                 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
777                 $deviceMappingTableNameID       = $this->deviceMappingTableNameID;
778         
779                 if($this->is_connected){
780                         $qry = "SELECT * FROM glpi_computer_device WHERE FK_computers=".$computerID.";";
781                         $res = $this->query($qry);
782                 
783                         $ret = array();
784                         foreach($deviceMappingGOsaGlpi as $GOsa => $glpi){
785                                 $ret[$GOsa] = array();
786                         }
788                         $tbls = array_flip($deviceMappingTableNameID);
790                         foreach($res as $device){
791                                 $devtype = $tbls[$device['device_type']];
792                                 $tbl_name = $deviceMappingGOsaGlpi[$devtype];
793                                 $qry = ("SELECT * FROM ".$tbl_name." WHERE ID=".$device['FK_device'].";");
794                                 $res2 = $this->query($qry);
795                                 if(count($res2)!=0){
796                                         $ret[$devtype][$res2[0]['designation']]=$res2[0];
797                                 }
799                         $qry = "SELECT * FROM glpi_connect_wire WHERE type=4 AND end2=".$computerID.";";
800                         $res2 = $this->query($qry);
801                         foreach($res2 as $monitor){
802                                 $qry = "SELECT * FROM glpi_monitors WHERE ID=".$monitor['end1'].";";
803                                 $res3 = $this->query($qry);
804                                 foreach($res3 as $moni){
805                                         $ret['monitor'][$moni['name']]=$moni;
806                                 }
807                         }
811                         }
812                         return($ret);
813                 }else{
814                         echo "not connected";
815                         return(false);
816                 }
817         }
819         /* This function removes all given devices from a computer, specified by $id
820        In the next step all devices specified by devices will be added.
821          */
822         function addDevicesToComputer($devices, $id)
823         {
824                 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
825                 $deviceMappingTableNameID       = $this->deviceMappingTableNameID;
826         
827                 if(($id == "" )||(!is_numeric($id))){
828                         return (false); 
829                 }
830                 if($this->is_connected){
831                         $qry = "DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";";
832                         $this->query($qry);     
833                 
834                         foreach($devices as $type => $entries){
835                                 foreach($entries as $entry){
836                                         if($type=="monitor"){
837                                                 $str = "INSERT INTO glpi_connect_wire (end1,end2,type) 
838                                                 VALUES (".$entry['ID'].",".$id.",4);";
839                                         }else{
840                                                 $str = "INSERT INTO glpi_computer_device (device_type,FK_device,FK_computers) 
841                                                 VALUES (".$deviceMappingTableNameID[$type].",".$entry['ID'].",".$id.");";
842                                         }
843                                         $this->query($str);
844                                 }
845                         }
846                 
848                 }else{
849                         echo "not connected";
850                         return(false);
851                 }
853         }
855         function removeComputerInformations($name)
856         {
857                 if($this->is_connected){
858                         $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
859                         if(isset($tmp[0])){
860                                 $id = $tmp[0]['ID'];
861                                 $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
862                                 $this->query("DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";");
863                                 return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
864                         }else{
865                                 echo "can't remove not existing entry";
866                                 return(false);
867                         }
868                 }else{
869                         echo "not connected";
870                         return(false);
871                 }
872         }
874         function is_connected()
875         {
876                 return($this->is_connected);
877         }
879         function addAttachmentsToComputer($attr,$id)
880         {
881         if(($id == "" )||(!is_numeric($id))){
882             return (false);
883         }
884         if($this->is_connected){
885             $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=1);";
886             $this->query($qry);
887                         
888                         foreach($attr as $aid => $entry){
889                                 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template) 
890                                                                                                 VALUES
891                                                                                                         ($aid,$id,1,'0');";
892                                 $this->query($str);
893                         }
894         }else{
895             echo "not connected";
896             return(false);
897         }
898         }
900         function getAssignAttachments($id)
901         {
903                 if($this->is_connected){
904                         $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=1) AND (FK_device=".$id.");";
905                         $ret = $this->query($qry);
906                         return($ret);
907                 }else{
908             echo "not connected";
909             return(false);
910         }
911         }
913         function deleteAttachment($id)
914         {
915                 if($this->is_connected){
916                         $qry = "DELETE FROM glpi_docs WHERE ID=".$id."";
917                         $this->query($qry);
918                 }else{
919             echo "not connected";
920             return(false);
921         }
922         }
923         
924         function getAttachments()
925         {
926                 $ret = array();
927                 if($this->is_connected){
928                         $qry = "SELECT * FROM glpi_docs WHERE name!='';";
929                         $re = $this->query($qry);
931                         foreach($re as $entry){
932                                 $ret[$entry['ID']]=$entry;
933                         }
935                         return($ret);
936                 }else{
937             echo "not connected";
938             return(false);
939         }
940         }
941         
942         function saveAttachments($attrs,$id = -1)
943         {
944                 if($this->is_connected){
945                         $atr = array("name","filename","rubrique","mime","date_mod","comment","deleted","link");
946                         $tmp = array();
947                         foreach($atr as $at){
948                                 if(isset($attrs[$at])){
949                                         $tmp[$at] = $attrs[$at];
950                                 }
951                         }
952                         if(count($tmp)==0){
953                                 return(false);
954                         }else{
956                                 // Add
957                                 if($id == -1){
958                                         $str = "INSERT INTO glpi_docs ";
959                                         $namen = "";
960                                         $values= "";
961                                         foreach($tmp as $name => $value){       
962                                                 $namen .= $name.", ";
963                                                 if(is_numeric($value)){
964                                                         $values .= $value.", ";
965                                                 }else{
966                                                         $values .= "'".$value."', ";
967                                                 }
968                                         }
969                                         $values = preg_replace("/, $/","",$values);
970                                         $namen  = preg_replace("/, $/","",$namen);
971                                         $str .= "(".$namen.") VALUES (".$values.");";
972                                 }else{
973                                         $str = "UPDATE glpi_docs SET ";
974                                         foreach($tmp as $name => $value){       
975                                                 $str .= $name."= ";
976                                                 if(is_numeric($value)){
977                                                         $str .= $value.", ";
978                                                 }else{
979                                                         $str .= "'".$value."', ";
980                                                 }
981                                         }
982                                         $str = preg_replace("/, $/","",$str);
983                                         $str .= " WHERE ID=".$id.";";
984                                 }
985                                 $this->query($str);
986                         }
987                 }else{
988                         echo "not connected";
989                         return(false);
990                 }
991         }
993         
994         /* Check if given attachment id is used in any Device 
995                 ( - avoid removing of used attachments)
996      */
997         function is_attachmentUsed($id)
998         {
999                 if($this->is_connected){
1000                         $qry = "SELECT * FROM glpi_doc_device WHERE FK_doc =".$id." LIMIT 1; ";
1001                         return(count($this->query($qry)));
1002                 }else{
1003             echo "not connected";
1004             return(false);
1005         }
1006         }
1008                 
1009         /* Monitor handling 
1010      */
1011     function getMonitors()
1012         {
1013                 if($this->is_connected){
1014                         $qry= "SELECT * FROM glpi_monitors;";
1015                         return($this->query($qry));     
1016                 
1017                 }else{
1018             echo "not connected";
1019             return(false);
1020         }
1021         }
1023     function updatedMonitor()
1024         {
1025                 if($this->is_connected){
1026 //                      $qry= "SELECT * FROM glpi_monitors;";
1027 //                      return($this->query($qry));     
1028                 
1029                 }else{
1030             echo "not connected";
1031             return(false);
1032         }
1033         }
1035     function addMonitor()
1036         {
1037                 if($this->is_connected){
1038 //                      $qry= "SELECT * FROM glpi_monitors;";
1039 //                      return($this->query($qry));     
1040                 
1041                 }else{
1042             echo "not connected";
1043             return(false);
1044         }
1045         }
1047     function removeMonitor($id)
1048         {
1049                 if($this->is_connected){
1050                         $qry= "DELETE FROM glpi_monitors WHERE ID=".$id.";";
1051                         $this->query($qry);     
1052                 }else{
1053             echo "not connected";
1054             return(false);
1055         }
1056         }
1058     function getMonitorTypes()
1059         {
1060                 if($this->is_connected){
1061                         $qry= "SELECT * FROM glpi_type_monitors;";
1062                         return($this->query($qry));     
1063                 
1064                 }else{
1065             echo "not connected";
1066             return(false);
1067         }
1068         }
1070     function getLocationTypes()
1071         {
1072                 if($this->is_connected){
1073                         $qry= "SELECT * FROM glpi_dropdown_locations;";
1074                         return($this->query($qry));     
1075                 
1076                 }else{
1077             echo "not connected";
1078             return(false);
1079         }
1080         }
1082     function getStateTypes()
1083         {
1084                 if($this->is_connected){
1085                         $qry= "SELECT * FROM glpi_dropdown_state;";
1086                         return($this->query($qry));     
1087                 }else{
1088             echo "not connected";
1089             return(false);
1090         }
1091         }
1093         
1094         /* Printer functions
1095      */
1098         /* This functions checks if the selected computer/network 
1099            device is already available in the db
1100          */
1101         function is_printer_account($dn)
1102         {
1103                 if(!$this->is_connected){
1104                         $this->lasterror ="Can't query anything, if we aren't connected.";
1105                         return(false);
1106                 }else{
1107                         $qry = "SELECT * FROM glpi_printers WHERE name='".$dn."';";
1108                         $res = $this->query($qry);
1109                         if(count($res)==0){
1110                                 return(false);
1111                         }else{
1112                                 return(true);
1113                         }
1114                 }
1115         }
1117         /* This function returns all available data 
1118        from a specified dn
1119      */
1120         function getPrinterInformations($name)
1121         {
1122                 if($this->is_connected){        
1123                         $ret = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1124                         return($ret);           
1125                 }else{
1126                         echo "not connected";
1127                         return(false);
1128                 }
1129         }
1131         /* Get Printer attachments
1132      */
1133         function getAssignPrinterAttachments($id)
1134         {
1136                 if($this->is_connected){
1137                         $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=3) AND (FK_device=".$id.");";
1138                         $ret = $this->query($qry);
1139                         return($ret);
1140                 }else{
1141             echo "not connected";
1142             return(false);
1143         }
1144         }
1146         /* Printer types 
1147        Returns all defined printer types 
1148          */
1149         function getPrinterTypes()
1150         {
1151                 if($this->is_connected){
1152                         $ret = array();
1153                         $tmp = ($this->query("SELECT * FROM glpi_type_printers;"));
1154                         foreach($tmp as $t){
1155                                 $ret[$t['ID']]=$t['name'];
1156                         }
1157                         return($ret);
1158                 }else{
1159                         echo "not connected";
1160                         return(false);
1161                 }
1162         }
1164         /* Add pritner types 
1165        Add one entry to the printer types 
1166      */
1167         function addPrinterType($name)
1168         {
1169                 if($this->is_connected){
1170                         $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE name='".$name."';");
1171                         if(isset($tmp[0])){
1172                                 //echo "such an entry already exists";
1173                                 return(false);
1174                         }else{  
1175                                 return($this->query("INSERT INTO glpi_type_printers (name) VALUES ('".$name."');"));
1176                         }
1177                 }else{
1178                         echo "not connected";
1179                         return(false);
1180                 }
1181         }
1183         /* remove printer types 
1184        Remove one entry from the printer types (specified by ID=$id)
1185      */
1186         function removePrinterType($id)
1187         {
1188                 if($this->is_connected){
1189                         $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1190                         if(isset($tmp[0])){
1191                                 return($this->query("DELETE FROM glpi_type_printers WHERE ID=".$id.";"));       
1192                         }else{
1193                                 echo "can't remove not existing entry";
1194                                 return(false);  
1195                         }
1196                 }else{
1197                         echo "not connected";
1198                         return(false);
1199                 }
1200         }
1202         /* Update printer types 
1203            Update a printer type        
1204         */
1205         function updatePrinterType($name,$id)
1206         {
1208                 if($this->is_connected){
1209                         $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1210                         if(isset($tmp[0])){
1211                                 return($this->query("UPDATE glpi_type_printers SET name='".$name."' WHERE ID=".$id.";"));       
1212                         }else{
1213                                 echo "can't update not existing entry";
1214                                 return(false);  
1215                         }
1216                 }else{
1217                         echo "not connected";
1218                         return(false);
1219                 }
1220         }
1223         /*  This fucntions updates an already existing entry 
1224      */
1225         function updatePrinterInformations($array,$name)
1226         {
1227                 if(!is_array($array)){
1228                         echo "updatePrinterInformations: first paraeter must be an array";
1229                 }elseif($this->is_connected){
1230                         $tmp = $this->query("SELECT * FROM  glpi_printers WHERE name='".$name."';");
1231                         if(isset($tmp[0])){
1233                                 $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
1234                                                 "tech_num","comments","date_mod","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1235                                                 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1236                                 $v = "";
1237                                 foreach($atr as $at){
1238                                         if(isset($array[$at])){
1239                                                 $v .= " ".$at."='".$array[$at]."', ";
1240                                         }
1241                                 }
1242                                 if(empty($v)){
1243                                         echo "updateSystemInformations: no attributes given ";
1244                                         return(false);
1245                                 }else{
1246                                         $v = preg_replace("/, $/","",$v);
1247                                         return($this->query("UPDATE glpi_printers SET ".$v." WHERE name='".$name."';"));
1248                                 }
1249                         }else{
1250                                 echo "can't update not existing entry";
1251                                 return(false);
1252                         }
1253                 }else{
1254                         echo "not connected";
1255                         return(false);
1256                 }
1258         }
1260         /* This function adds a new inventory settings for printers
1261      */
1262         function addPrinterInformations($array)
1263         {
1264                 if(!is_array($array)){
1265                         echo "updateComputerInformations: first paraeter must be an array";
1266                 }elseif($this->is_connected){
1267                         $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
1268                                         "tech_num","comments","date_mod","os","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1269                                         "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1270                         $v = "";
1271                         $a = "";
1272                         foreach($atr as $at){
1273                                 if(isset($array[$at])){
1274                                         $a .= $at.", ";
1275                                         $v .= "'".$array[$at]."', ";
1276                                 }
1277                         }
1278                         if(empty($v)){
1279                                 echo "updateComputerInformations: no attributes given ";
1280                                 return(false);
1281                         }else{
1282                                 $a = preg_replace("/, $/","",$a);
1283                                 $v = preg_replace("/, $/","",$v);
1284                                 return($this->query("INSERT INTO glpi_printers (".$a.") VALUES (".$v.");"));
1285                         }
1286                 
1287                 }else{
1288                         echo "not connected";
1289                         return(false);
1290                 }
1291         }
1293         /* add atachment to given printer */
1294         function addAttachmentsToPrinter($attr,$id)
1295         {
1296         if(($id == "" )||(!is_numeric($id))){
1297             return (false);
1298         }
1299         if($this->is_connected){
1300             $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=3);";
1301             $this->query($qry);
1302                         
1303                         foreach($attr as $aid => $entry){
1304                                 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template) 
1305                                                                                                 VALUES
1306                                                                                                         ($aid,$id,3,'0');";
1307                                 $this->query($str);
1308                         }
1309         }else{
1310             echo "not connected";
1311             return(false);
1312         }
1313         }
1315         function removePrinterInformations($name)
1316         {
1317                 if($this->is_connected){
1318                         $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1319                         if(isset($tmp[0])){
1320                                 $id = $tmp[0]['ID'];
1321 //                              $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
1322                                 $this->query("DELETE FROM glpi_doc_device WHERE FK_device=".$id." AND device_type=3;");
1323                                 return($this->query("DELETE FROM glpi_printers WHERE ID=".$id.";"));
1324                         }else{
1325                                 echo "can't remove not existing entry";
1326                                 return(false);
1327                         }
1328                 }else{
1329                         echo "not connected";
1330                         return(false);
1331                 }
1332         }
1335 //$s = new glpiDB("vserver-01","glpi","tester","glpi");
1336 //print_r($s->query("SELECT * FROM glpi_computers"));
1337 //$s->getComputerInformations("1 OR (c.ID<10000)");
1338 ?>