Code

12b5514738b45e4ceed52cc2eece9ac2b92644ff
[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       if (isset($this->attrs[$attr]['count'])) {
121         unset($this->attrs[$attr]['count']);
122       }
124       if(isset($this->attrs[$attr])){
125         foreach($this->attrs[$attr] as $mem){
126           if(preg_match("/Group/",$type)){
127             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
128             $entry = $ldap->fetch();
129             if(isset($entry['description'])){
130               $this->member[$type][$entry['cn'][0]]=$entry;
131             }
132           }else{
133             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
134             $entry = $ldap->fetch();
135             if(isset($entry['uid'])){
136               $this->member[$type][$entry['uid'][0]]=$entry;
137             }
138           }
139         }
140       }
141     }
142   
143     $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
144     if($attrs = $ldap->fetch()){
145       if(in_array("gotoTerminal",$attrs['objectClass'])){
146         $this->is_terminalBased = "Terminal";
147       }else{
148         $this->is_terminalBased = "Workstation";
149       }
150     }
152     /* A printer was only saved if the printer belongs to a workstation. 
153      * If there was a new single printer created, it was never saved.
154      * -This allows new printers to be saved.
155      */
156     if($this->dn == "new"){
157       $this->is_account = true;
158     }
159   }
161   function execute()
162   {
163         /* Call parent execute */
164         plugin::execute();
166     $smarty= get_smarty();
167     $display="";
168     /* Template management.
169      * There are two ways to call this tab.
170      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
171      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
172      *    Toggle Account state will be shown in this case, to disable or enable this tab.
173      *
174      * $this->is_terminal indecates this two different types.
175      */
176     if($this->is_terminal){    
177       $smarty->assign("is_terminal","true");
178     }else{
179       $smarty->assign("is_terminal","false");
180     }
182     /* Do we need to flip is_account state? */
183     if (isset($_POST['modify_state'])){
184       $this->is_modified = true;
185       $this->is_account= !$this->is_account;
186     }
188     if($this->is_terminal){
189       if(empty($this->cn)){
190         if(!in_array("gotoPrinter",$this->objectclasses)){
191           $display= $this->show_header(_("Add printer extension"),
192               _("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);
193           $this->is_account= false;
194           return $display;
195         }else{
196           $smarty->assign("is_terminal","false");
197           $skip = true;
198         }
199       }
201       if(!isset($skip)){
202         if (($this->is_account)){
203           if($this->type=="station"){
204             $display= $this->show_header(_("Remove printer extension"),
205                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
206           }else{
207             $display= $this->show_header(_("Remove printer extension"),
208                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
209           }
210         }else{
211           if($this->type=="station"){
212             $display= $this->show_header(_("Add printer extension"),
213                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
214           }else{
215             $display= $this->show_header(_("Add printer extension"),
216                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
217           }  
218           return ($display);
219         }
220       }
221     }
224     /* Do we represent a valid printer? */
225     if (!$this->is_account && $this->parent == NULL){
226       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
227         _("This 'dn' has no printer features.")."</b>";
228       return($display);
229     }
231     /* Fill templating stuff */
232     $smarty->assign("bases", $this->config->idepartments);
233     $smarty->assign("base_select", $this->base);
235     /* Assign attributes */
236     foreach ($this->attributes as $attr){
237       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
238       $smarty->assign("$attr", $this->$attr);
239     }
241     if(isset($_POST['AddUser'])){
242       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
243     }
244     if(isset($_POST['AddGroup'])){
245       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
246     }
247     if(isset($_POST['AddAdminUser'])){
248       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
249     }
250     if(isset($_POST['AddAdminGroup'])){
251       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
252     }
254     if(isset($_POST['EditDriver'])){
255       if($this->PPDdialogToSave){
256         $this->dialog = $this->PPDdialogToSave;
257       }else{
258         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
259       }
260     }
262     if(isset($_POST['PrinterCancel'])){
263       unset($this->dialog);
264       $this->dialog= NULL;
265     }
266     
267     if(isset($_POST['RemoveDriver'])){
268       $this->gotoPrinterPPD = array();
269       $this->PPDdialogToSave = NULL;
270     }
272     if(isset($_POST['SavePPD'])){
273       $this->dialog->save_object();
274       if(count($this->dialog->check())){
275         foreach($this->dialog->check() as $msg){
276           print_red($msg);
277         }
278       }else{
279         $this->gotoPrinterPPD = array();
280         
281         $this->gotoPrinterPPD = $this->dialog->save();
282         $this->PPDdialogToSave = $this->dialog;
283         unset($this->dialog);
284         $this->dialog=NULL;
285       }
287     }
289     if(isset($_POST['ClosePPD'])){
290       unset($this->dialog);
291       $this->dialog=NULL;
292     }
294     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
295       if(isset($this->member['AddUser'][$_POST['UserMember']])){
296         unset($this->member['AddUser'][$_POST['UserMember']]);
297       }
298     }
300     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
301       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
302         unset($this->member['AddGroup'][$_POST['UserMember']]);
303       }
304     }
306     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
307       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
308         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
309       }
310     }
312     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
313       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
314         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
315       }
316     }
318     if(isset($_POST['PrinterSave'])){
319       $this->dialog->save_object();
320       if(count($this->dialog->check())){
321         foreach($this->dialog->check() as $msg){
322           print_red($msg);
323         }
324       }else{
325         $new = $this->dialog->save();
326         $data = $new;
327         unset($data['type']);
329         if(preg_match("/User/",$new['type'])){
330           $use = "uid";
331         }else{
332           $use = "cn";
333         }
335         foreach($data as $mem){
336           $this->member[$new['type']][$mem[$use][0]]=$mem;    
337         }
338         unset($this->dialog);
339         $this->dialog=NULL; 
340       }
341     }
343     if($this->dialog != NULL){
344       $this->dialog->save_object();
345       $display = $this->dialog->execute();
346       return $display;
347     }
349     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
350     $smarty->assign("staticAddress", "");
353     require_once ("class_ppdManager.inc");
354     $ppdManager= new ppdManager('/var/spool/ppd/');
355     if(!empty($this->gotoPrinterPPD)){
356       if((!file_exists("/var/spool/ppd/".$this->gotoPrinterPPD))){
357         $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
358       }else{
359         $smarty->assign("driverInfo", $ppdManager->loadDescription("/var/spool/ppd/".$this->gotoPrinterPPD));
360       }
361     }else{
362       $smarty->assign("driverInfo", _("not defined"));
363     }
365     $list=$this->generateList();
366     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
367     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
369     asort($userlist);
370     asort($adminlist);
371     if($this->is_terminalBased){
372       if($this->is_terminalBased == "Terminal"){
373         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
374       }else{
375         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
376       }
377       $smarty->assign("cnACL"    ," disabled ");
378     }else{
379       $smarty->assign("desc"    ,"");
380     }
381     $smarty->assign("UserMember"    ,$this->UserMember);
382     $smarty->assign("UserMembers"   ,$userlist);
383     $smarty->assign("UserMemberKeys",array_flip($userlist));
385     $smarty->assign("AdminMember"    ,$this->AdminMember);
386     $smarty->assign("AdminMembers"   ,$adminlist);
387     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
388     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
390     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
391   }
393   function remove_from_parent()
394   {
395     $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
396     $ldap= $this->config->get_ldap_link();
397     $ldap->rmdir($this->dn);
398     show_ldap_error($ldap->get_error());
399     $this->handle_post_events("remove");
401     /* Delete references to object groups */
402     $ldap->cd ($this->config->current['BASE']);
403     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
404     while ($ldap->fetch()){
405       $og= new ogroup($this->config, $ldap->getDN());
406       unset($og->member[$this->dn]);
407       $og->save ();
408     }
409   }
411   /* Save data to object */
412   function save_object()
413   {
414     plugin::save_object();
416     /* Save base, since this is no LDAP attribute */
417     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
418       $this->base= $_POST['base'];
419     }
420   }
422   /* Check supplied data */
423   function check()
424   {
425     $message= array();
426     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
428     /* must: cn */
429     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
430       $message[]= "The required field 'Printer name' is not set.";
431     }
433     $ui= get_userinfo();
434     $acl= get_permissions ($this->dn, $ui->subtreeACL);
435     $acl= get_module_permission($acl, "printer", $this->dn);
436     if (chkacl($acl, "create") != ""){
437       $message[]= _("You have no permissions to create a printer on this 'Base'.");
438     }
440     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
441       $ldap= $this->config->get_ldap_link();
442       $ldap->cd ($this->base);
443       $ldap->search ("(cn=".$this->cn.")", array("cn"));
444       if ($ldap->count() != 0){
445         while ($attrs= $ldap->fetch()){
446           if ($attrs['dn'] != $this->orig_dn){
447             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
448             break;
449           }
450         }
451       }
452     }
454     return ($message);
455   }
458   /* Save to LDAP */
459   function save()
460   {
461     if (!$this->is_account){
462       return;
463     }
464   
465     if($this->PPDdialogToSave){
466       $this->PPDdialogToSave->save_ppd();
467     }
468     $dn= $this->dn;
469     plugin::save();
470     $ldap= $this->config->get_ldap_link();
471    
472     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
473       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
474     }
475     
476     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
477       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
478     }
480     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
481     */
482     if($this->is_terminal){
483       
484       /* reduce objectClasses to minimun */
485       $this->attrs['objectClass']= $this->objectclasses;
487       /* If a printer with the given dn exists, modify else create new one */
488       $ldap->cat($this->dn);
489       if($ldap->fetch()){
490         $this->orig_dn= $this->dn;
491       }else{
492         $this->orig_dn = "new";
493       }
494     }
496     /* Remove all empty values */
497     if ($this->orig_dn == 'new'){
498       $attrs= array();
499       foreach ($this->attrs as $key => $val){
500         if (is_array($val) && count($val) == 0){
501           continue;
502         }
503         $attrs[$key]= $val;
504       }
505       $this->attrs= $attrs;
506     }
509     /* Append printer user 
510      */
511     foreach($this->member['AddUser'] as $mem){
512       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
513     }
515     /* Append printer group 
516      */
517     foreach($this->member['AddGroup'] as $mem){
518       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
519     }
521     /* Append printer admin user 
522      */
523     foreach($this->member['AddAdminUser'] as $mem){
524       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
525     }
526     
527     /* Append printer admin group 
528      */
529     foreach($this->member['AddAdminGroup'] as $mem){
530       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
531     }
533     /* Write back to ldap */
534     $ldap= $this->config->get_ldap_link();
535     if ($this->orig_dn == 'new'){
536       $ldap->cd($this->config->current['BASE']);
537       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
538       $ldap->cd($this->dn);
539       $ldap->add($this->attrs);
540       $this->handle_post_events("add");
541     } else {
542       if ($this->orig_dn != $this->dn){
543         $this->move($this->orig_dn, $this->dn);
544       }
546       $ldap->cd($this->dn);
547       $ldap->modify($this->attrs);
548       $this->handle_post_events("modify");
549     }
550     show_ldap_error($ldap->get_error());
552     /* Optionally execute a command after we're done */
553     $this->postcreate();
554   }
556   function generateList(){
557     $a_return=array();
558     foreach($this->member as $type => $values){
559       $a_return[$type]=array();
560       foreach($values as $value){
561         if((preg_match("/Group/i",$type))){
562           if(!isset($value['description'])){
563             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
564           }else{
565             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
566           }
567         }else{
568           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
569         }
570       }
571     }
572     return($a_return);
573   }
578 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
579 ?>