Code

Fixed url stripping
[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     $smarty= get_smarty();
170     $display="";
171     /* Template management.
172      * There are two ways to call this tab.
173      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
174      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
175      *    Toggle Account state will be shown in this case, to disable or enable this tab.
176      *
177      * $this->is_terminal indecates this two different types.
178      */
179     if($this->is_terminal){    
180       $smarty->assign("is_terminal","true");
181     }else{
182       $smarty->assign("is_terminal","false");
183     }
185     /* Do we need to flip is_account state? */
186     if (isset($_POST['modify_state'])){
187       $this->is_modified = true;
188       $this->is_account= !$this->is_account;
189     }
191     if($this->is_terminal){
192       if(empty($this->cn)){
193         if(!in_array("gotoPrinter",$this->objectclasses)){
194           $display= $this->show_header(_("Add printer extension"),
195               _("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);
196           $this->is_account= false;
197           return $display;
198         }else{
199           $smarty->assign("is_terminal","false");
200           $skip = true;
201         }
202       }
204       if(!isset($skip)){
205         if (($this->is_account)){
206           if($this->type=="station"){
207             $display= $this->show_header(_("Remove printer extension"),
208                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
209           }else{
210             $display= $this->show_header(_("Remove printer extension"),
211                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
212           }
213         }else{
214           if($this->type=="station"){
215             $display= $this->show_header(_("Add printer extension"),
216                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
217           }else{
218             $display= $this->show_header(_("Add printer extension"),
219                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
220           }  
221           return ($display);
222         }
223       }
224     }
227     /* Do we represent a valid printer? */
228     if (!$this->is_account && $this->parent == NULL){
229       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
230         _("This 'dn' has no printer features.")."</b>";
231       return($display);
232     }
234     /* Fill templating stuff */
235     $smarty->assign("bases", $this->config->idepartments);
236     $smarty->assign("base_select", $this->base);
238     /* Assign attributes */
239     foreach ($this->attributes as $attr){
240       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
241       $smarty->assign("$attr", $this->$attr);
242     }
244     if(isset($_POST['AddUser'])){
245       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
246     }
247     if(isset($_POST['AddGroup'])){
248       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
249     }
250     if(isset($_POST['AddAdminUser'])){
251       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
252     }
253     if(isset($_POST['AddAdminGroup'])){
254       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
255     }
257     if(isset($_POST['EditDriver'])){
258       if($this->PPDdialogToSave){
259         $this->dialog = $this->PPDdialogToSave;
260       }else{
261         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
262       }
263     }
265     if(isset($_POST['PrinterCancel'])){
266       unset($this->dialog);
267       $this->dialog= NULL;
268     }
269     
270     if(isset($_POST['RemoveDriver'])){
271       $this->gotoPrinterPPD = array();
272       $this->PPDdialogToSave = NULL;
273     }
275     if(isset($_POST['SavePPD'])){
276       $this->dialog->save_object();
277       if(count($this->dialog->check())){
278         foreach($this->dialog->check() as $msg){
279           print_red($msg);
280         }
281       }else{
282         $this->gotoPrinterPPD = array();
283         
284         $this->gotoPrinterPPD = $this->dialog->save();
285         $this->PPDdialogToSave = $this->dialog;
286         unset($this->dialog);
287         $this->dialog=NULL;
288       }
290     }
292     if(isset($_POST['ClosePPD'])){
293       unset($this->dialog);
294       $this->dialog=NULL;
295     }
297     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
298       if(isset($this->member['AddUser'][$_POST['UserMember']])){
299         unset($this->member['AddUser'][$_POST['UserMember']]);
300       }
301     }
303     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
304       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
305         unset($this->member['AddGroup'][$_POST['UserMember']]);
306       }
307     }
309     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
310       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
311         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
312       }
313     }
315     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
316       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
317         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
318       }
319     }
321     if(isset($_POST['PrinterSave'])){
322       $this->dialog->save_object();
323       if(count($this->dialog->check())){
324         foreach($this->dialog->check() as $msg){
325           print_red($msg);
326         }
327       }else{
328         $new = $this->dialog->save();
329         $data = $new;
330         unset($data['type']);
332         if(preg_match("/User/",$new['type'])){
333           $use = "uid";
334         }else{
335           $use = "cn";
336         }
338         foreach($data as $mem){
339           $this->member[$new['type']][$mem[$use][0]]=$mem;    
340         }
341         unset($this->dialog);
342         $this->dialog=NULL; 
343       }
344     }
346     if($this->dialog != NULL){
347       $this->dialog->save_object();
348       $display = $this->dialog->execute();
349       return $display;
350     }
352     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
353     $smarty->assign("staticAddress", "");
356     require_once ("class_ppdManager.inc");
358     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
359       $ppdManager= new ppdManager($_SESSION['config']->data['MAIN']['PPD_PATH']);
360       if(!empty($this->gotoPrinterPPD)){
361         if((!file_exists($_SESSION['config']->data['MAIN']['PPD_PATH'].$this->gotoPrinterPPD))){
362           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
363         }else{
364           $smarty->assign("driverInfo", $ppdManager->loadDescription($_SESSION['config']->data['MAIN']['PPD_PATH'].$this->gotoPrinterPPD));
365         }
366       }else{
367         $smarty->assign("driverInfo", _("not defined"));
368       }
369     }else{
370       $smarty->assign("driverInfo",_("can't get ppd informations."));
371     }
374     $list=$this->generateList();
375     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
376     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
378     asort($userlist);
379     asort($adminlist);
380     if($this->is_terminalBased){
381       if($this->is_terminalBased == "Terminal"){
382         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
383       }else{
384         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
385       }
386       $smarty->assign("cnACL"    ," disabled ");
387     }else{
388       $smarty->assign("desc"    ,"");
389     }
390     $smarty->assign("UserMember"    ,$this->UserMember);
391     $smarty->assign("UserMembers"   ,$userlist);
392     $smarty->assign("UserMemberKeys",array_flip($userlist));
394     $smarty->assign("AdminMember"    ,$this->AdminMember);
395     $smarty->assign("AdminMembers"   ,$adminlist);
396     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
397     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
399     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
400   }
402   function remove_from_parent()
403   {
404     $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
405     $ldap= $this->config->get_ldap_link();
406     $ldap->rmdir($this->dn);
407     show_ldap_error($ldap->get_error());
408     $this->handle_post_events("remove");
410     /* Delete references to object groups */
411     $ldap->cd ($this->config->current['BASE']);
412     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
413     while ($ldap->fetch()){
414       $og= new ogroup($this->config, $ldap->getDN());
415       unset($og->member[$this->dn]);
416       $og->save ();
417     }
418   }
420   /* Save data to object */
421   function save_object()
422   {
423     plugin::save_object();
425     /* Save base, since this is no LDAP attribute */
426     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
427       $this->base= $_POST['base'];
428     }
429   }
431   /* Check supplied data */
432   function check()
433   {
434     $message= array();
435     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
437     /* must: cn */
438     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
439       $message[]= "The required field 'Printer name' is not set.";
440     }
442     $ui= get_userinfo();
443     $acl= get_permissions ($this->dn, $ui->subtreeACL);
444     $acl= get_module_permission($acl, "printer", $this->dn);
445     if (chkacl($acl, "create") != ""){
446       $message[]= _("You have no permissions to create a printer on this 'Base'.");
447     }
449     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
450       $ldap= $this->config->get_ldap_link();
451       $ldap->cd ($this->base);
452       $ldap->search ("(cn=".$this->cn.")", array("cn"));
453       if ($ldap->count() != 0){
454         while ($attrs= $ldap->fetch()){
455           if ($attrs['dn'] != $this->orig_dn){
456             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
457             break;
458           }
459         }
460       }
461     }
463     return ($message);
464   }
467   /* Save to LDAP */
468   function save()
469   {
470     if (!$this->is_account){
471       return;
472     }
473   
474     if($this->PPDdialogToSave){
475       $this->PPDdialogToSave->save_ppd();
476     }
477     $dn= $this->dn;
478     plugin::save();
479     $ldap= $this->config->get_ldap_link();
480    
481     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
482       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
483     }
484     
485     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
486       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
487     }
489     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
490     */
491     if($this->is_terminal){
492       
493       /* reduce objectClasses to minimun */
494       $this->attrs['objectClass']= $this->objectclasses;
496       /* If a printer with the given dn exists, modify else create new one */
497       $ldap->cat($this->dn);
498       if($ldap->fetch()){
499         $this->orig_dn= $this->dn;
500       }else{
501         $this->orig_dn = "new";
502       }
503     }
505     /* Remove all empty values */
506     if ($this->orig_dn == 'new'){
507       $attrs= array();
508       foreach ($this->attrs as $key => $val){
509         if (is_array($val) && count($val) == 0){
510           continue;
511         }
512         $attrs[$key]= $val;
513       }
514       $this->attrs= $attrs;
515     }
517     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
518       $method="https://";
519     }else{
520       $method="http://";
521     }
522   
523     if(!is_array($this->attrs['gotoPrinterPPD'])) {
524       $this->attrs['gotoPrinterPPD'] = $this->attrs['gotoPrinterPPD'];
525       $this->attrs['gotoPrinterPPD'] = $method.$_SERVER['SERVER_NAME']."/ppd/".
526                                        str_replace("//","/",$this->attrs['gotoPrinterPPD']);
527     }
529     /* Append printer user 
530      */
531     foreach($this->member['AddUser'] as $mem){
532       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
533     }
535     /* Append printer group 
536      */
537     foreach($this->member['AddGroup'] as $mem){
538       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
539     }
541     /* Append printer admin user 
542      */
543     foreach($this->member['AddAdminUser'] as $mem){
544       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
545     }
546     
547     /* Append printer admin group 
548      */
549     foreach($this->member['AddAdminGroup'] as $mem){
550       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
551     }
553     /* Write back to ldap */
554     $ldap= $this->config->get_ldap_link();
555     if ($this->orig_dn == 'new'){
556       $ldap->cd($this->config->current['BASE']);
557       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
558       $ldap->cd($this->dn);
559       $ldap->add($this->attrs);
560       $this->handle_post_events("add");
561     } else {
562       if ($this->orig_dn != $this->dn){
563         $this->move($this->orig_dn, $this->dn);
564       }
566       $ldap->cd($this->dn);
567       $ldap->modify($this->attrs);
568       $this->handle_post_events("modify");
569     }
570     show_ldap_error($ldap->get_error());
572     /* Optionally execute a command after we're done */
573     $this->postcreate();
574   }
576   function generateList(){
577     $a_return=array();
578     foreach($this->member as $type => $values){
579       $a_return[$type]=array();
580       foreach($values as $value){
581         if((preg_match("/Group/i",$type))){
582           if(!isset($value['description'])){
583             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
584           }else{
585             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
586           }
587         }else{
588           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
589         }
590       }
591     }
592     return($a_return);
593   }
598 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
599 ?>