Code

0cf8e3872808342bfdf21011d3e8f8b691363b47
[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     $smarty= get_smarty();
162     $display="";
163     /* Template management.
164      * There are two ways to call this tab.
165      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
166      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
167      *    Toggle Account state will be shown in this case, to disable or enable this tab.
168      *
169      * $this->is_terminal indecates this two different types.
170      */
171     if($this->is_terminal){    
172       $smarty->assign("is_terminal","true");
173     }else{
174       $smarty->assign("is_terminal","false");
175     }
177     /* Do we need to flip is_account state? */
178     if (isset($_POST['modify_state'])){
179       $this->is_modified = true;
180       $this->is_account= !$this->is_account;
181     }
183     if($this->is_terminal){
184       if(empty($this->cn)){
185         if(!in_array("gotoPrinter",$this->objectclasses)){
186           $display= $this->show_header(_("Add printer extension"),
187               _("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);
188           $this->is_account= false;
189           return $display;
190         }else{
191           $smarty->assign("is_terminal","false");
192           $skip = true;
193         }
194       }
196       if(!isset($skip)){
197         if (($this->is_account)){
198           if($this->type=="station"){
199             $display= $this->show_header(_("Remove printer extension"),
200                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
201           }else{
202             $display= $this->show_header(_("Remove printer extension"),
203                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
204           }
205         }else{
206           if($this->type=="station"){
207             $display= $this->show_header(_("Add printer extension"),
208                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
209           }else{
210             $display= $this->show_header(_("Add printer extension"),
211                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
212           }  
213           return ($display);
214         }
215       }
216     }
219     /* Do we represent a valid printer? */
220     if (!$this->is_account && $this->parent == NULL){
221       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
222         _("This 'dn' has no printer features.")."</b>";
223       return($display);
224     }
226     /* Fill templating stuff */
227     $smarty->assign("bases", $this->config->idepartments);
228     $smarty->assign("base_select", $this->base);
230     /* Assign attributes */
231     foreach ($this->attributes as $attr){
232       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
233       $smarty->assign("$attr", $this->$attr);
234     }
236     if(isset($_POST['AddUser'])){
237       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
238     }
239     if(isset($_POST['AddGroup'])){
240       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
241     }
242     if(isset($_POST['AddAdminUser'])){
243       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
244     }
245     if(isset($_POST['AddAdminGroup'])){
246       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
247     }
249     if(isset($_POST['EditDriver'])){
250       if($this->PPDdialogToSave){
251         $this->dialog = $this->PPDdialogToSave;
252       }else{
253         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
254       }
255     }
257     if(isset($_POST['PrinterCancel'])){
258       unset($this->dialog);
259       $this->dialog= NULL;
260     }
261     
262     if(isset($_POST['RemoveDriver'])){
263       $this->gotoPrinterPPD = array();
264       $this->PPDdialogToSave = NULL;
265     }
267     if(isset($_POST['SavePPD'])){
268       $this->dialog->save_object();
269       if(count($this->dialog->check())){
270         foreach($this->dialog->check() as $msg){
271           print_red($msg);
272         }
273       }else{
274         $this->gotoPrinterPPD = array();
275         
276         $this->gotoPrinterPPD = $this->dialog->save();
277         $this->PPDdialogToSave = $this->dialog;
278         unset($this->dialog);
279         $this->dialog=NULL;
280       }
282     }
284     if(isset($_POST['ClosePPD'])){
285       unset($this->dialog);
286       $this->dialog=NULL;
287     }
289     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
290       if(isset($this->member['AddUser'][$_POST['UserMember']])){
291         unset($this->member['AddUser'][$_POST['UserMember']]);
292       }
293     }
295     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
296       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
297         unset($this->member['AddGroup'][$_POST['UserMember']]);
298       }
299     }
301     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
302       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
303         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
304       }
305     }
307     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
308       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
309         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
310       }
311     }
313     if(isset($_POST['PrinterSave'])){
314       $this->dialog->save_object();
315       if(count($this->dialog->check())){
316         foreach($this->dialog->check() as $msg){
317           print_red($msg);
318         }
319       }else{
320         $new = $this->dialog->save();
321         $data = $new;
322         unset($data['type']);
324         if(preg_match("/User/",$new['type'])){
325           $use = "uid";
326         }else{
327           $use = "cn";
328         }
330         foreach($data as $mem){
331           $this->member[$new['type']][$mem[$use][0]]=$mem;    
332         }
333         unset($this->dialog);
334         $this->dialog=NULL; 
335       }
336     }
338     if($this->dialog != NULL){
339       $this->dialog->save_object();
340       $display = $this->dialog->execute();
341       return $display;
342     }
344     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
345     $smarty->assign("staticAddress", "");
348     require_once ("class_ppdManager.inc");
349     $ppdManager= new ppdManager('/var/spool/ppd/');
350     if(!empty($this->gotoPrinterPPD)){
351       $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
352     }else{
353       $smarty->assign("driverInfo", _("not defined"));
354     }
356     $list=$this->generateList();
357     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
358     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
360     asort($userlist);
361     asort($adminlist);
362     if($this->is_terminalBased){
363       if($this->is_terminalBased == "Terminal"){
364         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
365       }else{
366         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
367       }
368       $smarty->assign("cnACL"    ," disabled ");
369     }else{
370       $smarty->assign("desc"    ,"");
371     }
372     $smarty->assign("UserMember"    ,$this->UserMember);
373     $smarty->assign("UserMembers"   ,$userlist);
374     $smarty->assign("UserMemberKeys",array_flip($userlist));
376     $smarty->assign("AdminMember"    ,$this->AdminMember);
377     $smarty->assign("AdminMembers"   ,$adminlist);
378     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
379     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
381     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
382   }
384   function remove_from_parent()
385   {
386     $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
387     $ldap= $this->config->get_ldap_link();
388     $ldap->rmdir($this->dn);
389     show_ldap_error($ldap->get_error());
390     $this->handle_post_events("remove");
392     /* Delete references to object groups */
393     $ldap->cd ($this->config->current['BASE']);
394     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
395     while ($ldap->fetch()){
396       $og= new ogroup($this->config, $ldap->getDN());
397       unset($og->member[$this->dn]);
398       $og->save ();
399     }
400   }
402   /* Save data to object */
403   function save_object()
404   {
405     plugin::save_object();
407     /* Save base, since this is no LDAP attribute */
408     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
409       $this->base= $_POST['base'];
410     }
411   }
413   /* Check supplied data */
414   function check()
415   {
416     $message= array();
417     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
419     /* must: cn */
420     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
421       $message[]= "The required field 'Printer name' is not set.";
422     }
424     $ui= get_userinfo();
425     $acl= get_permissions ($this->dn, $ui->subtreeACL);
426     $acl= get_module_permission($acl, "printer", $this->dn);
427     if (chkacl($acl, "create") != ""){
428       $message[]= _("You have no permissions to create a printer on this 'Base'.");
429     }
431     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
432       $ldap= $this->config->get_ldap_link();
433       $ldap->cd ($this->base);
434       $ldap->search ("(cn=".$this->cn.")", array("cn"));
435       if ($ldap->count() != 0){
436         while ($attrs= $ldap->fetch()){
437           if ($attrs['dn'] != $this->orig_dn){
438             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
439             break;
440           }
441         }
442       }
443     }
445     return ($message);
446   }
449   /* Save to LDAP */
450   function save()
451   {
452     if (!$this->is_account){
453       return;
454     }
455   
456     if($this->PPDdialogToSave){
457       $this->PPDdialogToSave->save_ppd();
458     }
459   
460     $dn= $this->dn;
461     plugin::save();
462     $ldap= $this->config->get_ldap_link();
463    
464     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
465       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
466     }
467     
468     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
469       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
470     }
472     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
473     */
474     if($this->is_terminal){
475       
476       /* reduce objectClasses to minimun */
477       $this->attrs['objectClass']= $this->objectclasses;
479       /* If a printer with the given dn exists, modify else create new one */
480       $ldap->cat($this->dn);
481       if($ldap->fetch()){
482         $this->orig_dn= $this->dn;
483       }else{
484         $this->orig_dn = "new";
485       }
486     }
488     /* Remove all empty values */
489     if ($this->orig_dn == 'new'){
490       $attrs= array();
491       foreach ($this->attrs as $key => $val){
492         if (is_array($val) && count($val) == 0){
493           continue;
494         }
495         $attrs[$key]= $val;
496       }
497       $this->attrs= $attrs;
498     }
501     /* Append printer user 
502      */
503     foreach($this->member['AddUser'] as $mem){
504       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
505     }
507     /* Append printer group 
508      */
509     foreach($this->member['AddGroup'] as $mem){
510       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
511     }
513     /* Append printer admin user 
514      */
515     foreach($this->member['AddAdminUser'] as $mem){
516       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
517     }
518     
519     /* Append printer admin group 
520      */
521     foreach($this->member['AddAdminGroup'] as $mem){
522       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
523     }
525     /* Write back to ldap */
526     $ldap= $this->config->get_ldap_link();
527     if ($this->orig_dn == 'new'){
528       $ldap->cd($this->config->current['BASE']);
529       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
530       $ldap->cd($this->dn);
531       $ldap->add($this->attrs);
532       $this->handle_post_events("add");
533     } else {
534       if ($this->orig_dn != $this->dn){
535         $this->move($this->orig_dn, $this->dn);
536       }
538       $ldap->cd($this->dn);
539       $ldap->modify($this->attrs);
540       $this->handle_post_events("modify");
541     }
542     show_ldap_error($ldap->get_error());
544     /* Optionally execute a command after we're done */
545     $this->postcreate();
546   }
548   function generateList(){
549     $a_return=array();
550     foreach($this->member as $type => $values){
551       $a_return[$type]=array();
552       foreach($values as $value){
553         if((preg_match("/Group/i",$type))){
554           if(!isset($value['description'])){
555             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
556           }else{
557             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
558           }
559         }else{
560           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
561         }
562       }
563     }
564     return($a_return);
565   }
570 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
571 ?>