71306352ca3e6386314129ad3b307abb0295caa8
1 <?php
3 class glpiAccount extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account= TRUE;
12 var $attributes= array("ID","name","contact",
13 "tech_num","comments","date_mod","os","location","domain","network","contact_num",
14 "model","type","is_template","FK_glpi_enterprise","deleted");
16 var $ID ;
17 var $name ="";
18 var $contact ="";
19 var $contact_num ="";
21 var $tech_num ="";
22 var $comments ="";
24 var $addUser ="";
26 var $date_mod ="";
27 var $os =0;
28 var $location =0;
29 var $domain =0;
30 var $network =0;
32 var $model =0;
33 var $type =0;
34 var $is_template =0;
35 var $FK_glpi_enterprise =0;
36 var $deleted ="N";
38 var $objectclasses= array("whatever");
40 var $initialy_was_account = false;
42 var $edit_type =false;
43 var $edit_os =false;
45 var $data;
46 var $handle = NULL;
48 var $cur_dialog = NULL;
50 var $orig_dn;
51 var $ui;
53 var $usedDevices = array();
56 /* Contructor
57 Sets default values and checks if we already have an existing glpi account
58 */
59 function glpiAccount ($config, $dn= NULL)
60 {
61 plugin::plugin ($config, $dn);
62 $this->ui= get_userinfo();
64 if(!isset($this->config->data['SERVERS']['GLPI'])){
65 return;
66 }
68 $this->data = $this->config->data['SERVERS']['GLPI'];
70 if(!is_callable("mysql_connect")){
71 return;
72 }
74 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
76 if(!$this->handle->is_connected){
77 return;
78 }
80 if($this->handle->is_account($this->dn)){
81 $this->is_account = true;
82 $tmp = ($this->handle->getComputerInformations($this->dn));
84 foreach(array("tech_num","os","FK_glpi_enterprise","type","comments","contact_num") as $attr){
85 $this->$attr = $tmp[0][$attr];
86 }
87 $this->usedDevices = $this->handle->getUsedDevices($tmp[0]['ID']);
89 }else{
90 $this->is_account = false;
91 }
93 $this->name = $this->dn;
94 $this->orig_dn = $this->dn;
95 $this->initialy_was_account = $this->is_account;
96 }
98 function execute()
99 {
100 /* Call parent execute */
101 plugin::execute();
103 /* Fill templating stuff */
104 $smarty= get_smarty();
105 $display= "";
107 /* Assign smarty defaults
108 To avoid undefined indexes, if there is an error with the glpi db
109 */
110 foreach(array("SystemTypes","SystemTypeKeys","Manufacturers",
111 "OSs","TechnicalResponsibles","InstalledDevices","Attachments",
112 "OSKeys","OSs","ManufacturerKeys","InstalledDeviceKeys") as $attr){
113 $smarty->assign($attr,array());
114 $smarty->assign($attr."ACL"," disabled ");
115 }
116 foreach(array("type","FK_glpi_enterprise","os","tech_num","comments","contact_num") as $attr){
117 $smarty->assign($attr,"");
118 $smarty->assign($attr."ACL"," disabled ");
119 }
121 /* Check if there is a glpi database server defined
122 */
123 if(!isset($this->config->data['SERVERS']['GLPI'])){
124 print_red(_("There is no server with valid glpi database service."));
125 return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
126 }
128 $this->data = $this->config->data['SERVERS']['GLPI'];
130 /* Check if we can call mysql_connect
131 If we can't, there is no the mysql-php extension
132 */
133 if(!is_callable("mysql_connect")){
134 print_red(_("Can't connect to glpi database, the php-mysql extension is missing."));
135 return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
136 }
138 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
140 /* If handle == false, abort
141 Seems that the server, username and or password is wrong
142 */
143 if(!$this->handle->is_connected){
144 print_red(_("Can't connect to glpi database, check configuration twice."));
145 return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
146 }
148 /* All check are ok
149 Lets handle Posts, templates etc below ...
150 */
152 $users = $this->handle->getUsers();
153 $ldap= $this->config->get_ldap_link();
155 /* Add Device was requested, open new dialog
156 */
157 if(isset($_POST['AddDevice'])){
158 $this->dialog =true;
159 $this->cur_dialog = new glpiDeviceManagement($this->config,$this->dn,$this->usedDevices);
160 }
165 /* Attachment pool was closed with use
166 */
167 if(isset($_POST['UseAttachment'])){
168 if(count($this->cur_dialog->check())){
169 foreach($this->cur_dialog->check() as $msg){
170 print_red($msg);
171 }
172 }else{
173 /****/
174 echo "blabla attaching something ..... ";
176 }
177 }
179 /* Attachment pool was closed with abort
180 */
181 if(isset($_POST['AbortAttachment'])){
182 $this->cur_dialog = false;
183 $this->edit_type = false;
184 }
186 /* Open Attachment pool to add/edit Attachments
187 */
188 if(isset($_POST['AddAttachment'])){
189 $this->cur_dialog = new glpiAttachmentPool($this->config,$this->dn);
190 $this->dialog = true;
191 }
193 /* Remove Attachment fro this tab
194 */
195 if(isset($_POST['RemoveAttachment'])){
197 }
199 /* We have selected some devices and pressed use button
200 */
201 if(isset($_POST['SelectDeviceSave'])){
202 $this->cur_dialog->save_object();
203 $this->usedDevices= ($this->cur_dialog->getSelected());
204 $this->cur_dialog = false;
205 $this->dialog = false;
206 $this->edit_type=false;
207 }
209 /* Aborted Device selction
210 */
211 if(isset($_POST['SelectDeviceCancel'])){
212 $this->dialog = false;
213 $this->cur_dialog = false;
214 $this->edit_type=false;
215 }
217 /* System type management
218 */
219 if(isset($_POST['edit_type'])){
220 $this->dialog = true;
221 $this->edit_type=true;
222 }
224 /* This closes the system type editing dialog
225 */
226 if(isset($_POST['close_edit_type'])){
227 $this->edit_type=false;
228 $this->dialog = false;
229 }
231 /* This appends a new system to our sytem types
232 */
233 if((isset($_POST['add_type']))&&(!empty($_POST['type_string']))){
234 $this->handle->addSystemType($_POST['type_string']);
235 }
237 /* Remove selected type from our system types list
238 */
239 if((isset($_POST['del_type']))&&(!empty($_POST['select_type']))){
240 $this->handle->removeSystemType($_POST['select_type']);
241 }
243 /* Rename selected system type to given string
244 */
245 if((isset($_POST['rename_type']))&&(!empty($_POST['select_type']))&&(!empty($_POST['type_string']))){
246 $this->handle->updateSystemType($_POST['type_string'],$_POST['select_type']);
247 }
249 /* Someone wants to edit the system types ...
250 So, lets open a new dialog which provides some buttons to edit the types
251 */
252 if($this->edit_type){
253 $smarty->assign("SystemTypes", $this->handle->getSystemTypes());
254 $smarty->assign("SystemTypeKeys", array_flip($this->handle->getSystemTypes()));
255 $display= $smarty->fetch(get_template_path('glpi_edit_type.tpl', TRUE));
256 return($display);
257 }
259 /* System os management
260 */
261 if(isset($_POST['edit_os'])){
262 $this->dialog = true;
263 $this->edit_os=true;
264 }
266 /* Close Operating system dialog
267 */
268 if(isset($_POST['close_edit_os'])){
269 $this->edit_os=false;
270 $this->dialog = false;
271 }
273 /* Add new os to the db
274 */
275 if((isset($_POST['add_os']))&&(!empty($_POST['is_string']))){
276 $this->handle->addOS($_POST['is_string']);
277 }
279 /* Delete selected os from list and db
280 */
281 if((isset($_POST['del_os']))&&(!empty($_POST['select_os']))){
282 $this->handle->removeOS_byID($_POST['select_os']);
283 }
285 /* Rename selected os to given string
286 */
287 if((isset($_POST['rename_os']))&&(!empty($_POST['select_os']))&&(!empty($_POST['is_string']))){
288 $this->handle->updateOS($_POST['is_string'],$_POST['select_os']);
289 }
291 /* Open dialog to edit os types
292 */
293 if($this->edit_os){
294 $smarty->assign("OSKeys", array_flip($this->handle->getOSTypes()));
295 $smarty->assign("OSs", $this->handle->getOSTypes());
296 $display= $smarty->fetch(get_template_path('glpi_edit_os.tpl', TRUE));
297 return($display);
298 }
300 /* Show dialog to select a new contact person
301 * Select a contact person
302 */
303 if(isset($_POST['SelectContactPerson'])){
304 $this->addUser = "contact";
305 $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
306 }
308 /* Open dialog which allows to edit the manufacturers
309 */
310 if(isset($_POST['edit_manufacturer'])){
311 $this->cur_dialog = new glpiManufacturer($this->config,$this->dn);
312 $this->dialog = true;
313 }
315 /* Close manufacturer editing dialog
316 */
317 if(isset($_POST['close_edit_manufacturer'])){
318 $this->dialog = false;
319 $this->cur_dialog = false;
320 }
322 /* Abort user selection
323 */
324 if(isset($_POST['SelectUserCancel'])){
325 $this->dialog = false;
326 $this->addUser ="";
327 $this->cur_dialog = false;
328 }
330 /* Selecte technical responsible person
331 */
332 if(isset($_POST['SelectTechPerson'])){
333 $this->addUser ="tech";
334 $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
335 }
337 /* Technical responsible person selected*/
338 if(isset($_GET['act'])&&($_GET['act']=="user_tech_num")){
340 /* Get posted id */
341 $id = base64_decode($_GET['id']);
343 /* Check if user is already created in glpi database */
344 if(!in_array($id,$users)){
346 /* If this user doesn't exists in glpi db, we must create him */
347 $atr = $ldap->fetch($ldap->cat($id));
348 $tmp = array();
349 $use = array( "cn" =>"name",
350 "mail" =>"email",
351 "telephoneNumber" =>"phone");
353 /* Create array */
354 foreach($use as $gosa => $glpi){
355 if(isset($atr[$gosa])){
356 $tmp[$glpi]= $atr[$gosa][0];
357 }
358 }
360 /* Add this user */
361 $this->handle->addUser($tmp,$id);
362 }
364 /* Re-read users */
365 $users = ($this->handle->getUsers());
367 /* Get user */
368 $tmp = array_flip($users);
369 $id=$tmp[$id];
371 /* Use user id, close dialog */
372 if($this->addUser == "tech"){
373 $this->tech_num = $id;
374 }else{
375 $this->contact_num = $id;
376 }
377 $this->cur_dialog = false;
378 $this->dialog= false;
379 }
381 /* if( cur_dialog != false || cur_dialog != NULL)
382 * There is a dialog which wants to be displayed
383 */
384 if($this->cur_dialog){
385 $this->cur_dialog->save_object();
386 $this->dialog=true;
387 $this->cur_dialog->parent = &$this;
388 return($this->cur_dialog->execute());
389 }
391 /* Assign smarty defaults */
392 foreach(array("SystemTypes","SystemTypeKeys","Manufacturers","OSs","TechnicalResponsibles","InstalledDevices","Attachments") as $attr){
393 $smarty->assign($attr,array());
394 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
395 }
397 /* Assign some vars to smarty
398 */
399 foreach(array("type","FK_glpi_enterprise","os","tech_num","contact_num") as $attr){
400 $smarty->assign($attr,"");
401 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
402 }
404 /* Do we need to flip is_account state? */
405 if (isset($_POST['modify_state'])){
406 $this->is_account= !$this->is_account;
407 }
409 /* Show tab dialog headers */
410 if ($this->is_account){
411 $display= $this->show_header(_("Remove inventory service"),
412 _("This server has inventory features enabled. You can disable them by clicking below."));
413 } else {
414 $display= $this->show_header(_("Add inventory service"),
415 _("This server has inventory features disabled. You can enable them by clicking below."));
416 return ($display);
417 }
419 /* Assign ACLs to smarty*/
420 foreach($this->attributes as $attr){
421 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
422 }
424 /* Assign system types
425 */
426 $smarty->assign("SystemTypeKeys", array_flip($this->handle->getSystemTypes()));
427 $smarty->assign("SystemTypes", $this->handle->getSystemTypes());
428 $smarty->assign("type", $this->type);
430 /* Assign os types
431 */
432 $smarty->assign("OSKeys", array_flip($this->handle->getOSTypes()));
433 $smarty->assign("OSs", $this->handle->getOSTypes());
434 $smarty->assign("os", $this->os);
436 /* Dispaly installed devices */
437 $smarty->assign("InstalledDevices" ,$this->getDevices());
438 $smarty->assign("InstalledDeviceKeys" ,array_flip($this->getDevices()));
440 /* Append manufacturers
441 */
442 $smarty->assign("ManufacturerKeys", array_flip($this->handle->getEnterprises()));
443 $smarty->assign("Manufacturers", $this->handle->getEnterprises());
444 $smarty->assign("FK_glpi_enterprise", $this->FK_glpi_enterprise);
446 /* Handle contact person
447 Assign name ... to smarty, if set
448 */
449 if(isset($users[$this->contact_num])){
450 $tr = $ldap->fetch($ldap->cat($users[$this->contact_num]));
451 $str = "";
452 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
453 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
454 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
455 $smarty->assign("contact_num", $str);
456 }else{
457 $smarty->assign("contact_num", _("N/A"));
458 }
460 /* Handle tech person
461 Assign name ... to smarty, if set
462 */
463 if(isset($users[$this->tech_num])){
464 $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
465 $str = "";
466 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
467 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
468 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
469 $smarty->assign("tech_num", $str);
470 }else{
471 $smarty->assign("tech_num", _("N/A"));
472 }
473 $smarty->assign("comments", $this->comments);
475 $display.= $smarty->fetch(get_template_path('glpi.tpl', TRUE));
476 return($display);
477 }
479 function remove_from_parent()
480 {
481 }
483 function getDevices(){
484 $ret = array();
485 foreach($this->usedDevices as $type => $entries){
486 foreach($entries as $ent){
487 $ret[] = $ent['designation']." [".$type."]";
488 }
489 }
490 return($ret);
491 }
494 /* Save data to object */
495 function save_object()
496 {
497 plugin::save_object();
498 foreach($this->attributes as $attrs){
499 if(isset($_POST[$attrs])){
500 $this->$attrs = $_POST[$attrs];
501 }
502 }
503 }
506 /* Check supplied data */
507 function check()
508 {
509 $message= array();
511 // if($this->TechnicalResponsible == ""){
512 // $message[] = _("Please select a technical responsible person for this entry.");
513 // }
515 return ($message);
516 }
518 /* Save to LDAP */
519 function save()
520 {
521 if($this->is_account){
522 $attrs = array();
523 $this->date_mod = date("Y-m-d H:i:s");
524 foreach($this->attributes as $attr){
525 $attrs[$attr] = $this->$attr;
526 }
527 $attrs['name'] = $this->dn;
528 unset($attrs['ID']);
529 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
530 if($this->initialy_was_account&&$this->is_account){
531 $this->handle->updateComputerInformations($attrs,$this->dn);
532 }elseif($this->is_account){
533 $this->handle->addComputerInformations($attrs,$this->dn);
534 }
535 $tmp = ($this->handle->getComputerInformations($this->dn));
536 $this->handle->addDevicesToComputer($this->usedDevices,$tmp[0]['ID']);
537 }
538 }
540 }
542 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
543 ?>