Code

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