Code

Several changes for incoming objects
[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 $l                = "";
18   var $description      = "";
19   var $labeledURI       = "";
20   var $gotoPrinterPPD   = "";
21   var $orig_dn          = "";
22   var $is_terminal      = false;
24   var $UserMember       ="";
25   var $UserMembers      =array();
26   var $UserMemberKeys   =array();
27   
28   var $AdminMember      ="";
29   var $AdminMembers     =array();
30   var $AdminMemberKeys  =array();
32   var $is_terminalBased = false; 
34   var $PPDdialogToSave  = NULL;
35  
36   var $member           =array();
37   var $strings          = "";
38   var $type             = "";
39   var $dialog           =NULL;
41   var $netConfigDNS;
43   /* attribute list for save action */
44   var $attributes     = array("cn", "description", "l", "labeledURI", "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();
51     
52     $this->netConfigDNS = new termDNS($this->config, $this->dn,$this->objectclasses);
55     /* Set base */
56     if ($this->dn == "new"){
57       $ui= get_userinfo();
58       $this->base= dn2base($ui->dn);
59       $this->cn= "";
60       $this->is_terminal = true;
61     } else {
62       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
63     }
65     if(isset($this->gotoPrinterPPD)){
66       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
67     }
69     /* In case of gotoWorkstation this tab is calles from workstation plugin
70      * in case of gotoTerminal it is called from a terminal tab
71      * else it is a standalone printer
72      */
73     if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
74       /* Fix for new Workstations */
75       if(preg_match("/ou=incoming/",$this->dn)){    
76         $this->is_terminal = "true";   
77         $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
78         $this->type = "station";
79       }else{
80         $this->is_terminal = "true";   
81         $this->dn   = preg_replace("/ou=workstations/","ou=printers",$this->dn);
82         $this->type = "station";
83       }
84     }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
85       if(preg_match("/ou=incoming/",$this->dn)){    
86         $this->is_terminal = "true";   
87         $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
88         $this->type = "terminal";
89       }else{
90         $this->type = "terminal";
91         $this->is_terminal = "true";
92         $this->dn   = preg_replace("/ou=terminals/","ou=printers",$this->dn);
93       }
94     }else{
95       /* Save dn for later references */
96       $this->orig_dn= $this->dn;
97     }
99     /* If it is no standalone printer 
100      */
101     if($this->is_terminal){
102       // Reload plugin with new dn... (ou=printers instead of ou=terminals)
103       plugin::plugin ($this->config, $this->dn);
104       $ldap->cat($this->dn);
105       if(count($ldap->fetch())>0){
106         $this->orig_dn= $this->dn;
107         $this->is_account=true;
108         $this->initially_was_account = true;
109       }else{
110         $this->orig_dn = "new";
111         $this->is_account=false;
112         $this->initially_was_account = false;
113       }
114     }
116     /* Prepare different member types 
117      */ 
118     foreach(array("AddUser"       =>"gotoUserPrinter",
119                   "AddGroup"      =>"gotoGroupPrinter",
120                   "AddAdminUser"  =>"gotoUserAdminPrinter",
121                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
123       /* $this->members contains all members */
124       $this->member[$type]=array();
126       if (isset($this->attrs[$attr]['count'])) {
127         unset($this->attrs[$attr]['count']);
128       }
130       if(isset($this->attrs[$attr])){
131         foreach($this->attrs[$attr] as $mem){
132           if(preg_match("/Group/",$type)){
133             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
134             $entry = $ldap->fetch();
135             if(isset($entry['description'])){
136               $this->member[$type][$entry['cn'][0]]=$entry;
137             }
138           }else{
139             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
140             $entry = $ldap->fetch();
141             if(isset($entry['uid'])){
142               $this->member[$type][$entry['uid'][0]]=$entry;
143             }
144           }
145         }
146       }
147     }
148   
149     $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
150     if($attrs = $ldap->fetch()){
151       if(in_array("gotoTerminal",$attrs['objectClass'])){
152         $this->is_terminalBased = "Terminal";
153       }else{
154         $this->is_terminalBased = "Workstation";
155       }
156     }
158     /* This attributes is sometimes written with Uri instead of URI
159        In RFC 2079 it is defined with URI ...
160      */
161     if(isset($this->attrs['labeledUri'])){
162       $this->labeledURI = $this->attrs['labeledUri'][0];
163     }
165     /* A printer was only saved if the printer belongs to a workstation. 
166      * If there was a new single printer created, it was never saved.
167      * -This allows new printers to be saved.
168      */
169     if($this->dn == "new"){
170       $this->is_account = true;
171     }
172   }
174   function execute()
175   {
176         /* Call parent execute */
177         plugin::execute();
179     if(isset($this->parent->by_name['workgeneric'])){
180 //      echo "workstation";
181       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
182         $display= $this->show_header(_("Add printer extension"),
183             _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
184         $this->is_account= false;
185         return $display;
186       }
187     }elseif(isset($this->parent->by_name['termgeneric'])){
188       if($this->parent->by_object['termgeneric']->cn == "default"){
189         $display= $this->show_header(_("Add printer extension"),
190             _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
191         $this->is_account= false;
192         return $display;
193       }
194     }elseif(isset($this->parent->by_name['printgeneric'])){
195 //      echo "printer";
196     }
199     $smarty= get_smarty();
200     $display="";
201     /* Template management.
202      * There are two ways to call this tab.
203      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
204      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
205      *    Toggle Account state will be shown in this case, to disable or enable this tab.
206      *
207      * $this->is_terminal indecates this two different types.
208      */
209     if($this->is_terminal){    
210       $smarty->assign("is_terminal","true");
211     }else{
212       $smarty->assign("is_terminal","false");
213     }
215     /* Do we need to flip is_account state? */
216     if (isset($_POST['modify_state'])){
217       $this->is_modified = true;
218       $this->is_account= !$this->is_account;
219     }
221     if($this->is_terminal){
222       if(empty($this->cn)){
223         if(!in_array("gotoPrinter",$this->objectclasses)){
224           $display= $this->show_header(_("Add printer extension"),
225               _("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);
226           $this->is_account= false;
227           return $display;
228         }else{
229           $smarty->assign("is_terminal","false");
230           $skip = true;
231         }
232       }
234       if(!isset($skip)){
235         if (($this->is_account)){
236           if($this->type=="station"){
237             $display= $this->show_header(_("Remove printer extension"),
238                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
239           }else{
240             $display= $this->show_header(_("Remove printer extension"),
241                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
242           }
243         }else{
244           if($this->type=="station"){
245             $display= $this->show_header(_("Add printer extension"),
246                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
247           }else{
248             $display= $this->show_header(_("Add printer extension"),
249                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
250           }  
251           return ($display);
252         }
253       }
254     }
257     /* Do we represent a valid printer? */
258     if (!$this->is_account && $this->parent == NULL){
259       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
260         _("This 'dn' has no printer features.")."</b>";
261       return($display);
262     }
264     /* Fill templating stuff */
265     $smarty->assign("bases", $this->config->idepartments);
266     $smarty->assign("base_select", $this->base);
268     /* Assign attributes */
269     foreach ($this->attributes as $attr){
270       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
271       $smarty->assign("$attr", $this->$attr);
272     }
274     if(isset($_POST['AddUser'])){
275       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
276     }
277     if(isset($_POST['AddGroup'])){
278       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
279     }
280     if(isset($_POST['AddAdminUser'])){
281       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
282     }
283     if(isset($_POST['AddAdminGroup'])){
284       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
285     }
287     if(isset($_POST['EditDriver'])){
288       if($this->PPDdialogToSave){
289         $this->dialog = $this->PPDdialogToSave;
290       }else{
291         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
292       }
293     }
295     if(isset($_POST['PrinterCancel'])){
296       unset($this->dialog);
297       $this->dialog= NULL;
298     }
299     
300     if(isset($_POST['RemoveDriver'])){
301       $this->gotoPrinterPPD = array();
302       $this->PPDdialogToSave = NULL;
303     }
305     if(isset($_POST['SavePPD'])){
306       $this->dialog->save_object();
307       if(count($this->dialog->check())){
308         foreach($this->dialog->check() as $msg){
309           print_red($msg);
310         }
311       }else{
312         $this->gotoPrinterPPD = array();
313         
314         $this->gotoPrinterPPD = $this->dialog->save();
315         $this->PPDdialogToSave = $this->dialog;
316         unset($this->dialog);
317         $this->dialog=NULL;
318       }
320     }
322     if(isset($_POST['ClosePPD'])){
323       unset($this->dialog);
324       $this->dialog=NULL;
325     }
327   
329     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
330       if(isset($this->member['AddUser'][$_POST['UserMember']])){
331         unset($this->member['AddUser'][$_POST['UserMember']]);
332       }
333     }
335     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
336       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
337         unset($this->member['AddGroup'][$_POST['UserMember']]);
338       }
339     }
341     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
342       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
343         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
344       }
345     }
347     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
348       if(isset($this->member['AddAdminGroup'][$_POST['AdminMember']])){
349         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
350       }
351     }
353     if(isset($_POST['PrinterSave'])){
354       $this->dialog->save_object();
355       if(count($this->dialog->check())){
356         foreach($this->dialog->check() as $msg){
357           print_red($msg);
358         }
359       }else{
360         $new = $this->dialog->save();
361         $data = $new;
362         unset($data['type']);
364         if(preg_match("/User/",$new['type'])){
365           $use = "uid";
366         }else{
367           $use = "cn";
368         }
370         foreach($data as $mem){
371           $this->member[$new['type']][$mem[$use][0]]=$mem;    
372         }
373         unset($this->dialog);
374         $this->dialog=NULL; 
375       }
376     }
378     if($this->dialog != NULL){
379       $this->dialog->save_object();
380       $display = $this->dialog->execute();
381       return $display;
382     }
384     require_once ("class_ppdManager.inc");
386     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
388       $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
389       if(!preg_match("/\/$/",$path)){
390         $path = $path."/";
391       }
393       $ppdManager= new ppdManager($path);
394       if(!empty($this->gotoPrinterPPD)){
395         if((!file_exists($path.$this->gotoPrinterPPD))){
396           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
397         }else{
398           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
399         }
400       }else{
401         $smarty->assign("driverInfo", _("not defined"));
402       }
403     }else{
404       $smarty->assign("driverInfo",_("can't get ppd informations."));
405     }
408     $list=$this->generateList();
409     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
410     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
412     asort($userlist);
413     asort($adminlist);
414     if($this->is_terminalBased){
415       if($this->is_terminalBased == "Terminal"){
416         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
417       }else{
418         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
419       }
420       $smarty->assign("cnACL"    ," disabled ");
421     }else{
422       $smarty->assign("desc"    ,"");
423     }
424     $smarty->assign("UserMember"    ,$this->UserMember);
425     $smarty->assign("UserMembers"   ,$userlist);
426     $smarty->assign("UserMemberKeys",array_flip($userlist));
428     $smarty->assign("AdminMember"    ,$this->AdminMember);
429     $smarty->assign("AdminMembers"   ,$adminlist);
430     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
431     $smarty->assign("netconfig", $this->netConfigDNS->execute());
433     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
434   }
436   function remove_from_parent()
437   {
438     if($this->initially_was_account){
439       $ldap= $this->config->get_ldap_link();
440       $ldap->cat($this->dn);
441       if(count($ldap->fetch()) ){
443         $this->netConfigDNS->remove_from_parent();
444         $ldap->rmdir($this->dn);
445         show_ldap_error($ldap->get_error());
446         $this->handle_post_events("remove");
448         /* Delete references to object groups */
449         $ldap->cd ($this->config->current['BASE']);
450         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
451         while ($ldap->fetch()){
452           $og= new ogroup($this->config, $ldap->getDN());
453           unset($og->member[$this->dn]);
454           $og->save ();
455         }
456       }
457     }
458   }
459   /* Save data to object */
460   function save_object()
461   {
462     plugin::save_object();
463     $this->netConfigDNS->save_object();
464     /* Save base, since this is no LDAP attribute */
465     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
466       $this->base= $_POST['base'];
467     }
468   }
470   /* Check supplied data */
471   function check()
472   {
473     $message= $this->netConfigDNS->check();
475     if(isset($this->parent->by_name['workgeneric'])){
476       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
477         return $message;
478       }
479     }elseif(isset($this->parent->by_name['termgeneric'])){
480       if($this->parent->by_object['termgeneric']->cn == "default"){
481         return $message;
482       }
483     }
485     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
487     /* must: cn */
488     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
489       $message[]= "The required field 'Printer name' is not set.";
490     }
492     $ui= get_userinfo();
493     $acl= get_permissions ($this->dn, $ui->subtreeACL);
494     $acl= get_module_permission($acl, "printer", $this->dn);
495     if (chkacl($acl, "create") != ""){
496       $message[]= _("You have no permissions to create a printer on this 'Base'.");
497     }
499     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
500       $ldap= $this->config->get_ldap_link();
501       $ldap->cd ($this->base);
502       $ldap->search ("(cn=".$this->cn.")", array("cn"));
503       if ($ldap->count() != 0){
504         while ($attrs= $ldap->fetch()){
505           if ($attrs['dn'] != $this->orig_dn){
506             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
507             break;
508           }
509         }
510       }
511     }
513     return ($message);
514   }
517   /* Save to LDAP */
518   function save()
519   {
520     if (!$this->is_account){
521       return;
522     }
523     if(isset($this->parent->by_name['workgeneric'])){
524       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
525         return;
526       }
527     }elseif(isset($this->parent->by_name['termgeneric'])){
528       if($this->parent->by_object['termgeneric']->cn == "default"){
529         return;
530       }
531     }
533     if($this->PPDdialogToSave){
534       $this->PPDdialogToSave->save_ppd();
535     }
536     $dn= $this->dn;
537     plugin::save();
538     $ldap= $this->config->get_ldap_link();
539    
540     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
541       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
542     }
543     
544     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
545       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
546     }
548     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
549     */
550     if($this->is_terminal){
551       
552       /* reduce objectClasses to minimun */
553       $this->attrs['objectClass']= $this->objectclasses;
555       /* If a printer with the given dn exists, modify else create new one */
556       $ldap->cat($this->dn);
557       if($ldap->fetch()){
558         $this->orig_dn= $this->dn;
559       }else{
560         $this->orig_dn = "new";
561       }
562     }
564     /* Remove all empty values */
565     if ($this->orig_dn == 'new'){
566       $attrs= array();
567       foreach ($this->attrs as $key => $val){
568         if (is_array($val) && count($val) == 0){
569           continue;
570         }
571         $attrs[$key]= $val;
572       }
573       $this->attrs= $attrs;
574     }
576     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
577       $method="https://";
578     }else{
579       $method="http://";
580     }
581   
582     if(!is_array($this->attrs['gotoPrinterPPD'])) {
583       $this->attrs['gotoPrinterPPD'] = $this->attrs['gotoPrinterPPD'];
584       $this->attrs['gotoPrinterPPD'] = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->attrs['gotoPrinterPPD']);
585     }
587     /* Append printer user 
588      */
589     foreach($this->member['AddUser'] as $mem){
590       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
591     }
593     /* Append printer group 
594      */
595     foreach($this->member['AddGroup'] as $mem){
596       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
597     }
599     /* Append printer admin user 
600      */
601     foreach($this->member['AddAdminUser'] as $mem){
602       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
603     }
604     
605     /* Append printer admin group 
606      */
607     foreach($this->member['AddAdminGroup'] as $mem){
608       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
609     }
611     /* Write back to ldap */
612     $ldap= $this->config->get_ldap_link();
613     if ($this->orig_dn == 'new'){
614       $ldap->cd($this->config->current['BASE']);
615       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
616       $ldap->cd($this->dn);
617       $ldap->add($this->attrs);
618       $this->handle_post_events("add");
619     } else {
620       if ($this->orig_dn != $this->dn){
621         $this->move($this->orig_dn, $this->dn);
622       }
624       $ldap->cd($this->dn);
625       $ldap->modify($this->attrs);
626       $this->handle_post_events("modify");
627     }
628     show_ldap_error($ldap->get_error());
630     $this->netConfigDNS->cn = $this->cn;
631     $this->netConfigDNS->save($this->dn);
633     /* Optionally execute a command after we're done */
634     $this->postcreate();
635   }
637   function generateList(){
638     $a_return=array();
639     foreach($this->member as $type => $values){
640       $a_return[$type]=array();
641       foreach($values as $value){
642         if((preg_match("/Group/i",$type))){
643           if(!isset($value['description'])){
644             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
645           }else{
646             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
647           }
648         }else{
649           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
650         }
651       }
652     }
653     return($a_return);
654   }
659 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
660 ?>