Code

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