Code

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