Code

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