Code

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