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= FALSE;
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 ; // Is set if this entry is edited
17 var $name = ""; // This should be the dn of this entry
18 var $contact = ""; // Empty
20 var $comments = ""; // Comment
22 var $contact_num = ""; // Contact person
23 var $tech_num = ""; // Technical responsible person
25 var $addUser = ""; // This is used to remember if a dialog was opened for tech_num or contact_num
27 var $date_mod = ""; // Modification timestamp
28 var $os = 0; // Operating system
29 var $location = 0; // Not used yet
30 var $domain = 0; // ? Set to 0
31 var $network = 0; // ? Set to 0
33 var $model = 0; // ? Can't remember this, it isn't used in GOsa
34 var $type = 0; // System type id
35 var $is_template = 0; // Used as template ?
36 var $FK_glpi_enterprise = 0; // Manufacturer id
37 var $deleted = "N"; // Deleted entries should have this set to Y
39 /* Not necessary, cause we use mysql databse */
40 var $objectclasses= array("whatever");
42 /* Used to remember if this was an account (simply: is this an edited entry) */
43 var $initialy_was_account = false;
45 /* Remember current dialog */
46 var $edit_type = false;
47 var $edit_os = false;
49 var $data;
50 var $handle = NULL; // Glpi class handle used to query database
52 var $cur_dialog = NULL; // This contains the sub dialog handle
54 var $orig_dn; // To check if dn, has changed
55 var $ui; // Some GOsa specific user informations
57 var $usedDevices = array(); // Which devices are currently selected
58 var $usedAttachments = array(); // Used Attachments
60 /* Contructor
61 Sets default values and checks if we already have an existing glpi account
62 */
63 function glpiAccount ($config, $dn= NULL)
64 {
65 plugin::plugin ($config, $dn);
66 $this->ui= get_userinfo();
68 /* Abort class construction, if no db is defined */
69 if(!isset($this->config->data['SERVERS']['GLPI'])){
70 return;
71 }
73 // Get informations about databse connection
74 $this->data = $this->config->data['SERVERS']['GLPI'];
76 // Abort if mysql extension is missing
77 if(!is_callable("mysql_connect")){
78 return;
79 }
81 // Create handle of glpi class, and check if database connection is established
82 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
84 if(!$this->handle->is_connected){
85 return;
86 }
88 // If this dn is already used in database, then get all informations for this entry
89 if($this->handle->is_account($this->dn)){
90 $this->is_account = true;
91 $tmp = ($this->handle->getComputerInformations($this->dn));
93 foreach(array("tech_num","os","FK_glpi_enterprise","type","comments","contact_num") as $attr){
94 $this->$attr = $tmp[0][$attr];
95 }
96 $this->usedDevices = $this->handle->getUsedDevices($tmp[0]['ID']);
97 $atts = $this->handle->getAssignAttachments($tmp[0]['ID']);
98 foreach($atts as $attachment){
100 $this->usedAttachments[$attachment['FK_doc']]=$attachment['FK_doc'];
101 }
102 }else{
103 $this->is_account = false;
104 }
106 /* set defaults */
107 $this->name = $this->dn;
108 $this->orig_dn = $this->dn;
109 $this->initialy_was_account = $this->is_account;
112 }
114 function execute()
115 {
116 /* Call parent execute */
117 plugin::execute();
119 /* Fill templating stuff */
120 $smarty= get_smarty();
121 $display= "";
123 /* Assign smarty defaults
124 To avoid undefined indexes, if there is an error with the glpi db
125 */
126 foreach(array("SystemTypes","SystemTypeKeys","Manufacturers",
127 "OSs","TechnicalResponsibles","InstalledDevices","Attachments","AttachmentKeys",
128 "OSKeys","OSs","ManufacturerKeys","InstalledDeviceKeys") as $attr){
129 $smarty->assign($attr,array());
130 $smarty->assign($attr."ACL"," disabled ");
131 }
132 foreach(array("type","FK_glpi_enterprise","os","tech_num","comments","contact_num") as $attr){
133 $smarty->assign($attr,"");
134 $smarty->assign($attr."ACL"," disabled ");
135 }
137 /* Check if there is a glpi database server defined
138 */
139 if(!isset($this->config->data['SERVERS']['GLPI'])){
140 print_red(_("There is no server with valid glpi database service."));
141 return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
142 }
144 $this->data = $this->config->data['SERVERS']['GLPI'];
146 /* Check if we can call mysql_connect
147 If we can't, there is no the mysql-php extension
148 */
149 if(!is_callable("mysql_connect")){
150 print_red(_("Can't connect to glpi database, the php-mysql extension is missing."));
151 return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
152 }
154 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
156 /* If handle == false, abort
157 Seems that the server, username and or password is wrong
158 */
159 if(!$this->handle->is_connected){
160 print_red(_("Can't connect to glpi database, check configuration twice."));
161 return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
162 }
164 /* All checks are ok
165 Lets handle Posts, templates etc below ...
166 */
168 $users = $this->handle->getUsers();
169 $ldap= $this->config->get_ldap_link();
171 /* Check for Trading button Post
172 */
173 if(isset($_POST['Trading'])){
174 print_red(_("This feature is not implemented yet."));
175 }
177 /* Check for Software button Post
178 */
179 if(isset($_POST['Software'])){
180 print_red(_("This feature is not implemented yet."));
181 }
183 /* Check for Contract button Post
184 */
185 if(isset($_POST['Contracts'])){
186 print_red(_("This feature is not implemented yet."));
187 }
189 /* Add Device was requested, open new dialog
190 */
191 if(isset($_POST['AddDevice'])){
192 $this->dialog =true;
193 $this->cur_dialog = new glpiDeviceManagement($this->config,$this->dn,$this->usedDevices);
194 }
196 /* Attachment pool was closed with use
197 */
198 if(isset($_POST['UseAttachment'])){
199 if(count($this->cur_dialog->check())){
200 foreach($this->cur_dialog->check() as $msg){
201 print_red($msg);
202 }
203 }else{
204 $this->cur_dialog->save_object();
205 $this->usedAttachments = $this->cur_dialog->save();
206 $this->cur_dialog = false;
207 $this->edit_type = false;
208 }
209 }
211 /* Attachment pool was closed with abort
212 */
213 if(isset($_POST['AbortAttachment'])){
214 $this->cur_dialog = false;
215 $this->edit_type = false;
216 }
218 /* Open Attachment pool to add/edit Attachments
219 */
220 if(isset($_POST['AddAttachment'])){
221 $this->cur_dialog = new glpiAttachmentPool($this->config,$this->dn,$this->usedAttachments);
222 $this->dialog = true;
223 }
225 /* Remove Attachment fro this tab
226 */
228 $once = true;
229 foreach($_POST as $name => $value){
230 if((preg_match("/^delAttachment_/",$name))&&($once)){
231 $once= false;
232 $name = preg_replace("/^delAttachment_/","",$name);
233 $entry = preg_replace("/_.*$/","",$name);
234 if(isset($this->usedAttachments[$entry])){
235 unset($this->usedAttachments[$entry]);
236 }
237 }
238 }
239 if((isset($_POST['RemoveAttachment']))&&(isset($_POST['Attachments']))){
240 foreach($_POST['Attachments'] as $entry){
241 if(isset($this->usedAttachments[$entry])){
242 unset($this->usedAttachments[$entry]);
243 }
244 }
245 }
247 /* We have selected some devices and pressed use button
248 */
249 if(isset($_POST['SelectDeviceSave'])){
250 $this->cur_dialog->save_object();
251 $this->usedDevices= ($this->cur_dialog->getSelected());
252 $this->cur_dialog = false;
253 $this->dialog = false;
254 $this->edit_type=false;
255 }
257 /* Aborted Device selction
258 */
259 if(isset($_POST['SelectDeviceCancel'])){
260 $this->dialog = false;
261 $this->cur_dialog = false;
262 $this->edit_type=false;
263 }
265 /* System type management
266 */
267 if(isset($_POST['edit_type'])){
268 $this->dialog = true;
269 $this->edit_type=true;
270 }
272 /* This closes the system type editing dialog
273 */
274 if(isset($_POST['close_edit_type'])){
275 $this->edit_type=false;
276 $this->dialog = false;
277 }
279 /* This appends a new system to our sytem types
280 */
281 if((isset($_POST['add_type']))&&(!empty($_POST['type_string']))){
282 $attr = $this->handle->getSystemTypes();
283 if(in_array(trim($_POST['type_string']),$attr)){
284 print_red(_("Adding new sytem type failed, this system type name is already used.")) ;
285 }else{
286 $this->handle->addSystemType(trim($_POST['type_string']));
287 }
288 }
290 /* Remove selected type from our system types list
291 */
292 if((isset($_POST['del_type']))&&(!empty($_POST['select_type']))){
293 $tmp = $this->handle->is_systemTypeUsed($_POST['select_type']);
294 if(count($tmp)){
295 $names = "";
296 foreach($tmp as $name){
297 $names .= ", ".$name;
298 }
299 $names = preg_replace("/^, /","",$names);
300 $names = trim($names);
301 if(count($tmp) == 3){
302 $names .= " ...";
303 }
304 print_red(sprintf(_("You can't delete this system type, it is still in use by these system(s) '%s'"),$names));
305 }else{
306 $this->handle->removeSystemType($_POST['select_type']);
307 }
308 }
310 /* Rename selected system type to given string
311 */
312 if((isset($_POST['rename_type']))&&(!empty($_POST['select_type']))&&(!empty($_POST['type_string']))){
313 $attr = $this->handle->getSystemTypes();
314 if(in_array(trim($_POST['type_string']),$attr)){
315 print_red(_("Rename failed, this system type name is already used.")) ;
316 }else{
317 $this->handle->updateSystemType($_POST['type_string'],trim($_POST['select_type']));
318 }
319 }
321 /* Someone wants to edit the system types ...
322 So, lets open a new dialog which provides some buttons to edit the types
323 */
324 if($this->edit_type){
325 $smarty->assign("SystemTypes", $this->handle->getSystemTypes());
326 $smarty->assign("SystemTypeKeys", array_flip($this->handle->getSystemTypes()));
327 $display= $smarty->fetch(get_template_path('glpi_edit_type.tpl', TRUE));
328 return($display);
329 }
331 /* System os management
332 */
333 if(isset($_POST['edit_os'])){
334 $this->dialog = true;
335 $this->edit_os=true;
336 }
338 /* Close Operating system dialog
339 */
340 if(isset($_POST['close_edit_os'])){
341 $this->edit_os=false;
342 $this->dialog = false;
343 }
345 /* Add new os to the db
346 */
347 if((isset($_POST['add_os']))&&(!empty($_POST['is_string']))){
348 $attr = $this->handle->getOSTypes();
349 if(in_array(trim($_POST['is_string']),$attr)){
350 print_red(_("Adding new operating system failed, specifed name is already used.")) ;
351 }else{
352 $this->handle->addOS(trim($_POST['is_string']));
353 }
354 }
356 /* Delete selected os from list and db
357 */
358 if((isset($_POST['del_os']))&&(!empty($_POST['select_os']))){
359 $tmp = $this->handle->is_osUsed($_POST['select_os']);
361 if(count($tmp)){
363 $names = "";
364 foreach($tmp as $name){
365 $names .= ", ".$name;
366 }
367 $names = preg_replace("/^, /","",$names);
368 $names = trim($names);
369 if(count($tmp) == 3){
370 $names .= " ...";
371 }
372 print_red(sprintf(_("You can't delete this operating system, it is still in use by these system(s) '%s'"),$names));
374 }else{
375 $this->handle->removeOS_byID($_POST['select_os']);
376 }
377 }
379 /* Rename selected os to given string
380 */
381 if((isset($_POST['rename_os']))&&(!empty($_POST['select_os']))&&(!empty($_POST['is_string']))){
382 $attr = $this->handle->getOSTypes();
383 if(in_array(trim($_POST['is_string']),$attr)){
384 print_red(_("Updating operating system failed, specifed name is already used.")) ;
385 }else{
386 $this->handle->updateOS($_POST['is_string'],$_POST['select_os']);
387 }
388 }
390 /* Open dialog to edit os types
391 */
392 if($this->edit_os){
393 $smarty->assign("OSKeys", array_flip($this->handle->getOSTypes()));
394 $smarty->assign("OSs", $this->handle->getOSTypes());
395 $display= $smarty->fetch(get_template_path('glpi_edit_os.tpl', TRUE));
396 return($display);
397 }
399 /* Show dialog to select a new contact person
400 * Select a contact person
401 */
402 if(isset($_POST['SelectContactPerson'])){
403 $this->addUser = "contact";
404 $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_tech_num");
405 }
407 /* Open dialog which allows to edit the manufacturers
408 */
409 if(isset($_POST['edit_manufacturer'])){
410 $this->cur_dialog = new glpiManufacturer($this->config,$this->dn);
411 $this->dialog = true;
412 }
414 /* Close manufacturer editing dialog
415 */
416 if(isset($_POST['close_edit_manufacturer'])){
417 $this->dialog = false;
418 $this->cur_dialog = false;
419 }
421 /* Abort user selection
422 */
423 $smarty->assign("AbortSelectUser","SelectUserCancel");
424 if(isset($_POST['SelectUserCancel'])){
425 $this->dialog = false;
426 $this->addUser ="";
427 $this->cur_dialog = false;
428 }
430 /* Selecte technical responsible person
431 */
432 if(isset($_POST['SelectTechPerson'])){
433 $this->addUser ="tech";
434 $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_tech_num");
435 }
437 /* Technical responsible person selected*/
438 if(isset($_GET['act'])&&($_GET['act']=="user_tech_num")){
440 /* Get posted id */
441 $id = base64_decode($_GET['id']);
443 /* Check if user is already created in glpi database */
444 if(!in_array($id,$users)){
446 /* If this user doesn't exists in glpi db, we must create him */
447 $atr = $ldap->fetch($ldap->cat($id));
448 $tmp = array();
449 $use = array( "cn" =>"name",
450 "mail" =>"email",
451 "telephoneNumber" =>"phone");
453 /* Create array */
454 foreach($use as $gosa => $glpi){
455 if(isset($atr[$gosa])){
456 $tmp[$glpi]= $atr[$gosa][0];
457 }
458 }
460 /* Add this user */
461 $this->handle->addUser($tmp,$id);
462 }
464 /* Re-read users */
465 $users = ($this->handle->getUsers());
467 /* Get user */
468 $tmp = array_flip($users);
469 $id=$tmp[$id];
471 /* Use user id, close dialog */
472 if($this->addUser == "tech"){
473 $this->tech_num = $id;
474 }else{
475 $this->contact_num = $id;
476 }
477 $this->cur_dialog = false;
478 $this->dialog= false;
479 }
481 /* if( cur_dialog != false || cur_dialog != NULL)
482 * There is a dialog which wants to be displayed
483 */
484 if($this->cur_dialog){
485 $this->cur_dialog->save_object();
486 $this->dialog=true;
487 $this->cur_dialog->parent = &$this;
488 return($this->cur_dialog->execute());
489 }else{
490 $this->dialog= false;
491 }
493 /* Assign smarty defaults */
494 foreach(array("SystemTypes","SystemTypeKeys","Manufacturers","OSs","TechnicalResponsibles","InstalledDevices","Attachments") as $attr){
495 $smarty->assign($attr,array());
496 }
498 foreach($this->attributes as $attr){
499 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
500 }
502 /* Assign some vars to smarty
503 */
504 foreach(array("type","FK_glpi_enterprise","os","tech_num","contact_num","Attachments","InstalledDevices") as $attr){
505 $smarty->assign($attr,"");
506 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
507 }
509 /* Do we need to flip is_account state? */
510 if (isset($_POST['modify_state'])){
511 $this->is_account= !$this->is_account;
512 }
514 /* Show tab dialog headers */
515 if ($this->is_account){
516 $display= $this->show_header(_("Remove inventory service"),
517 _("This server has inventory features enabled. You can disable them by clicking below."));
518 } else {
519 $display= $this->show_header(_("Add inventory service"),
520 _("This server has inventory features disabled. You can enable them by clicking below."));
521 return ($display);
522 }
524 /* Assign ACLs to smarty*/
525 foreach($this->attributes as $attr){
526 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
527 }
529 /* Assign system types
530 */
531 $smarty->assign("SystemTypeKeys", array_flip($this->handle->getSystemTypes()));
532 $smarty->assign("SystemTypes", $this->handle->getSystemTypes());
533 $smarty->assign("type", $this->type);
535 /* Assign os types
536 */
537 $smarty->assign("OSKeys", array_flip($this->handle->getOSTypes()));
538 $smarty->assign("OSs", $this->handle->getOSTypes());
539 $smarty->assign("os", $this->os);
541 /* Dispaly installed devices */
542 $smarty->assign("InstalledDevices" ,$this->getDevices());
543 $smarty->assign("InstalledDeviceKeys" ,array_flip($this->getDevices()));
545 /* Append manufacturers
546 */
547 $smarty->assign("ManufacturerKeys", array_flip($this->handle->getEnterprises()));
548 $smarty->assign("Manufacturers", $this->handle->getEnterprises());
549 $smarty->assign("FK_glpi_enterprise", $this->FK_glpi_enterprise);
551 /* Assign used Attachments
552 */
554 $divlist = new divSelectBox("glpiAttachmentsList");
555 $divlist-> SetHeight(150);
556 $atts = $this->getUsedAttachments(true);
557 $downlink = "<a href='get_attachment.php?id=%s' target='_blank'>%s</a>";
558 $del_link = "<input type='image' src='images/edittrash.png' name='delAttachment_%s'>";
559 foreach($atts as $id => $attachment){
560 $divlist->AddEntry
561 (
562 array(
563 array("string"=>$attachment['name']),
564 array("string"=>$attachment['mime']),
565 array("string"=>sprintf($downlink,$id,$attachment['filename'])),
566 array("string"=>sprintf($del_link,$attachment['ID']),"attach"=>"style='border-right:0px;'"),
567 )
568 );
569 }
571 $smarty->assign("AttachmentsDiv" ,$divlist->DrawList());
572 /* Handle contact person
573 Assign name ... to smarty, if set
574 */
575 if(isset($users[$this->contact_num])){
576 $tr = $ldap->fetch($ldap->cat($users[$this->contact_num]));
577 $str = "";
578 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
579 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
580 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
581 $smarty->assign("contact_num", $str);
582 }else{
583 $smarty->assign("contact_num", _("N/A"));
584 }
586 /* Handle tech person
587 Assign name ... to smarty, if set
588 */
589 if(isset($users[$this->tech_num])){
590 $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
591 $str = "";
592 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
593 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
594 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
595 $smarty->assign("tech_num", $str);
596 }else{
597 $smarty->assign("tech_num", _("N/A"));
598 }
599 $smarty->assign("comments", $this->comments);
601 $display.= $smarty->fetch(get_template_path('glpi.tpl', TRUE));
602 return($display);
603 }
605 function remove_from_parent()
606 {
607 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
608 if($this->initialy_was_account){
609 $this->handle->removeComputerInformations($this->dn);
610 }
612 }
614 function getDevices(){
615 $ret = array();
616 foreach($this->usedDevices as $type => $entries){
617 foreach($entries as $ent){
618 if(isset($ent['designation'])){
619 $ret[] = $ent['designation']." [".$type."]";
620 }else{
621 $ret[] = $ent['name']." [".$type."]";
622 }
623 }
624 }
625 return($ret);
626 }
629 /* Save data to object */
630 function save_object()
631 {
632 if(!isset($_POST['glpi_tpl_posted'])) {
633 return ;
634 }
635 plugin::save_object();
636 foreach($this->attributes as $attrs){
637 if(isset($_POST[$attrs])){
638 $this->$attrs = $_POST[$attrs];
639 }
640 }
641 }
644 /* Check supplied data */
645 function check()
646 {
647 $message= array();
649 // if($this->TechnicalResponsible == ""){
650 // $message[] = _("Please select a technical responsible person for this entry.");
651 // }
653 return ($message);
654 }
656 /* Save to LDAP */
657 function save()
658 {
659 if($this->is_account){
660 $attrs = array();
661 $this->date_mod = date("Y-m-d H:i:s");
662 foreach($this->attributes as $attr){
663 $attrs[$attr] = $this->$attr;
664 }
665 $attrs['name'] = $this->dn;
666 unset($attrs['ID']);
667 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
668 if($this->initialy_was_account&&$this->is_account){
669 $this->handle->updateComputerInformations($attrs,$this->dn);
670 }elseif($this->is_account){
671 $this->handle->addComputerInformations($attrs,$this->dn);
672 }
673 $tmp = $this->handle->getComputerInformations($this->dn);
674 $this->handle->addDevicesToComputer($this->usedDevices,$tmp[0]['ID']);
675 $this->handle->addAttachmentsToComputer($this->usedAttachments,$tmp[0]['ID']);
676 }
677 }
679 /* Return used attachments */
680 function getUsedAttachments($divlist = false)
681 {
682 $atts =$this->handle->getAttachments();
683 $ret = array();
684 foreach($atts as $entry){
685 if(in_array($entry['ID'],$this->usedAttachments)){
686 if($divlist){
687 $ret[$entry['ID']] = $entry;
688 }else{
689 $cm ="";
690 if(isset($entry['comment'])){
691 $cm=" [".$entry['comment']."]";
692 }
693 if(isset($entry['mime'])){
694 $cm.=" -".$entry['mime']."";
695 }
697 $ret[$entry['ID']]= $entry['name'].$cm;
698 }
699 }
700 }
701 return($ret);
702 }
705 }
707 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
708 ?>