Code

b883023405c5654d2f4e6d670fbe3dc4ae460b9e
[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; 
35  
36   var $member           =array();
37   var $strings          = "";
38   var $type             = "";
39   var $dialog           =NULL;
41   /* attribute list for save action */
42   var $attributes     = array("cn", "description", "l", "labeledURI", "macAddress", "ipHostNumber","gotoPrinterPPD");
43   var $objectclasses  = array("top", "gotoPrinter");
45   function printgeneric ($config, $dn= NULL)
46   {
47     plugin::plugin ($config, $dn);
48     $ldap= $this->config->get_ldap_link();
50     /* Set base */
51     if ($this->dn == "new"){
52       $ui= get_userinfo();
53       $this->base= dn2base($ui->dn);
54       $this->cn= "";
55       $this->is_terminal = true;
56     } else {
57       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
58     }
61     /* In case of gotoWorkstation this tab is calles from workstation plugin
62      * in case of gotoTerminal it is called from a terminal tab
63      * else it is a standalone printer
64      */
65     if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
66       /* Fix for new Workstations */
67       if(preg_match("/ou=incoming/",$this->dn)){    
68         $this->is_terminal = "true";   
69         $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
70         $this->type = "station";
71       }else{
72         $this->is_terminal = "true";   
73         $this->dn   = preg_replace("/ou=workstations/","ou=printers",$this->dn);
74         $this->type = "station";
75       }
76     }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
77       if(preg_match("/ou=incoming/",$this->dn)){    
78         $this->is_terminal = "true";   
79         $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
80         $this->type = "terminal";
81       }else{
82         $this->type = "terminal";
83         $this->is_terminal = "true";
84         $this->dn   = preg_replace("/ou=terminal/","ou=printers",$this->dn);
85       }
86     }else{
87       /* Save dn for later references */
88       $this->orig_dn= $this->dn;
89     }
91     /* If it is no standalone printer 
92      */
93     if($this->is_terminal){
94       // Reload plugin with new dn... (ou=printers instead of ou=terminals)
95       plugin::plugin ($this->config, $this->dn);
96       $ldap->cat($this->dn);
97       if(count($ldap->fetch())>0){
98         $this->orig_dn= $this->dn;
99         $this->is_account=true;
100         $this->initially_was_account = true;
101       }else{
102         $this->orig_dn = "new";
103         $this->is_account=false;
104         $this->initially_was_account = false;
105       }
106     }
108     /* Prepare different member types 
109      */ 
110     foreach(array("AddUser"       =>"gotoUserPrinter",
111                   "AddGroup"      =>"gotoGroupPrinter",
112                   "AddAdminUser"  =>"gotoUserAdminPrinter",
113                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
115       /* $this->members contains all members */
116       $this->member[$type]=array();
118       unset($this->attrs[$attr]['count']);
120       if(isset($this->attrs[$attr])){
121         foreach($this->attrs[$attr] as $mem){
122           if(preg_match("/Group/",$type)){
123             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
124             $entry = $ldap->fetch();
125             if(isset($entry['description'])){
126               $this->member[$type][$entry['cn'][0]]=$entry;
127             }
128           }else{
129             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
130             $entry = $ldap->fetch();
131             if(isset($entry['uid'])){
132               $this->member[$type][$entry['uid'][0]]=$entry;
133             }
134           }
135         }
136       }
137     }
138   
139     $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
140     if($attrs = $ldap->fetch()){
141       if(in_array("gotoTerminal",$attrs['objectClass'])){
142         $this->is_terminalBased = "Terminal";
143       }else{
144         $this->is_terminalBased = "Workstation";
145       }
146     }
148     /* A printer was only saved if the printer belongs to a workstation. 
149      * If there was a new single printer created, it was never saved.
150      * -This allows new printers to be saved.
151      */
152     if($this->dn == "new"){
153       $this->is_account = true;
154     }
155   }
157   function execute()
158   {
159     $smarty= get_smarty();
160     $display="";
161     /* Template management.
162      * There are two ways to call this tab.
163      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
164      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
165      *    Toggle Account state will be shown in this case, to disable or enable this tab.
166      *
167      * $this->is_terminal indecates this two different types.
168      */
169     if($this->is_terminal){    
170       $smarty->assign("is_terminal","true");
171     }else{
172       $smarty->assign("is_terminal","false");
173     }
175     /* Do we need to flip is_account state? */
176     if (isset($_POST['modify_state'])){
177       $this->is_modified = true;
178       $this->is_account= !$this->is_account;
179     }
181     if($this->is_terminal){
182       if(empty($this->cn)){
183         if(!in_array("gotoPrinter",$this->objectclasses)){
184           $display= $this->show_header(_("Add printer extension"),
185               _("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);
186           $this->is_account= false;
187           return $display;
188         }else{
189           $smarty->assign("is_terminal","false");
190           $skip = true;
191         }
192       }
194       if(!isset($skip)){
195         if (($this->is_account)){
196           if($this->type=="station"){
197             $display= $this->show_header(_("Remove printer extension"),
198                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
199           }else{
200             $display= $this->show_header(_("Remove printer extension"),
201                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
202           }
203         }else{
204           if($this->type=="station"){
205             $display= $this->show_header(_("Add printer extension"),
206                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
207           }else{
208             $display= $this->show_header(_("Add printer extension"),
209                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
210           }  
211           return ($display);
212         }
213       }
214     }
217     /* Do we represent a valid printer? */
218     if (!$this->is_account && $this->parent == NULL){
219       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
220         _("This 'dn' has no printer features.")."</b>";
221       return($display);
222     }
224     /* Fill templating stuff */
225     $smarty->assign("bases", $this->config->idepartments);
226     $smarty->assign("base_select", $this->base);
228     /* Assign attributes */
229     foreach ($this->attributes as $attr){
230       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
231       $smarty->assign("$attr", $this->$attr);
232     }
234     if(isset($_POST['AddUser'])){
235       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
236     }
237     if(isset($_POST['AddGroup'])){
238       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
239     }
240     if(isset($_POST['AddAdminUser'])){
241       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
242     }
243     if(isset($_POST['AddAdminGroup'])){
244       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
245     }
247     if(isset($_POST['EditDriver'])){
248       $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
249     }
251     if(isset($_POST['PrinterCancel'])){
252       unset($this->dialog);
253       $this->dialog= NULL;
254     }
255     
256     if(isset($_POST['RemoveDriver'])){
257       
258       $this->gotoPrinterPPD = array();
259     }
261     if(isset($_POST['SavePPD'])){
262       $this->dialog->save_object();
263       if(count($this->dialog->check())){
264         foreach($this->dialog->check() as $msg){
265           print_red($msg);
266         }
267       }else{
268         $this->gotoPrinterPPD = array();
269         
270         $this->gotoPrinterPPD = $this->dialog->save();
271         unset($this->dialog);
272         $this->dialog=NULL;
273       }
275     }
277     if(isset($_POST['ClosePPD'])){
278       unset($this->dialog);
279       $this->dialog=NULL;
280     }
282     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
283       if(isset($this->member['AddUser'][$_POST['UserMember']])){
284         unset($this->member['AddUser'][$_POST['UserMember']]);
285       }
286     }
288     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
289       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
290         unset($this->member['AddGroup'][$_POST['UserMember']]);
291       }
292     }
294     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
295       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
296         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
297       }
298     }
300     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
301       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
302         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
303       }
304     }
306     if(isset($_POST['PrinterSave'])){
307       $this->dialog->save_object();
308       if(count($this->dialog->check())){
309         foreach($this->dialog->check() as $msg){
310           print_red($msg);
311         }
312       }else{
313         $new = $this->dialog->save();
314         $data = $new;
315         unset($data['type']);
317         if(preg_match("/User/",$new['type'])){
318           $use = "uid";
319         }else{
320           $use = "cn";
321         }
323         foreach($data as $mem){
324           $this->member[$new['type']][$mem[$use][0]]=$mem;    
325         }
326         unset($this->dialog);
327         $this->dialog=NULL; 
328       }
329     }
331     if($this->dialog != NULL){
332       $this->dialog->save_object();
333       $display = $this->dialog->execute();
334       return $display;
335     }
337     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
338     $smarty->assign("staticAddress", "");
341     require_once ("class_ppdManager.inc");
342     $ppdManager= new ppdManager('/var/spool/ppd/');
343     if(!empty($this->gotoPrinterPPD)){
344       $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
345     }else{
346       $smarty->assign("driverInfo", _("Undefined"));
347     }
349     $list=$this->generateList();
350     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
351     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
353     asort($userlist);
354     asort($adminlist);
355     if($this->is_terminalBased){
356       if($this->is_terminalBased == "Terminal"){
357         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
358       }else{
359         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
360       }
361       $smarty->assign("cnACL"    ," disabled ");
362     }else{
363       $smarty->assign("desc"    ,"");
364     }
365     $smarty->assign("UserMember"    ,$this->UserMember);
366     $smarty->assign("UserMembers"   ,$userlist);
367     $smarty->assign("UserMemberKeys",array_flip($userlist));
369     $smarty->assign("AdminMember"    ,$this->AdminMember);
370     $smarty->assign("AdminMembers"   ,$adminlist);
371     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
372     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
374     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
375   }
377   function remove_from_parent()
378   {
379     $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
380     $ldap= $this->config->get_ldap_link();
381     $ldap->rmdir($this->dn);
382     show_ldap_error($ldap->get_error());
383     $this->handle_post_events("remove");
385     /* Delete references to object groups */
386     $ldap->cd ($this->config->current['BASE']);
387     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
388     while ($ldap->fetch()){
389       $og= new ogroup($this->config, $ldap->getDN());
390       unset($og->member[$this->dn]);
391       $og->save ();
392     }
393   }
395   /* Save data to object */
396   function save_object()
397   {
398     plugin::save_object();
400     /* Save base, since this is no LDAP attribute */
401     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
402       $this->base= $_POST['base'];
403     }
404   }
406   /* Check supplied data */
407   function check()
408   {
409     $message= array();
410     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
412     /* must: cn */
413     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
414       $message[]= "The required field 'Printer name' is not set.";
415     }
417     $ui= get_userinfo();
418     $acl= get_permissions ($this->dn, $ui->subtreeACL);
419     $acl= get_module_permission($acl, "printer", $this->dn);
420     if (chkacl($acl, "create") != ""){
421       $message[]= _("You have no permissions to create a printer on this 'Base'.");
422     }
424     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
425       $ldap= $this->config->get_ldap_link();
426       $ldap->cd ($this->base);
427       $ldap->search ("(cn=".$this->cn.")", array("cn"));
428       if ($ldap->count() != 0){
429         while ($attrs= $ldap->fetch()){
430           if ($attrs['dn'] != $this->orig_dn){
431             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
432             break;
433           }
434         }
435       }
436     }
438     return ($message);
439   }
442   /* Save to LDAP */
443   function save()
444   {
445     if (!$this->is_account){
446       return;
447     }
448     
449     $dn= $this->dn;
450     plugin::save();
451     $ldap= $this->config->get_ldap_link();
452    
453     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
454       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
455     }
456     
457     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
458       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
459     }
461     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
462     */
463     if($this->is_terminal){
464       
465       /* reduce objectClasses to minimun */
466       $this->attrs['objectClass']= $this->objectclasses;
468       /* If a printer with the given dn exists, modify else create new one */
469       $ldap->cat($this->dn);
470       if($ldap->fetch()){
471         $this->orig_dn= $this->dn;
472       }else{
473         $this->orig_dn = "new";
474       }
475     }
477     /* Remove all empty values */
478     if ($this->orig_dn == 'new'){
479       $attrs= array();
480       foreach ($this->attrs as $key => $val){
481         if (is_array($val) && count($val) == 0){
482           continue;
483         }
484         $attrs[$key]= $val;
485       }
486       $this->attrs= $attrs;
487     }
490     /* Append printer user 
491      */
492     foreach($this->member['AddUser'] as $mem){
493       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
494     }
496     /* Append printer group 
497      */
498     foreach($this->member['AddGroup'] as $mem){
499       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
500     }
502     /* Append printer admin user 
503      */
504     foreach($this->member['AddAdminUser'] as $mem){
505       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
506     }
507     
508     /* Append printer admin group 
509      */
510     foreach($this->member['AddAdminGroup'] as $mem){
511       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
512     }
514     /* Write back to ldap */
515     $ldap= $this->config->get_ldap_link();
516     if ($this->orig_dn == 'new'){
517       $ldap->cd($this->config->current['BASE']);
518       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
519       $ldap->cd($this->dn);
520       $ldap->add($this->attrs);
521       $this->handle_post_events("add");
522     } else {
523       if ($this->orig_dn != $this->dn){
524         $this->move($this->orig_dn, $this->dn);
525       }
527       $ldap->cd($this->dn);
528       $ldap->modify($this->attrs);
529       $this->handle_post_events("modify");
530     }
531     show_ldap_error($ldap->get_error());
533     /* Optionally execute a command after we're done */
534     $this->postcreate();
535   }
537   function generateList(){
538     $a_return=array();
539     foreach($this->member as $type => $values){
540       $a_return[$type]=array();
541       foreach($values as $value){
542         if((preg_match("/Group/i",$type))){
543           if(!isset($value['description'])){
544             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
545           }else{
546             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
547           }
548         }else{
549           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
550         }
551       }
552     }
553     return($a_return);
554   }
559 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
560 ?>