Code

0a3ab6e06740c929ca693af206be3783f22d10a0
[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; 
36   var $PPDdialogToSave  = NULL;
37  
38   var $member           =array();
39   var $strings          = "";
40   var $type             = "";
41   var $dialog           =NULL;
43   /* attribute list for save action */
44   var $attributes     = array("cn", "description", "l", "labeledURI", "macAddress", "ipHostNumber","gotoPrinterPPD");
45   var $objectclasses  = array("top", "gotoPrinter");
47   function printgeneric ($config, $dn= NULL)
48   {
49     plugin::plugin ($config, $dn);
50     $ldap= $this->config->get_ldap_link();
52     /* Set base */
53     if ($this->dn == "new"){
54       $ui= get_userinfo();
55       $this->base= dn2base($ui->dn);
56       $this->cn= "";
57       $this->is_terminal = true;
58     } else {
59       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
60     }
63     /* In case of gotoWorkstation this tab is calles from workstation plugin
64      * in case of gotoTerminal it is called from a terminal tab
65      * else it is a standalone printer
66      */
67     if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
68       /* Fix for new Workstations */
69       if(preg_match("/ou=incoming/",$this->dn)){    
70         $this->is_terminal = "true";   
71         $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
72         $this->type = "station";
73       }else{
74         $this->is_terminal = "true";   
75         $this->dn   = preg_replace("/ou=workstations/","ou=printers",$this->dn);
76         $this->type = "station";
77       }
78     }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
79       if(preg_match("/ou=incoming/",$this->dn)){    
80         $this->is_terminal = "true";   
81         $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
82         $this->type = "terminal";
83       }else{
84         $this->type = "terminal";
85         $this->is_terminal = "true";
86         $this->dn   = preg_replace("/ou=terminal/","ou=printers",$this->dn);
87       }
88     }else{
89       /* Save dn for later references */
90       $this->orig_dn= $this->dn;
91     }
93     /* If it is no standalone printer 
94      */
95     if($this->is_terminal){
96       // Reload plugin with new dn... (ou=printers instead of ou=terminals)
97       plugin::plugin ($this->config, $this->dn);
98       $ldap->cat($this->dn);
99       if(count($ldap->fetch())>0){
100         $this->orig_dn= $this->dn;
101         $this->is_account=true;
102         $this->initially_was_account = true;
103       }else{
104         $this->orig_dn = "new";
105         $this->is_account=false;
106         $this->initially_was_account = false;
107       }
108     }
110     /* Prepare different member types 
111      */ 
112     foreach(array("AddUser"       =>"gotoUserPrinter",
113                   "AddGroup"      =>"gotoGroupPrinter",
114                   "AddAdminUser"  =>"gotoUserAdminPrinter",
115                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
117       /* $this->members contains all members */
118       $this->member[$type]=array();
120       unset($this->attrs[$attr]['count']);
122       if(isset($this->attrs[$attr])){
123         foreach($this->attrs[$attr] as $mem){
124           if(preg_match("/Group/",$type)){
125             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
126             $entry = $ldap->fetch();
127             if(isset($entry['description'])){
128               $this->member[$type][$entry['cn'][0]]=$entry;
129             }
130           }else{
131             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
132             $entry = $ldap->fetch();
133             if(isset($entry['uid'])){
134               $this->member[$type][$entry['uid'][0]]=$entry;
135             }
136           }
137         }
138       }
139     }
140   
141     $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
142     if($attrs = $ldap->fetch()){
143       if(in_array("gotoTerminal",$attrs['objectClass'])){
144         $this->is_terminalBased = "Terminal";
145       }else{
146         $this->is_terminalBased = "Workstation";
147       }
148     }
150     /* A printer was only saved if the printer belongs to a workstation. 
151      * If there was a new single printer created, it was never saved.
152      * -This allows new printers to be saved.
153      */
154     if($this->dn == "new"){
155       $this->is_account = true;
156     }
157   }
159   function execute()
160   {
161         /* Call parent execute */
162         plugin::execute();
163     $smarty= get_smarty();
164     $display="";
165     /* Template management.
166      * There are two ways to call this tab.
167      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
168      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
169      *    Toggle Account state will be shown in this case, to disable or enable this tab.
170      *
171      * $this->is_terminal indecates this two different types.
172      */
173     if($this->is_terminal){    
174       $smarty->assign("is_terminal","true");
175     }else{
176       $smarty->assign("is_terminal","false");
177     }
179     /* Do we need to flip is_account state? */
180     if (isset($_POST['modify_state'])){
181       $this->is_modified = true;
182       $this->is_account= !$this->is_account;
183     }
185     if($this->is_terminal){
186       if(empty($this->cn)){
187         if(!in_array("gotoPrinter",$this->objectclasses)){
188           $display= $this->show_header(_("Add printer extension"),
189               _("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);
190           $this->is_account= false;
191           return $display;
192         }else{
193           $smarty->assign("is_terminal","false");
194           $skip = true;
195         }
196       }
198       if(!isset($skip)){
199         if (($this->is_account)){
200           if($this->type=="station"){
201             $display= $this->show_header(_("Remove printer extension"),
202                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
203           }else{
204             $display= $this->show_header(_("Remove printer extension"),
205                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
206           }
207         }else{
208           if($this->type=="station"){
209             $display= $this->show_header(_("Add printer extension"),
210                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
211           }else{
212             $display= $this->show_header(_("Add printer extension"),
213                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
214           }  
215           return ($display);
216         }
217       }
218     }
221     /* Do we represent a valid printer? */
222     if (!$this->is_account && $this->parent == NULL){
223       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
224         _("This 'dn' has no printer features.")."</b>";
225       return($display);
226     }
228     /* Fill templating stuff */
229     $smarty->assign("bases", $this->config->idepartments);
230     $smarty->assign("base_select", $this->base);
232     /* Assign attributes */
233     foreach ($this->attributes as $attr){
234       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
235       $smarty->assign("$attr", $this->$attr);
236     }
238     if(isset($_POST['AddUser'])){
239       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
240     }
241     if(isset($_POST['AddGroup'])){
242       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
243     }
244     if(isset($_POST['AddAdminUser'])){
245       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
246     }
247     if(isset($_POST['AddAdminGroup'])){
248       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
249     }
251     if(isset($_POST['EditDriver'])){
252       if($this->PPDdialogToSave){
253         $this->dialog = $this->PPDdialogToSave;
254       }else{
255         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
256       }
257     }
259     if(isset($_POST['PrinterCancel'])){
260       unset($this->dialog);
261       $this->dialog= NULL;
262     }
263     
264     if(isset($_POST['RemoveDriver'])){
265       $this->gotoPrinterPPD = array();
266       $this->PPDdialogToSave = NULL;
267     }
269     if(isset($_POST['SavePPD'])){
270       $this->dialog->save_object();
271       if(count($this->dialog->check())){
272         foreach($this->dialog->check() as $msg){
273           print_red($msg);
274         }
275       }else{
276         $this->gotoPrinterPPD = array();
277         
278         $this->gotoPrinterPPD = $this->dialog->save();
279         $this->PPDdialogToSave = $this->dialog;
280         unset($this->dialog);
281         $this->dialog=NULL;
282       }
284     }
286     if(isset($_POST['ClosePPD'])){
287       unset($this->dialog);
288       $this->dialog=NULL;
289     }
291     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
292       if(isset($this->member['AddUser'][$_POST['UserMember']])){
293         unset($this->member['AddUser'][$_POST['UserMember']]);
294       }
295     }
297     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
298       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
299         unset($this->member['AddGroup'][$_POST['UserMember']]);
300       }
301     }
303     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
304       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
305         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
306       }
307     }
309     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
310       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
311         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
312       }
313     }
315     if(isset($_POST['PrinterSave'])){
316       $this->dialog->save_object();
317       if(count($this->dialog->check())){
318         foreach($this->dialog->check() as $msg){
319           print_red($msg);
320         }
321       }else{
322         $new = $this->dialog->save();
323         $data = $new;
324         unset($data['type']);
326         if(preg_match("/User/",$new['type'])){
327           $use = "uid";
328         }else{
329           $use = "cn";
330         }
332         foreach($data as $mem){
333           $this->member[$new['type']][$mem[$use][0]]=$mem;    
334         }
335         unset($this->dialog);
336         $this->dialog=NULL; 
337       }
338     }
340     if($this->dialog != NULL){
341       $this->dialog->save_object();
342       $display = $this->dialog->execute();
343       return $display;
344     }
346     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
347     $smarty->assign("staticAddress", "");
350     require_once ("class_ppdManager.inc");
351     $ppdManager= new ppdManager('/var/spool/ppd/');
352     if(!empty($this->gotoPrinterPPD)){
353       $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
354     }else{
355       $smarty->assign("driverInfo", _("not defined"));
356     }
358     $list=$this->generateList();
359     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
360     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
362     asort($userlist);
363     asort($adminlist);
364     if($this->is_terminalBased){
365       if($this->is_terminalBased == "Terminal"){
366         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
367       }else{
368         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
369       }
370       $smarty->assign("cnACL"    ," disabled ");
371     }else{
372       $smarty->assign("desc"    ,"");
373     }
374     $smarty->assign("UserMember"    ,$this->UserMember);
375     $smarty->assign("UserMembers"   ,$userlist);
376     $smarty->assign("UserMemberKeys",array_flip($userlist));
378     $smarty->assign("AdminMember"    ,$this->AdminMember);
379     $smarty->assign("AdminMembers"   ,$adminlist);
380     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
381     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
383     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
384   }
386   function remove_from_parent()
387   {
388     $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
389     $ldap= $this->config->get_ldap_link();
390     $ldap->rmdir($this->dn);
391     show_ldap_error($ldap->get_error());
392     $this->handle_post_events("remove");
394     /* Delete references to object groups */
395     $ldap->cd ($this->config->current['BASE']);
396     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
397     while ($ldap->fetch()){
398       $og= new ogroup($this->config, $ldap->getDN());
399       unset($og->member[$this->dn]);
400       $og->save ();
401     }
402   }
404   /* Save data to object */
405   function save_object()
406   {
407     plugin::save_object();
409     /* Save base, since this is no LDAP attribute */
410     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
411       $this->base= $_POST['base'];
412     }
413   }
415   /* Check supplied data */
416   function check()
417   {
418     $message= array();
419     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
421     /* must: cn */
422     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
423       $message[]= "The required field 'Printer name' is not set.";
424     }
426     $ui= get_userinfo();
427     $acl= get_permissions ($this->dn, $ui->subtreeACL);
428     $acl= get_module_permission($acl, "printer", $this->dn);
429     if (chkacl($acl, "create") != ""){
430       $message[]= _("You have no permissions to create a printer on this 'Base'.");
431     }
433     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
434       $ldap= $this->config->get_ldap_link();
435       $ldap->cd ($this->base);
436       $ldap->search ("(cn=".$this->cn.")", array("cn"));
437       if ($ldap->count() != 0){
438         while ($attrs= $ldap->fetch()){
439           if ($attrs['dn'] != $this->orig_dn){
440             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
441             break;
442           }
443         }
444       }
445     }
447     return ($message);
448   }
451   /* Save to LDAP */
452   function save()
453   {
454     if (!$this->is_account){
455       return;
456     }
457   
458     if($this->PPDdialogToSave){
459       $this->PPDdialogToSave->save_ppd();
460     }
461   
462     $dn= $this->dn;
463     plugin::save();
464     $ldap= $this->config->get_ldap_link();
465    
466     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
467       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
468     }
469     
470     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
471       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
472     }
474     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
475     */
476     if($this->is_terminal){
477       
478       /* reduce objectClasses to minimun */
479       $this->attrs['objectClass']= $this->objectclasses;
481       /* If a printer with the given dn exists, modify else create new one */
482       $ldap->cat($this->dn);
483       if($ldap->fetch()){
484         $this->orig_dn= $this->dn;
485       }else{
486         $this->orig_dn = "new";
487       }
488     }
490     /* Remove all empty values */
491     if ($this->orig_dn == 'new'){
492       $attrs= array();
493       foreach ($this->attrs as $key => $val){
494         if (is_array($val) && count($val) == 0){
495           continue;
496         }
497         $attrs[$key]= $val;
498       }
499       $this->attrs= $attrs;
500     }
503     /* Append printer user 
504      */
505     foreach($this->member['AddUser'] as $mem){
506       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
507     }
509     /* Append printer group 
510      */
511     foreach($this->member['AddGroup'] as $mem){
512       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
513     }
515     /* Append printer admin user 
516      */
517     foreach($this->member['AddAdminUser'] as $mem){
518       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
519     }
520     
521     /* Append printer admin group 
522      */
523     foreach($this->member['AddAdminGroup'] as $mem){
524       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
525     }
527     /* Write back to ldap */
528     $ldap= $this->config->get_ldap_link();
529     if ($this->orig_dn == 'new'){
530       $ldap->cd($this->config->current['BASE']);
531       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
532       $ldap->cd($this->dn);
533       $ldap->add($this->attrs);
534       $this->handle_post_events("add");
535     } else {
536       if ($this->orig_dn != $this->dn){
537         $this->move($this->orig_dn, $this->dn);
538       }
540       $ldap->cd($this->dn);
541       $ldap->modify($this->attrs);
542       $this->handle_post_events("modify");
543     }
544     show_ldap_error($ldap->get_error());
546     /* Optionally execute a command after we're done */
547     $this->postcreate();
548   }
550   function generateList(){
551     $a_return=array();
552     foreach($this->member as $type => $values){
553       $a_return[$type]=array();
554       foreach($values as $value){
555         if((preg_match("/Group/i",$type))){
556           if(!isset($value['description'])){
557             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
558           }else{
559             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
560           }
561         }else{
562           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
563         }
564       }
565     }
566     return($a_return);
567   }
572 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
573 ?>