1 <?php
2 /* This class is used for glpi account management.
3 ADD // EDIT // DELETE of computers or changng
4 something else in the glpi database should be done here.
7 function glpiDB($server,$user,$pwd,$db){
8 function SelectDB()
9 function query($qry)
11 // return all available Sytemtypes
12 function getSystemTypes()
14 // Update system type specifid by id
15 function updateSystemType($name,$id)
17 // Add system type
18 function addSystemType($name)
20 // Delete system type by name / id
21 function removeSystemType_byID($id)
22 function removeSystemType_byNAME($name)
24 // Get all manufacturer
25 function getEnterprisesTypes()
27 // Update with specified attributes, on entry $id
28 function updateEnterprisesType($array,$id)
30 // remove entry with id=$id
31 function removeEnterprisesType($id)
33 // return all os types
34 function getOSTypes()
36 // return all users
37 function getUsers()
39 // Return available devices
40 function getDevices()
42 // return computer informations (phone, terminal,ws ...)
43 function getComputerInformations_byID($id)
44 function getComputerInformations_byNAME($name)
47 */
48 class glpiDB{
50 var $user ="";
51 var $password ="";
52 var $server ="";
53 var $db ="";
55 var $is_connected = 0;
56 var $handle = NULL;
58 var $lasterror ="";
60 function glpiDB($server,$user,$pwd,$db){
61 $this->server = $server;
62 $this->user = $user;
63 $this->password = $pwd;
64 $this->db = $db;
66 $this->handle = @mysql_connect($this->server,$this->user,$this->password);
68 if($this->handle){
69 $this->is_connected = true;
70 $this->SelectDB($this->db);
71 }
72 }
74 function SelectDB()
75 {
76 if($this->is_connected){
77 mysql_select_db($this->db,$this->handle);
78 }
79 }
81 function is_account($dn)
82 {
83 if(!$this->is_connected){
84 $this->lasterror ="Can't query anything, if we aren't connected.";
85 return(false);
86 }else{
87 $qry = "SELECT * FROM glpi_computers WHERE name='".$dn."';";
88 $res = $this->query($qry);
89 if(count($res)==0){
90 return(false);
91 }else{
92 return(true);
93 }
94 }
95 }
97 function query($qry)
98 {
99 if(!$this->is_connected){
100 $this->lasterror ="Can't query anything, if we aren't connected.";
101 return(false);
102 }else{
103 $ret =array();
104 $res = mysql_query($qry,$this->handle);
106 while($rs = @mysql_fetch_array($res,MYSQL_ASSOC)){
107 $ret[]=$rs;
108 }
109 return($ret);
110 }
111 }
113 /* System types */
114 function getSystemTypes()
115 {
116 if($this->is_connected){
117 $ret = array();
118 $tmp = ($this->query("SELECT * FROM glpi_type_computers;"));
119 foreach($tmp as $t){
120 $ret[$t['ID']]=$t['name'];
121 }
122 return($ret);
123 }else{
124 echo "not connected";
125 return(false);
126 }
127 }
129 /* System types */
130 function updateSystemType($name,$id)
131 {
133 if($this->is_connected){
134 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
135 if(isset($tmp[0])){
136 return($this->query("UPDATE glpi_type_computers SET name='".$name."' WHERE ID=".$id.";"));
137 }else{
138 echo "can't update not existing entry";
139 return(false);
140 }
141 }else{
142 echo "not connected";
143 return(false);
144 }
145 }
147 /* System types */
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 function removeSystemType_byID($id)
166 {
167 if($this->is_connected){
168 $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
169 if(isset($tmp[0])){
170 return($this->query("DELETE FROM glpi_type_computers WHERE ID=".$id.";"));
171 }else{
172 echo "can't remove not existing entry";
173 return(false);
174 }
175 }else{
176 echo "not connected";
177 return(false);
178 }
179 }
181 /* Manufacturer */
182 function getEnterprisesTypes()
183 {
184 if($this->is_connected){
185 $ret = array();
186 $tmp = $this->query("SELECT * FROM glpi_enterprises;");
187 foreach($tmp as $t){
188 $ret[$t['ID']]=$t['name'];
189 }
190 return($ret);
191 }else{
192 echo "not connected";
193 return(false);
194 }
195 }
197 /* Manufacturer */
198 function getEnterprise($id)
199 {
200 if($this->is_connected){
201 $ret = array();
202 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
203 return($tmp);
204 }else{
205 echo "not connected";
206 return(false);
207 }
208 }
210 /* System types */
211 function updateEnterprisesType($array,$id)
212 {
213 if(!is_array($array)){
214 echo "updateEnterprisesType: first paraeter must be an array";
215 }elseif($this->is_connected){
216 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID='".$id."';");
217 if(isset($tmp[0])){
218 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
220 $v = "";
221 foreach($atr as $at){
222 if(isset($array[$at])){
223 $v .= " ".$at."='".$array[$at]."', ";
224 }
225 }
226 if(empty($v)){
227 echo "updateEnterprisesType: no attributes given ";
228 return(false);
229 }else{
230 $v = preg_replace("/, $/","",$v);
231 return($this->query("UPDATE glpi_enterprises SET ".$v." WHERE ID='".$id."';"));
232 }
233 }else{
234 echo "can't update not existing entry";
235 return(false);
236 }
237 }else{
238 echo "not connected";
239 return(false);
240 }
241 }
243 function addEnterprisesType($array)
244 {
245 if(!is_array($array)){
246 echo "addUser: first paraeter must be an array";
247 }elseif($this->is_connected){
248 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
249 $v = "";
250 $a = "";
251 foreach($atr as $at){
252 if(isset($array[$at])){
253 $a .= $at.", ";
254 $v .= "'".$array[$at]."', ";
255 }
256 }
257 if(empty($v)){
258 echo "addUser: no attributes given ";
259 return(false);
260 }else{
261 $a = preg_replace("/, $/","",$a);
262 $v = preg_replace("/, $/","",$v);
263 return($this->query("INSERT INTO glpi_enterprises (".$a.") VALUES (".$v.");"));
264 }
266 }else{
267 echo "not connected";
268 return(false);
269 }
271 }
275 function removeEnterprisesType($id)
276 {
277 if($this->is_connected){
278 $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
279 if(isset($tmp[0])){
280 return($this->query("DELETE FROM glpi_enterprises WHERE ID=".$id.";"));
281 }else{
282 echo "can't remove not existing entry";
283 return(false);
284 }
285 }else{
286 echo "not connected";
287 return(false);
288 }
289 }
292 /* Operating systems */
293 function getOSTypes()
294 {
295 if($this->is_connected){
296 $ret = array();
297 $tmp=($this->query("SELECT * FROM glpi_dropdown_os;"));
299 foreach($tmp as $t){
300 $ret[$t['ID']]=$t['name'];
301 }
303 return($ret);
305 }else{
306 echo "not connected";
307 return(false);
308 }
309 }
311 /* os */
312 function addOS($name)
313 {
314 if($this->is_connected){
315 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE name='".$name."';");
316 if(isset($tmp[0])){
317 echo "such an entry already exists";
318 return(false);
319 }else{
320 return($this->query("INSERT INTO glpi_dropdown_os (name) VALUES ('".$name."');"));
321 }
322 }else{
323 echo "not connected";
324 return(false);
325 }
326 }
328 /* System types */
329 function removeOS_byID($id)
330 {
331 if($this->is_connected){
332 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
333 if(is_array($tmp[0])){
334 return($this->query("DELETE FROM glpi_dropdown_os WHERE ID=".$id.";"));
335 }else{
336 echo "can't remove not existing entry";
337 return(false);
338 }
339 }else{
340 echo "not connected";
341 return(false);
342 }
343 }
347 /* System types */
348 function updateOS($name,$id)
349 {
351 if($this->is_connected){
352 $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
353 if(isset($tmp[0])){
354 return($this->query("UPDATE glpi_dropdown_os SET name='".$name."' WHERE ID=".$id.";"));
355 }else{
356 echo "can't update not existing entry";
357 return(false);
358 }
359 }else{
360 echo "not connected";
361 return(false);
362 }
363 }
366 /* Technical / Responsible person / glpi users */
367 function getUsers()
368 {
369 if($this->is_connected){
370 $ret = array();
371 $tmp = ($this->query("SELECT * FROM glpi_users"));
372 foreach($tmp as $user){
373 $ret[$user['ID']]=$user['name'];
374 }
375 return($ret);
377 }else{
378 echo "not connected";
379 return(false);
380 }
381 }
384 function addUser($array,$dn)
385 {
386 if(!is_array($array)){
387 echo "addUser: first paraeter must be an array";
388 }elseif($this->is_connected){
389 $array['name']=$dn;
390 $atr = array("name","phone","email");
391 $v = "";
392 $a = "";
393 foreach($atr as $at){
394 if(isset($array[$at])){
395 $a .= $at.", ";
396 $v .= "'".$array[$at]."', ";
397 }
398 }
399 if(empty($v)){
400 echo "addUser: no attributes given ";
401 return(false);
402 }else{
403 $a = preg_replace("/, $/","",$a);
404 $v = preg_replace("/, $/","",$v);
405 return($this->query("INSERT INTO glpi_users (".$a.") VALUES (".$v.");"));
406 }
408 }else{
409 echo "not connected";
410 return(false);
411 }
413 }
415 function updateUser($array,$dn)
416 {
417 if(!is_array($array)){
418 echo "updateUser: first paraeter must be an array";
419 }elseif($this->is_connected){
420 $tmp = $this->query("SELECT * FROM glpi_users WHERE name='".$dn."';");
421 if(isset($tmp[0])){
423 $atr = array("name","phone","email");
424 $v = "";
425 foreach($atr as $at){
426 if(isset($array[$at])){
427 $v .= " ".$at."='".$array[$at]."', ";
428 }
429 }
430 if(empty($v)){
431 echo "UpdateUser: no attributes given ";
432 return(false);
433 }else{
434 $v = preg_replace("/, $/","",$v);
435 return($this->query("UPDATE glpi_users SET ".$v." WHERE name='".$dn."';"));
436 }
437 }else{
438 echo "can't update not existing entry";
439 return(false);
440 }
441 }else{
442 echo "not connected";
443 return(false);
444 }
446 }
450 /* Gets all pulldown and needed tableinformations */
451 function getDevices()
452 {
453 if($this->is_connected){
454 $ret = array();
455 $ret['devices']['glpi_device_moboard'] = $this->query("SELECT * FROM glpi_device_moboard;");
456 $ret['devices']['glpi_device_case'] = $this->query("SELECT * FROM glpi_device_case;");
457 $ret['devices']['glpi_device_control'] = $this->query("SELECT * FROM glpi_device_control;");
458 $ret['devices']['glpi_device_drive'] = $this->query("SELECT * FROM glpi_device_drive;");
459 $ret['devices']['glpi_device_gfxcard'] = $this->query("SELECT * FROM glpi_device_gfxcard;");
460 $ret['devices']['glpi_device_hdd'] = $this->query("SELECT * FROM glpi_device_hdd;");
461 $ret['devices']['glpi_device_iface'] = $this->query("SELECT * FROM glpi_device_iface;");
462 $ret['devices']['glpi_device_pci'] = $this->query("SELECT * FROM glpi_device_pci;");
463 $ret['devices']['glpi_device_power'] = $this->query("SELECT * FROM glpi_device_power;");
464 $ret['devices']['glpi_device_processor'] = $this->query("SELECT * FROM glpi_device_processor;");
465 $ret['devices']['glpi_device_ram'] = $this->query("SELECT * FROM glpi_device_ram;");
466 $ret['devices']['glpi_device_sndcard'] = $this->query("SELECT * FROM glpi_device_sndcard;");
467 return($ret);
468 }else{
469 echo "not connected";
470 return(false);
471 }
472 }
474 function getComputerInformations($name)
475 {
476 if($this->is_connected){
477 /*
478 $ret = $this->query( '
479 SELECT
480 c.ID,
481 c.name as "Name",
482 c.comments as "Commets",
483 d.name as "OS",
484 e.name as "Manufacturer",
485 dc.name as "Type",
486 u.name as "Technical responsible"
487 FROM
488 glpi_computers as c
489 left join glpi_dropdown_os as d on (c.os = d.ID)
490 left join glpi_enterprises as e on (c.FK_glpi_enterprise = e.ID)
491 left join glpi_type_computers as dc on (c.type = dc.ID)
492 left join glpi_users as u on (c.tech_num = u.ID)
494 WHERE c.Name!="" AND c.ID='.$id.';');
495 */
496 $ret = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
497 return($ret);
498 }else{
499 echo "not connected";
500 return(false);
501 }
502 }
504 function updateComputerInformations($array,$name)
505 {
506 if(!is_array($array)){
507 echo "updateComputerInformations: first paraeter must be an array";
508 }elseif($this->is_connected){
509 $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
510 if(isset($tmp[0])){
512 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
513 "tech_num","comments","date_mod","os","location","domain","network",
514 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
515 $v = "";
516 foreach($atr as $at){
517 if(isset($array[$at])){
518 $v .= " ".$at."='".$array[$at]."', ";
519 }
520 }
521 if(empty($v)){
522 echo "updateComputerInformations: no attributes given ";
523 return(false);
524 }else{
525 $v = preg_replace("/, $/","",$v);
526 return($this->query("UPDATE glpi_computers SET ".$v." WHERE name='".$name."';"));
527 }
528 }else{
529 echo "can't update not existing entry";
530 return(false);
531 }
532 }else{
533 echo "not connected";
534 return(false);
535 }
537 }
539 function addComputerInformations($array)
540 {
541 if(!is_array($array)){
542 echo "updateComputerInformations: first paraeter must be an array";
543 }elseif($this->is_connected){
544 $atr = array( "ID","name","serial","otherserial","contact","contact_num",
545 "tech_num","comments","date_mod","os","location","domain","network",
546 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
547 $v = "";
548 $a = "";
549 foreach($atr as $at){
550 if(isset($array[$at])){
551 $a .= $at.", ";
552 $v .= "'".$array[$at]."', ";
553 }
554 }
555 if(empty($v)){
556 echo "updateComputerInformations: no attributes given ";
557 return(false);
558 }else{
559 $a = preg_replace("/, $/","",$a);
560 $v = preg_replace("/, $/","",$v);
561 return($this->query("INSERT INTO glpi_computers (".$a.") VALUES (".$v.");"));
562 }
564 }else{
565 echo "not connected";
566 return(false);
567 }
569 }
571 function removeComputerInformations($id)
572 {
573 /* if($this->is_connected){
574 $tmp = $this->query("SELECT * FROM glpi_computers WHERE ID=".$id.";");
575 if(isset($tmp[0])){
576 return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
577 }else{
578 echo "can't remove not existing entry";
579 return(false);
580 }
581 }else{
582 echo "not connected";
583 return(false);
584 }
585 */
586 }
588 function is_connected()
589 {
590 return($this->is_connected);
591 }
593 }
594 //$s = new glpiDB("vserver-01","glpi","tester","glpi");
595 //print_r($s->query("SELECT * FROM glpi_computers"));
596 //$s->getComputerInformations("1 OR (c.ID<10000)");
597 ?>