Code

819d495db72325b4513b7f392aa882a5db85b4b8
[gosa.git] / plugins / admin / systems / class_printGeneric.inc
1 <?php
10 class printgeneric extends plugin
11 {
12   /* CLI vars */
13   var $cli_summary      = "Manage terminal base objects";
14   var $cli_description  = "Some longer text\nfor help";
15   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
17   /* Generic terminal attributes */
18   var $interfaces     = array();
19   var $ignore_account = FALSE;
21   /* Needed values and lists */
22   var $base             = "";
23   var $cn               = "";
24   var $macAddress       = "";
25   var $ipHostNumber     = "";
26   var $l                = "";
27   var $description      = "";
28   var $labeledURI       = "";
29   var $gotoPrinterPPD   = "";
30   var $orig_dn          = "";
31   var $is_terminal      = false;
33   var $UserMember       ="";
34   var $UserMembers      =array();
35   var $UserMemberKeys   =array();
36   
37   var $AdminMember      ="";
38   var $AdminMembers     =array();
39   var $AdminMemberKeys  =array();
41   var $is_terminalBased = false; 
43   var $PPDdialogToSave  = NULL;
44  
45   var $member           =array();
46   var $strings          = "";
47   var $type             = "";
48   var $dialog           =NULL;
50   /* attribute list for save action */
51   var $attributes     = array("cn", "description", "l", "labeledURI", "macAddress", "ipHostNumber","gotoPrinterPPD");
52   var $objectclasses  = array("top", "gotoPrinter");
54   function printgeneric ($config, $dn= NULL)
55   {
56     plugin::plugin ($config, $dn);
57     $ldap= $this->config->get_ldap_link();
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=terminal/","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"){
167       $this->is_account = true;
168     }
169   }
171   function execute()
172   {
173         /* Call parent execute */
174         plugin::execute();
176     $smarty= get_smarty();
177     $display="";
178     /* Template management.
179      * There are two ways to call this tab.
180      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
181      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
182      *    Toggle Account state will be shown in this case, to disable or enable this tab.
183      *
184      * $this->is_terminal indecates this two different types.
185      */
186     if($this->is_terminal){    
187       $smarty->assign("is_terminal","true");
188     }else{
189       $smarty->assign("is_terminal","false");
190     }
192     /* Do we need to flip is_account state? */
193     if (isset($_POST['modify_state'])){
194       $this->is_modified = true;
195       $this->is_account= !$this->is_account;
196     }
198     if($this->is_terminal){
199       if(empty($this->cn)){
200         if(!in_array("gotoPrinter",$this->objectclasses)){
201           $display= $this->show_header(_("Add printer extension"),
202               _("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);
203           $this->is_account= false;
204           return $display;
205         }else{
206           $smarty->assign("is_terminal","false");
207           $skip = true;
208         }
209       }
211       if(!isset($skip)){
212         if (($this->is_account)){
213           if($this->type=="station"){
214             $display= $this->show_header(_("Remove printer extension"),
215                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
216           }else{
217             $display= $this->show_header(_("Remove printer extension"),
218                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
219           }
220         }else{
221           if($this->type=="station"){
222             $display= $this->show_header(_("Add printer extension"),
223                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
224           }else{
225             $display= $this->show_header(_("Add printer extension"),
226                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
227           }  
228           return ($display);
229         }
230       }
231     }
234     /* Do we represent a valid printer? */
235     if (!$this->is_account && $this->parent == NULL){
236       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
237         _("This 'dn' has no printer features.")."</b>";
238       return($display);
239     }
241     /* Fill templating stuff */
242     $smarty->assign("bases", $this->config->idepartments);
243     $smarty->assign("base_select", $this->base);
245     /* Assign attributes */
246     foreach ($this->attributes as $attr){
247       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
248       $smarty->assign("$attr", $this->$attr);
249     }
251     if(isset($_POST['AddUser'])){
252       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
253     }
254     if(isset($_POST['AddGroup'])){
255       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
256     }
257     if(isset($_POST['AddAdminUser'])){
258       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
259     }
260     if(isset($_POST['AddAdminGroup'])){
261       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
262     }
264     if(isset($_POST['EditDriver'])){
265       if($this->PPDdialogToSave){
266         $this->dialog = $this->PPDdialogToSave;
267       }else{
268         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
269       }
270     }
272     if(isset($_POST['PrinterCancel'])){
273       unset($this->dialog);
274       $this->dialog= NULL;
275     }
276     
277     if(isset($_POST['RemoveDriver'])){
278       $this->gotoPrinterPPD = array();
279       $this->PPDdialogToSave = NULL;
280     }
282     if(isset($_POST['SavePPD'])){
283       $this->dialog->save_object();
284       if(count($this->dialog->check())){
285         foreach($this->dialog->check() as $msg){
286           print_red($msg);
287         }
288       }else{
289         $this->gotoPrinterPPD = array();
290         
291         $this->gotoPrinterPPD = $this->dialog->save();
292         $this->PPDdialogToSave = $this->dialog;
293         unset($this->dialog);
294         $this->dialog=NULL;
295       }
297     }
299     if(isset($_POST['ClosePPD'])){
300       unset($this->dialog);
301       $this->dialog=NULL;
302     }
304     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
305       if(isset($this->member['AddUser'][$_POST['UserMember']])){
306         unset($this->member['AddUser'][$_POST['UserMember']]);
307       }
308     }
310     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
311       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
312         unset($this->member['AddGroup'][$_POST['UserMember']]);
313       }
314     }
316     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
317       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
318         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
319       }
320     }
322     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
323       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
324         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
325       }
326     }
328     if(isset($_POST['PrinterSave'])){
329       $this->dialog->save_object();
330       if(count($this->dialog->check())){
331         foreach($this->dialog->check() as $msg){
332           print_red($msg);
333         }
334       }else{
335         $new = $this->dialog->save();
336         $data = $new;
337         unset($data['type']);
339         if(preg_match("/User/",$new['type'])){
340           $use = "uid";
341         }else{
342           $use = "cn";
343         }
345         foreach($data as $mem){
346           $this->member[$new['type']][$mem[$use][0]]=$mem;    
347         }
348         unset($this->dialog);
349         $this->dialog=NULL; 
350       }
351     }
353     if($this->dialog != NULL){
354       $this->dialog->save_object();
355       $display = $this->dialog->execute();
356       return $display;
357     }
359     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
360     $smarty->assign("staticAddress", "");
363     require_once ("class_ppdManager.inc");
365     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
366       $ppdManager= new ppdManager($_SESSION['config']->data['MAIN']['PPD_PATH']);
367       if(!empty($this->gotoPrinterPPD)){
368         if((!file_exists($_SESSION['config']->data['MAIN']['PPD_PATH'].$this->gotoPrinterPPD))){
369           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
370         }else{
371           $smarty->assign("driverInfo", $ppdManager->loadDescription($_SESSION['config']->data['MAIN']['PPD_PATH'].$this->gotoPrinterPPD));
372         }
373       }else{
374         $smarty->assign("driverInfo", _("not defined"));
375       }
376     }else{
377       $smarty->assign("driverInfo",_("can't get ppd informations."));
378     }
381     $list=$this->generateList();
382     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
383     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
385     asort($userlist);
386     asort($adminlist);
387     if($this->is_terminalBased){
388       if($this->is_terminalBased == "Terminal"){
389         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
390       }else{
391         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
392       }
393       $smarty->assign("cnACL"    ," disabled ");
394     }else{
395       $smarty->assign("desc"    ,"");
396     }
397     $smarty->assign("UserMember"    ,$this->UserMember);
398     $smarty->assign("UserMembers"   ,$userlist);
399     $smarty->assign("UserMemberKeys",array_flip($userlist));
401     $smarty->assign("AdminMember"    ,$this->AdminMember);
402     $smarty->assign("AdminMembers"   ,$adminlist);
403     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
404     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
406     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
407   }
409   function remove_from_parent()
410   {
411     $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
412     $ldap= $this->config->get_ldap_link();
413     $ldap->rmdir($this->dn);
414     show_ldap_error($ldap->get_error());
415     $this->handle_post_events("remove");
417     /* Delete references to object groups */
418     $ldap->cd ($this->config->current['BASE']);
419     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
420     while ($ldap->fetch()){
421       $og= new ogroup($this->config, $ldap->getDN());
422       unset($og->member[$this->dn]);
423       $og->save ();
424     }
425   }
427   /* Save data to object */
428   function save_object()
429   {
430     plugin::save_object();
432     /* Save base, since this is no LDAP attribute */
433     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
434       $this->base= $_POST['base'];
435     }
436   }
438   /* Check supplied data */
439   function check()
440   {
441     $message= array();
442     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
444     /* must: cn */
445     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
446       $message[]= "The required field 'Printer name' is not set.";
447     }
449     $ui= get_userinfo();
450     $acl= get_permissions ($this->dn, $ui->subtreeACL);
451     $acl= get_module_permission($acl, "printer", $this->dn);
452     if (chkacl($acl, "create") != ""){
453       $message[]= _("You have no permissions to create a printer on this 'Base'.");
454     }
456     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
457       $ldap= $this->config->get_ldap_link();
458       $ldap->cd ($this->base);
459       $ldap->search ("(cn=".$this->cn.")", array("cn"));
460       if ($ldap->count() != 0){
461         while ($attrs= $ldap->fetch()){
462           if ($attrs['dn'] != $this->orig_dn){
463             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
464             break;
465           }
466         }
467       }
468     }
470     return ($message);
471   }
474   /* Save to LDAP */
475   function save()
476   {
477     if (!$this->is_account){
478       return;
479     }
480   
481     if($this->PPDdialogToSave){
482       $this->PPDdialogToSave->save_ppd();
483     }
484     $dn= $this->dn;
485     plugin::save();
486     $ldap= $this->config->get_ldap_link();
487    
488     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
489       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
490     }
491     
492     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
493       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
494     }
496     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
497     */
498     if($this->is_terminal){
499       
500       /* reduce objectClasses to minimun */
501       $this->attrs['objectClass']= $this->objectclasses;
503       /* If a printer with the given dn exists, modify else create new one */
504       $ldap->cat($this->dn);
505       if($ldap->fetch()){
506         $this->orig_dn= $this->dn;
507       }else{
508         $this->orig_dn = "new";
509       }
510     }
512     /* Remove all empty values */
513     if ($this->orig_dn == 'new'){
514       $attrs= array();
515       foreach ($this->attrs as $key => $val){
516         if (is_array($val) && count($val) == 0){
517           continue;
518         }
519         $attrs[$key]= $val;
520       }
521       $this->attrs= $attrs;
522     }
524     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
525       $method="https://";
526     }else{
527       $method="http://";
528     }
529   
530     if(!is_array($this->attrs['gotoPrinterPPD'])) {
531       $str = $method.$_SERVER['SERVER_NAME']."/ppd/";
532       $this->attrs['gotoPrinterPPD'] = $str.$this->attrs['gotoPrinterPPD'];
533       $this->attrs['gotoPrinterPPD'] = str_replace("//","/",$this->attrs['gotoPrinterPPD']);
534     }
536     /* Append printer user 
537      */
538     foreach($this->member['AddUser'] as $mem){
539       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
540     }
542     /* Append printer group 
543      */
544     foreach($this->member['AddGroup'] as $mem){
545       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
546     }
548     /* Append printer admin user 
549      */
550     foreach($this->member['AddAdminUser'] as $mem){
551       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
552     }
553     
554     /* Append printer admin group 
555      */
556     foreach($this->member['AddAdminGroup'] as $mem){
557       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
558     }
560     /* Write back to ldap */
561     $ldap= $this->config->get_ldap_link();
562     if ($this->orig_dn == 'new'){
563       $ldap->cd($this->config->current['BASE']);
564       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
565       $ldap->cd($this->dn);
566       $ldap->add($this->attrs);
567       $this->handle_post_events("add");
568     } else {
569       if ($this->orig_dn != $this->dn){
570         $this->move($this->orig_dn, $this->dn);
571       }
573       $ldap->cd($this->dn);
574       $ldap->modify($this->attrs);
575       $this->handle_post_events("modify");
576     }
577     show_ldap_error($ldap->get_error());
579     /* Optionally execute a command after we're done */
580     $this->postcreate();
581   }
583   function generateList(){
584     $a_return=array();
585     foreach($this->member as $type => $values){
586       $a_return[$type]=array();
587       foreach($values as $value){
588         if((preg_match("/Group/i",$type))){
589           if(!isset($value['description'])){
590             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
591           }else{
592             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
593           }
594         }else{
595           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
596         }
597       }
598     }
599     return($a_return);
600   }
605 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
606 ?>