Code

Added copy & paste for ogroups
[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","gotoUserPrinter");
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     /* A printer was only saved if the printer belongs to a workstation. 
159      * If there was a new single printer created, it was never saved.
160      * -This allows new printers to be saved.
161      */
162     if(($this->dn == "new")&&(!$this->is_terminal)){
163       $this->is_account = true;
164     }
165   }
167   function execute()
168   {
169         /* Call parent execute */
170         plugin::execute();
172     if(isset($this->parent->by_name['workgeneric'])){
173 //      echo "workstation";
174       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
175         $display= $this->show_header(_("Add printer extension"),
176             _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
177         $this->is_account= false;
178         return $display;
179       }
180     }elseif(isset($this->parent->by_name['termgeneric'])){
181       if($this->parent->by_object['termgeneric']->cn == "default"){
182         $display= $this->show_header(_("Add printer extension"),
183             _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
184         $this->is_account= false;
185         return $display;
186       }
187     }elseif(isset($this->parent->by_name['printgeneric'])){
188 //      echo "printer";
189     }
192     $smarty= get_smarty();
193     $display="";
194     /* Template management.
195      * There are two ways to call this tab.
196      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
197      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
198      *    Toggle Account state will be shown in this case, to disable or enable this tab.
199      *
200      * $this->is_terminal indecates this two different types.
201      */
202     if($this->is_terminal){    
203       $smarty->assign("is_terminal","true");
204     }else{
205       $smarty->assign("is_terminal","false");
206     }
208     /* Do we need to flip is_account state? */
209     if (isset($_POST['modify_state'])){
210       $this->is_modified = true;
211       $this->is_account= !$this->is_account;
212     }
214     if($this->is_terminal){
215       if(empty($this->cn)){
216         if(!in_array("gotoPrinter",$this->objectclasses)){
217           $display= $this->show_header(_("Add printer extension"),
218               _("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);
219           $this->is_account= false;
220           return $display;
221         }else{
222           $smarty->assign("is_terminal","false");
223           $skip = true;
224         }
225       }
227       if(!isset($skip)){
228         if (($this->is_account)){
229           if($this->type=="station"){
230             $display= $this->show_header(_("Remove printer extension"),
231                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
232           }else{
233             $display= $this->show_header(_("Remove printer extension"),
234                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
235           }
236         }else{
237           if($this->type=="station"){
238             $display= $this->show_header(_("Add printer extension"),
239                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
240           }else{
241             $display= $this->show_header(_("Add printer extension"),
242                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
243           }  
244           return ($display);
245         }
246       }
247     }
249     /* Base select dialog */
250     $once = true;
251     foreach($_POST as $name => $value){
252       if(preg_match("/^chooseBase/",$name) && $once){
253         $once = false;
254         $this->dialog = new baseSelectDialog($this->config);
255         $this->dialog->setCurrentBase($this->base);
256       }
257     }
259     /* Dialog handling */
260     if(is_object($this->dialog)){
261       /* Must be called before save_object */
262       $this->dialog->save_object();
264       if($this->dialog->isClosed()){
265         $this->dialog = false;
266       }elseif($this->dialog->isSelected()){
267         $this->base = $this->dialog->isSelected();
268         $this->dialog= false;
269       }else{
270         return($this->dialog->execute());
271       }
272     }
274     /* Do we represent a valid printer? */
275     if (!$this->is_account && $this->parent == NULL){
276       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
277         _("This 'dn' has no printer features.")."</b>";
278       return($display);
279     }
281     /* Fill templating stuff */
282     $smarty->assign("bases", $this->config->idepartments);
283     $smarty->assign("base_select", $this->base);
285     /* Assign attributes */
286     foreach ($this->attributes as $attr){
287       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
288       $smarty->assign("$attr", $this->$attr);
289     }
291     if(isset($_POST['AddUser'])){
292       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
293     }
294     if(isset($_POST['AddGroup'])){
295       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
296     }
297     if(isset($_POST['AddAdminUser'])){
298       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
299     }
300     if(isset($_POST['AddAdminGroup'])){
301       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
302     }
304     if(isset($_POST['EditDriver'])){
305       if($this->PPDdialogToSave){
306         $this->dialog = $this->PPDdialogToSave;
307       }else{
308         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
309       }
310     }
312     if(isset($_POST['PrinterCancel'])){
313       unset($this->dialog);
314       $this->dialog= NULL;
315     }
316     
317     if(isset($_POST['RemoveDriver'])){
318       $this->gotoPrinterPPD = array();
319       $this->PPDdialogToSave = NULL;
320     }
322     if(isset($_POST['SavePPD'])){
323       $this->dialog->save_object();
324       if(count($this->dialog->check())){
325         foreach($this->dialog->check() as $msg){
326           print_red($msg);
327         }
328       }else{
329         $this->gotoPrinterPPD = array();
330         
331         $this->gotoPrinterPPD = $this->dialog->save();
332         $this->PPDdialogToSave = $this->dialog;
333         unset($this->dialog);
334         $this->dialog=NULL;
335       }
337     }
339     if(isset($_POST['ClosePPD'])){
340       unset($this->dialog);
341       $this->dialog=NULL;
342     }
344   
346     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
347       if(isset($this->member['AddUser'][$_POST['UserMember']])){
348         unset($this->member['AddUser'][$_POST['UserMember']]);
349       }
350     }
352     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
353       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
354         unset($this->member['AddGroup'][$_POST['UserMember']]);
355       }
356     }
358     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
359       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
360         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
361       }
362     }
364     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
365       if(isset($this->member['AddAdminGroup'][$_POST['AdminMember']])){
366         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
367       }
368     }
370     if(isset($_POST['PrinterSave'])){
371       $this->dialog->save_object();
372       if(count($this->dialog->check())){
373         foreach($this->dialog->check() as $msg){
374           print_red($msg);
375         }
376       }else{
377         $new = $this->dialog->save();
378         $data = $new;
379         unset($data['type']);
381         if(preg_match("/User/",$new['type'])){
382           $use = "uid";
383         }else{
384           $use = "cn";
385         }
387         foreach($data as $mem){
388           $this->member[$new['type']][$mem[$use][0]]=$mem;    
389         }
390         unset($this->dialog);
391         $this->dialog=NULL; 
392       }
393     }
395     if($this->dialog != NULL){
396       $this->dialog->save_object();
397       $display = $this->dialog->execute();
398       return $display;
399     }
401     require_once ("class_ppdManager.inc");
403     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
405       $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
406       if(!preg_match("/\/$/",$path)){
407         $path = $path."/";
408       }
410       $ppdManager= new ppdManager($path);
411       if(!empty($this->gotoPrinterPPD)){
412         if((!file_exists($path.$this->gotoPrinterPPD))){
413           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
414         }else{
415           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
416         }
417       }else{
418         $smarty->assign("driverInfo", _("not defined"));
419       }
420     }else{
421       $smarty->assign("driverInfo",_("can't get ppd informations."));
422     }
425     $list=$this->generateList();
426     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
427     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
429     asort($userlist);
430     asort($adminlist);
431     if($this->is_terminalBased){
432       if($this->is_terminalBased == "Terminal"){
433         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
434       }else{
435         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
436       }
437       $smarty->assign("cnACL"    ," disabled ");
438     }else{
439       $smarty->assign("desc"    ,"");
440     }
441     $smarty->assign("UserMember"    ,$this->UserMember);
442     $smarty->assign("UserMembers"   ,$userlist);
443     $smarty->assign("UserMemberKeys",array_flip($userlist));
445     $smarty->assign("AdminMember"    ,$this->AdminMember);
446     $smarty->assign("AdminMembers"   ,$adminlist);
447     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
448     $smarty->assign("netconfig", $this->netConfigDNS->execute());
450     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
451   }
453   function remove_from_parent()
454   {
455     if($this->initially_was_account){
456       $ldap= $this->config->get_ldap_link();
457       $ldap->cat($this->dn);
458       if(count($ldap->fetch()) ){
460         $this->netConfigDNS->remove_from_parent();
461         $ldap->rmdir($this->dn);
462         show_ldap_error($ldap->get_error());
463         $this->handle_post_events("remove");
465         /* Delete references to object groups */
466         $ldap->cd ($this->config->current['BASE']);
467         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
468         while ($ldap->fetch()){
469           $og= new ogroup($this->config, $ldap->getDN());
470           unset($og->member[$this->dn]);
471           $og->save ();
472         }
473       }
474     }
475   }
476   /* Save data to object */
477   function save_object()
478   {
479     plugin::save_object();
480     $this->netConfigDNS->save_object();
481     /* Save base, since this is no LDAP attribute */
482     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
483       $this->base= $_POST['base'];
484     }
485   }
487   /* Check supplied data */
488   function check()
489   {
490     $message= $this->netConfigDNS->check();
492     if(isset($this->parent->by_name['workgeneric'])){
493       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
494         return $message;
495       }
496     }elseif(isset($this->parent->by_name['termgeneric'])){
497       if($this->parent->by_object['termgeneric']->cn == "default"){
498         return $message;
499       }
500     }
502     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
504     /* must: cn */
505     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
506       $message[]= "The required field 'Printer name' is not set.";
507     }
509     $ui= get_userinfo();
510     $acl= get_permissions ($this->dn, $ui->subtreeACL);
511     $acl= get_module_permission($acl, "printer", $this->dn);
512     if (chkacl($acl, "create") != ""){
513       $message[]= _("You have no permissions to create a printer on this 'Base'.");
514     }
516     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
517       $ldap= $this->config->get_ldap_link();
518       $ldap->cd ($this->base);
519       $ldap->search ("(cn=".$this->cn.")", array("cn"));
520       if ($ldap->count() != 0){
521         while ($attrs= $ldap->fetch()){
522           if ($attrs['dn'] != $this->orig_dn){
523             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
524             break;
525           }
526         }
527       }
528     }
530     return ($message);
531   }
534   /* Save to LDAP */
535   function save()
536   {
537     if (!$this->is_account){
538       return;
539     }
540     if(isset($this->parent->by_name['workgeneric'])){
541       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
542         return;
543       }
544     }elseif(isset($this->parent->by_name['termgeneric'])){
545       if($this->parent->by_object['termgeneric']->cn == "default"){
546         return;
547       }
548     }
550     if($this->PPDdialogToSave){
551       $this->PPDdialogToSave->save_ppd();
552     }
553     $dn= $this->dn;
554     plugin::save();
555     $ldap= $this->config->get_ldap_link();
556    
557     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
558       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
559     }
560     
561     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
562       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
563     }
565     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
566     */
567     if($this->is_terminal){
568       
569       /* reduce objectClasses to minimun */
570       $this->attrs['objectClass']= $this->objectclasses;
572       /* If a printer with the given dn exists, modify else create new one */
573       $ldap->cat($this->dn);
574       if($ldap->fetch()){
575         $this->orig_dn= $this->dn;
576       }else{
577         $this->orig_dn = "new";
578       }
579     }
581     /* Remove all empty values */
582     if ($this->orig_dn == 'new'){
583       $attrs= array();
584       foreach ($this->attrs as $key => $val){
585         if (is_array($val) && count($val) == 0){
586           continue;
587         }
588         $attrs[$key]= $val;
589       }
590       $this->attrs= $attrs;
591     }
593     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
594       $method="https://";
595     }else{
596       $method="http://";
597     }
598   
599     if(!is_array($this->attrs['gotoPrinterPPD'])) {
600       $this->attrs['gotoPrinterPPD'] = $this->attrs['gotoPrinterPPD'];
601       $this->attrs['gotoPrinterPPD'] = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->attrs['gotoPrinterPPD']);
602     }
604     /* Append printer user 
605      */
606     if(is_string( $this->attrs['gotoUserPrinter'])){
607        $this->attrs['gotoUserPrinter']=array();
608     }
609     foreach($this->member['AddUser'] as $mem){
610       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
611     }
613     /* Append printer group 
614      */
615     foreach($this->member['AddGroup'] as $mem){
616       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
617     }
619     /* Append printer admin user 
620      */
621     foreach($this->member['AddAdminUser'] as $mem){
622       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
623     }
624     
625     /* Append printer admin group 
626      */
627     foreach($this->member['AddAdminGroup'] as $mem){
628       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
629     }
631     /* Write back to ldap */
632     $ldap= $this->config->get_ldap_link();
633     if ($this->orig_dn == 'new'){
634       $ldap->cd($this->config->current['BASE']);
635       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
636       $ldap->cd($this->dn);
637       $ldap->add($this->attrs);
638       $this->handle_post_events("add");
639     } else {
640       if ($this->orig_dn != $this->dn){
641         $this->move($this->orig_dn, $this->dn);
642       }
644       $ldap->cd($this->dn);
645       $this->cleanup();
646 $ldap->modify ($this->attrs); 
648       $this->handle_post_events("modify");
649     }
650     show_ldap_error($ldap->get_error());
652     $this->netConfigDNS->cn = $this->cn;
653     $this->netConfigDNS->save($this->dn);
655     /* Optionally execute a command after we're done */
656     $this->postcreate();
657   }
659   function generateList(){
660     $a_return=array();
661     foreach($this->member as $type => $values){
662       $a_return[$type]=array();
663       foreach($values as $value){
664         if((preg_match("/Group/i",$type))){
665           if(!isset($value['description'])){
666             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
667           }else{
668             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
669           }
670         }else{
671           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
672         }
673       }
674     }
675     return($a_return);
676   }
681 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
682 ?>