Code

c6b177fadef392029f8476493596f14808030cda
[gosa.git] / plugins / admin / systems / class_printGeneric.inc
1 <?php
3 class printgeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage terminal base 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   /* Generic terminal attributes */
11   var $interfaces     = array();
12   var $ignore_account = FALSE;
14   /* Needed values and lists */
15   var $base             = "";
16   var $cn               = "";
17   var $macAddress       = "";
18   var $ipHostNumber     = "";
19   var $l                = "";
20   var $description      = "";
21   var $labeledURI       = "";
22   var $gotoPrinterPPD   = "";
23   var $orig_dn          = "";
24   var $is_terminal      = false;
26   var $UserMember       ="";
27   var $UserMembers      =array();
28   var $UserMemberKeys   =array();
29   
30   var $AdminMember      ="";
31   var $AdminMembers     =array();
32   var $AdminMemberKeys  =array();
34   var $is_terminalBased = false; 
35  
36   var $member           =array();
37   var $strings          = "";
38   var $type             = "";
39   var $dialog           =NULL;
41   /* attribute list for save action */
42   var $attributes     = array("cn", "description", "l", "labeledURI", "macAddress", "ipHostNumber","gotoPrinterPPD");
43   var $objectclasses  = array("top", "gotoPrinter");
45   function printgeneric ($config, $dn= NULL)
46   {
47     plugin::plugin ($config, $dn);
48     $ldap= $this->config->get_ldap_link();
50     /* Set base */
51     if ($this->dn == "new"){
52       $ui= get_userinfo();
53       $this->base= dn2base($ui->dn);
54       $this->cn= "";
55       $this->is_terminal = true;
56     } else {
57       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
58     }
61     /* In case of gotoWorkstation this tab is calles from workstation plugin
62      * in case of gotoTerminal it is called from a terminal tab
63      * else it is a standalone printer
64      */
65     if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
66       $this->is_terminal = "true";   
67       $this->dn   = preg_replace("/ou=workstation/","ou=printer",$this->dn);
68       $this->type = "station";
69     }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
70       $this->type = "terminal";
71       $this->is_terminal = "true";
72       $this->dn   = preg_replace("/ou=terminal/","ou=printer",$this->dn);
73     }else{
74       /* Save dn for later references */
75       $this->orig_dn= $this->dn;
76     }
78     /* If it is no standalone printer 
79      */
80     if($this->is_terminal){
81       // Reload plugin with new dn... (ou=printers instead of ou=terminals)
82       plugin::plugin ($this->config, $this->dn);
83       $ldap->cat($this->dn);
84       if(count($ldap->fetch())>0){
85         $this->orig_dn= $this->dn;
86         $this->is_account=true;
87         $this->initially_was_account = true;
88       }else{
89         $this->orig_dn = "new";
90         $this->is_account=false;
91         $this->initially_was_account = false;
92       }
93     }
95    
96     /* Prepare different member types 
97      */ 
98     foreach(array("AddUser"       =>"gotoUserPrinter",
99                   "AddGroup"      =>"gotoGroupPrinter",
100                   "AddAdminUser"  =>"gotoUserAdminPrinter",
101                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
103       /* $this->members contains all members */
104       $this->member[$type]=array();
106       unset($this->attrs[$attr]['count']);
108       if(isset($this->attrs[$attr])){
109         foreach($this->attrs[$attr] as $mem){
110           if(preg_match("/Group/",$type)){
111             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
112             $entry = $ldap->fetch();
113             if(isset($entry['description'])){
114               $this->member[$type][$entry['cn'][0]]=$entry;
115             }
116           }else{
117             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
118             $entry = $ldap->fetch();
119             if(isset($entry['uid'])){
120               $this->member[$type][$entry['uid'][0]]=$entry;
121             }
122           }
123         }
124       }
125     }
126   
127     $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
128     if($attrs = $ldap->fetch()){
129       if(in_array("gotoTerminal",$attrs['objectClass'])){
130         $this->is_terminalBased = "Terminal";
131       }else{
132         $this->is_terminalBased = "Workstation";
133       }
134     }
138   }
140   function execute()
141   {
143     $smarty= get_smarty();
144     $display="";
145     /* Template management.
146      * There are two ways to call this tab.
147      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
148      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
149      *    Toggle Account state will be shown in this case, to disable or enable this tab.
150      *
151      * $this->is_terminal indecates this two different types.
152      */
153     if($this->is_terminal){    
154       $smarty->assign("is_terminal","true");
155     }else{
156       $smarty->assign("is_terminal","false");
157     }
159     /* Do we need to flip is_account state? */
160     if (isset($_POST['modify_state'])){
161       $this->is_modified = true;
162       $this->is_account= !$this->is_account;
163     }
165     if($this->is_terminal){
166       if(empty($this->cn)){
167         if(!in_array("gotoPrinter",$this->objectclasses)){
168           $display= $this->show_header(_("Add printer extension"),
169               _("This workstation has printer extension disabled. You can't enable it while 'cn' is not present in entry. Possibly you are currently creating a new terminal template"),TRUE,TRUE);
170           $this->is_account= false;
171           return $display;
172         }else{
173           $smarty->assign("is_terminal","false");
174           $skip = true;
175         }
176       }
178       if(!isset($skip)){
179         if (($this->is_account)){
180           if($this->type=="station"){
181             $display= $this->show_header(_("Remove printer extension"),
182                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
183           }else{
184             $display= $this->show_header(_("Remove printer extension"),
185                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
186           }
187         }else{
188           if($this->type=="station"){
189             $display= $this->show_header(_("Add printer extension"),
190                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
191           }else{
192             $display= $this->show_header(_("Add printer extension"),
193                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
194           }  
195           return ($display);
196         }
197       }
198     }
201     /* Do we represent a valid printer? */
202     if (!$this->is_account && $this->parent == NULL){
203       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
204         _("This 'dn' has no printer features.")."</b>";
205       return($display);
206     }
208     /* Fill templating stuff */
209     $smarty->assign("bases", $this->config->idepartments);
210     $smarty->assign("base_select", $this->base);
212     /* Assign attributes */
213     foreach ($this->attributes as $attr){
214       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
215       $smarty->assign("$attr", $this->$attr);
216     }
218     if(isset($_POST['AddUser'])){
219       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
220     }
221     if(isset($_POST['AddGroup'])){
222       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
223     }
224     if(isset($_POST['AddAdminUser'])){
225       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
226     }
227     if(isset($_POST['AddAdminGroup'])){
228       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
229     }
231     if(isset($_POST['EditDriver'])){
232       $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
233     }
235     if(isset($_POST['PrinterCancel'])){
236       unset($this->dialog);
237       $this->dialog= NULL;
238     }
240     if(isset($_POST['SavePPD'])){
241       if(count($this->dialog->check())){
242         foreach($this->dialog->check() as $msg){
243           print_red($msg);
244         }
245       }else{
246         $this->gotoPrinterPPD = array();
247         $this->gotoPrinterPPD = $this->dialog->save();
248         unset($this->dialog);
249         $this->dialog=NULL;
250       }
252     }
254     if(isset($_POST['ClosePPD'])){
255       unset($this->dialog);
256       $this->dialog=NULL;
257     }
259     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
260       if(isset($this->member['AddUser'][$_POST['UserMember']])){
261         unset($this->member['AddUser'][$_POST['UserMember']]);
262       }
263     }
265     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
266       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
267         unset($this->member['AddGroup'][$_POST['UserMember']]);
268       }
269     }
271     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
272       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
273         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
274       }
275     }
277     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
278       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
279         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
280       }
281     }
283     if(isset($_POST['PrinterSave'])){
284       $this->dialog->save_object();
285       if(count($this->dialog->check())){
286         foreach($this->dialog->check() as $msg){
287           print_red($msg);
288         }
289       }else{
290         $new = $this->dialog->save();
291         $data = $new;
292         unset($data['type']);
294         if(preg_match("/User/",$new['type'])){
295           $use = "uid";
296         }else{
297           $use = "cn";
298         }
300         foreach($data as $mem){
301           $this->member[$new['type']][$mem[$use][0]]=$mem;    
302         }
303         unset($this->dialog);
304         $this->dialog=NULL; 
305       }
306     }
308     if($this->dialog != NULL){
309       $display = $this->dialog->execute();
310       return $display;
311     }
313     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
314     $smarty->assign("staticAddress", "");
317     require_once ("class_ppdManager.inc");
318     $ppdManager= new ppdManager('/var/spool/ppd/');
319     if(!empty($this->gotoPrinterPPD)){
320       $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
321     }else{
322       $smarty->assign("driverInfo", _("Undefined"));
323     }
325     $list=$this->generateList();
326     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
327     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
329     asort($userlist);
330     asort($adminlist);
331     if($this->is_terminalBased){
332       if($this->is_terminalBased == "Terminal"){
333         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
334       }else{
335         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
336       }
337       $smarty->assign("cnACL"    ," disabled ");
338     }else{
339       $smarty->assign("desc"    ,_(""));
340     }
341     $smarty->assign("UserMember"    ,$this->UserMember);
342     $smarty->assign("UserMembers"   ,$userlist);
343     $smarty->assign("UserMemberKeys",array_flip($userlist));
345     $smarty->assign("AdminMember"    ,$this->AdminMember);
346     $smarty->assign("AdminMembers"   ,$adminlist);
347     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
348     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
350     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
351   }
353   function remove_from_parent()
354   {
355     $ldap= $this->config->get_ldap_link();
356     $ldap->rmdir($this->dn);
357     show_ldap_error($ldap->get_error());
358     $this->handle_post_events("remove");
360     /* Delete references to object groups */
361     $ldap->cd ($this->config->current['BASE']);
362     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
363     while ($ldap->fetch()){
364       $og= new ogroup($this->config, $ldap->getDN());
365       unset($og->member[$this->dn]);
366       $og->save ();
367     }
368   }
370   /* Save data to object */
371   function save_object()
372   {
373     plugin::save_object();
375     /* Save base, since this is no LDAP attribute */
376     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
377       $this->base= $_POST['base'];
378     }
379   }
381   /* Check supplied data */
382   function check()
383   {
384     $message= array();
385     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
387     /* must: cn */
388     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
389       $message[]= "The required field 'Printer name' is not set.";
390     }
392     $ui= get_userinfo();
393     $acl= get_permissions ($this->dn, $ui->subtreeACL);
394     $acl= get_module_permission($acl, "printer", $this->dn);
395     if (chkacl($acl, "create") != ""){
396       $message[]= _("You have no permissions to create a printer on this 'Base'.");
397     }
399     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
400       $ldap= $this->config->get_ldap_link();
401       $ldap->cd ($this->base);
402       $ldap->search ("(cn=".$this->cn.")", array("cn"));
403       if ($ldap->count() != 0){
404         while ($attrs= $ldap->fetch()){
405           if ($attrs['dn'] != $this->orig_dn){
406             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
407             break;
408           }
409         }
410       }
411     }
413     return ($message);
414   }
417   /* Save to LDAP */
418   function save()
419   {
420     $dn= $this->dn;
421     plugin::save();
422     $ldap= $this->config->get_ldap_link();
423    
424     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
425       $this->dn= preg_replace("/ou=terminal/","ou=printer",$this->dn);
426     }
427     
428     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
429       $this->dn= preg_replace("/ou=workstation/","ou=printer",$this->dn);
430     }
432     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
433     */
434     if($this->is_terminal){
435       
436       /* reduce objectClasses to minimun */
437       $this->attrs['objectClass']= $this->objectclasses;
439       /* If a printer with the given dn exists, modify else create new one */
440       $ldap->cat($this->dn);
441       if($ldap->fetch()){
442         $this->orig_dn= $this->dn;
443       }else{
444         $this->orig_dn = "new";
445       }
446     }
448     /* Remove all empty values */
449     if ($this->orig_dn == 'new'){
450       $attrs= array();
451       foreach ($this->attrs as $key => $val){
452         if (is_array($val) && count($val) == 0){
453           continue;
454         }
455         $attrs[$key]= $val;
456       }
457       $this->attrs= $attrs;
458     }
461     /* Append printer user 
462      */
463     foreach($this->member['AddUser'] as $mem){
464       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
465     }
467     /* Append printer group 
468      */
469     foreach($this->member['AddGroup'] as $mem){
470       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
471     }
473     /* Append printer admin user 
474      */
475     foreach($this->member['AddAdminUser'] as $mem){
476       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
477     }
478     
479     /* Append printer admin group 
480      */
481     foreach($this->member['AddAdminGroup'] as $mem){
482       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
483     }
485     /* Write back to ldap */
486     $ldap= $this->config->get_ldap_link();
487     if ($this->orig_dn == 'new'){
488       $ldap->cd($this->config->current['BASE']);
489       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
490       $ldap->cd($this->dn);
491       $ldap->add($this->attrs);
492       $this->handle_post_events("add");
493     } else {
494       if ($this->orig_dn != $this->dn){
495         $this->move($this->orig_dn, $this->dn);
496       }
498       $ldap->cd($this->dn);
499       $ldap->modify($this->attrs);
500       $this->handle_post_events("modify");
501     }
502     show_ldap_error($ldap->get_error());
504     /* Optionally execute a command after we're done */
505     $this->postcreate();
506   }
508   function generateList(){
509     $a_return=array();
510     foreach($this->member as $type => $values){
511       $a_return[$type]=array();
512       foreach($values as $value){
513         if((preg_match("/Group/i",$type))){
514           if(!isset($value['description'])){
515             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
516           }else{
517             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
518           }
519         }else{
520           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
521         }
522       }
523     }
524     return($a_return);
525   }
530 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
531 ?>