Code

Exported ppd_path to gosa.conf.
[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");
355     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
356       $ppdManager= new ppdManager($_SESSION['config']->data['MAIN']['PPD_PATH']);
357       if(!empty($this->gotoPrinterPPD)){
358         if((!file_exists($_SESSION['config']->data['MAIN']['PPD_PATH'].$this->gotoPrinterPPD))){
359           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
360         }else{
361           $smarty->assign("driverInfo", $ppdManager->loadDescription($_SESSION['config']->data['MAIN']['PPD_PATH'].$this->gotoPrinterPPD));
362         }
363       }else{
364         $smarty->assign("driverInfo", _("not defined"));
365       }
366     }else{
367       $smarty->assign("driverInfo",_("can't get ppd informations."));
368     }
371     $list=$this->generateList();
372     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
373     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
375     asort($userlist);
376     asort($adminlist);
377     if($this->is_terminalBased){
378       if($this->is_terminalBased == "Terminal"){
379         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
380       }else{
381         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
382       }
383       $smarty->assign("cnACL"    ," disabled ");
384     }else{
385       $smarty->assign("desc"    ,"");
386     }
387     $smarty->assign("UserMember"    ,$this->UserMember);
388     $smarty->assign("UserMembers"   ,$userlist);
389     $smarty->assign("UserMemberKeys",array_flip($userlist));
391     $smarty->assign("AdminMember"    ,$this->AdminMember);
392     $smarty->assign("AdminMembers"   ,$adminlist);
393     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
394     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
396     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
397   }
399   function remove_from_parent()
400   {
401     $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
402     $ldap= $this->config->get_ldap_link();
403     $ldap->rmdir($this->dn);
404     show_ldap_error($ldap->get_error());
405     $this->handle_post_events("remove");
407     /* Delete references to object groups */
408     $ldap->cd ($this->config->current['BASE']);
409     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
410     while ($ldap->fetch()){
411       $og= new ogroup($this->config, $ldap->getDN());
412       unset($og->member[$this->dn]);
413       $og->save ();
414     }
415   }
417   /* Save data to object */
418   function save_object()
419   {
420     plugin::save_object();
422     /* Save base, since this is no LDAP attribute */
423     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
424       $this->base= $_POST['base'];
425     }
426   }
428   /* Check supplied data */
429   function check()
430   {
431     $message= array();
432     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
434     /* must: cn */
435     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
436       $message[]= "The required field 'Printer name' is not set.";
437     }
439     $ui= get_userinfo();
440     $acl= get_permissions ($this->dn, $ui->subtreeACL);
441     $acl= get_module_permission($acl, "printer", $this->dn);
442     if (chkacl($acl, "create") != ""){
443       $message[]= _("You have no permissions to create a printer on this 'Base'.");
444     }
446     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
447       $ldap= $this->config->get_ldap_link();
448       $ldap->cd ($this->base);
449       $ldap->search ("(cn=".$this->cn.")", array("cn"));
450       if ($ldap->count() != 0){
451         while ($attrs= $ldap->fetch()){
452           if ($attrs['dn'] != $this->orig_dn){
453             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
454             break;
455           }
456         }
457       }
458     }
460     return ($message);
461   }
464   /* Save to LDAP */
465   function save()
466   {
467     if (!$this->is_account){
468       return;
469     }
470   
471     if($this->PPDdialogToSave){
472       $this->PPDdialogToSave->save_ppd();
473     }
474     $dn= $this->dn;
475     plugin::save();
476     $ldap= $this->config->get_ldap_link();
477    
478     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
479       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
480     }
481     
482     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
483       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
484     }
486     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
487     */
488     if($this->is_terminal){
489       
490       /* reduce objectClasses to minimun */
491       $this->attrs['objectClass']= $this->objectclasses;
493       /* If a printer with the given dn exists, modify else create new one */
494       $ldap->cat($this->dn);
495       if($ldap->fetch()){
496         $this->orig_dn= $this->dn;
497       }else{
498         $this->orig_dn = "new";
499       }
500     }
502     /* Remove all empty values */
503     if ($this->orig_dn == 'new'){
504       $attrs= array();
505       foreach ($this->attrs as $key => $val){
506         if (is_array($val) && count($val) == 0){
507           continue;
508         }
509         $attrs[$key]= $val;
510       }
511       $this->attrs= $attrs;
512     }
515     /* Append printer user 
516      */
517     foreach($this->member['AddUser'] as $mem){
518       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
519     }
521     /* Append printer group 
522      */
523     foreach($this->member['AddGroup'] as $mem){
524       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
525     }
527     /* Append printer admin user 
528      */
529     foreach($this->member['AddAdminUser'] as $mem){
530       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
531     }
532     
533     /* Append printer admin group 
534      */
535     foreach($this->member['AddAdminGroup'] as $mem){
536       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
537     }
539     /* Write back to ldap */
540     $ldap= $this->config->get_ldap_link();
541     if ($this->orig_dn == 'new'){
542       $ldap->cd($this->config->current['BASE']);
543       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
544       $ldap->cd($this->dn);
545       $ldap->add($this->attrs);
546       $this->handle_post_events("add");
547     } else {
548       if ($this->orig_dn != $this->dn){
549         $this->move($this->orig_dn, $this->dn);
550       }
552       $ldap->cd($this->dn);
553       $ldap->modify($this->attrs);
554       $this->handle_post_events("modify");
555     }
556     show_ldap_error($ldap->get_error());
558     /* Optionally execute a command after we're done */
559     $this->postcreate();
560   }
562   function generateList(){
563     $a_return=array();
564     foreach($this->member as $type => $values){
565       $a_return[$type]=array();
566       foreach($values as $value){
567         if((preg_match("/Group/i",$type))){
568           if(!isset($value['description'])){
569             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
570           }else{
571             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
572           }
573         }else{
574           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
575         }
576       }
577     }
578     return($a_return);
579   }
584 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
585 ?>