Code

Added ppd for printer dialog, schema parser is still missing
[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();
33   
34   var $member           =array();
35   var $strings          = "";
36   var $type             = "";
37   var $dialog           =NULL;
39   /* attribute list for save action */
40   var $attributes     = array("cn", "description", "l", "labeledURI", "macAddress", "ipHostNumber","gotoPrinterPPD");
41   var $objectclasses  = array("top", "gotoPrinter");
43   function printgeneric ($config, $dn= NULL)
44   {
45     plugin::plugin ($config, $dn);
46     $ldap= $this->config->get_ldap_link();
48     /* Set base */
49     if ($this->dn == "new"){
50       $ui= get_userinfo();
51       $this->base= dn2base($ui->dn);
52       $this->cn= "";
53     } else {
54       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
55     }
58     /* In case of gotoWorkstation this tab is calles from workstation plugin
59      * in case of gotoTerminal it is called from a terminal tab
60      * else it is a standalone printer
61      */
62     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
63       $this->is_terminal = "true";   
64       $this->dn   = preg_replace("/ou=workstation/","ou=printer",$this->dn);
65       $this->type = "station";
66     }elseif((in_array("gotoTerminal",$this->attrs['objectClass']))){
67       $this->type = "terminal";
68       $this->is_terminal = "true";
69       $this->dn   = preg_replace("/ou=terminal/","ou=printer",$this->dn);
70     }else{
71       /* Save dn for later references */
72       $this->orig_dn= $this->dn;
73     }
75     /* If it is no standalone printer 
76      */
77     if($this->is_terminal){
78       // Reload plugin with new dn... (ou=printers instead of ou=terminals)
79       plugin::plugin ($this->config, $this->dn);
80       $ldap->cat($this->dn);
81       if(count($ldap->fetch())>0){
82         $this->orig_dn= $this->dn;
83         $this->is_account=true;
84         $this->initially_was_account = true;
85       }else{
86         $this->orig_dn = "new";
87         $this->is_account=false;
88         $this->initially_was_account = false;
89       }
90     }
92    
93     /* Prepare different member types 
94      */ 
95     foreach(array("AddUser"       =>"gotoUserPrinter",
96                   "AddGroup"      =>"gotoGroupPrinter",
97                   "AddAdminUser"  =>"gotoUserAdminPrinter",
98                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
100       /* $this->members contains all members */
101       $this->member[$type]=array();
103       unset($this->attrs[$attr]['count']);
105       if(isset($this->attrs[$attr])){
106         foreach($this->attrs[$attr] as $mem){
107           if(preg_match("/Group/",$type)){
108             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
109             $entry = $ldap->fetch();
110             if(isset($entry['description'])){
111               $this->member[$type][$entry['cn'][0]]=$entry;
112             }
113           }else{
114             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
115             $entry = $ldap->fetch();
116             if(isset($entry['uid'])){
117               $this->member[$type][$entry['uid'][0]]=$entry;
118             }
119           }
120         }
121       }
122     }
123   }
125   function execute()
126   {
128     $smarty= get_smarty();
129     $display="";
130     /* Template management.
131      * There are two ways to call this tab.
132      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
133      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
134      *    Toggle Account state will be shown in this case, to disable or enable this tab.
135      *
136      * $this->is_terminal indecates this two different types.
137      */
138     if($this->is_terminal){    
139       $smarty->assign("is_terminal","true");
140     }else{
141       $smarty->assign("is_terminal","false");
142     }
144     /* Do we need to flip is_account state? */
145     if (isset($_POST['modify_state'])){
146       $this->is_modified = true;
147       $this->is_account= !$this->is_account;
148     }
150     if($this->is_terminal){
151       if ($this->is_account){
152         if($this->type=="station"){
153           $display= $this->show_header(_("Remove printer extension"),
154               _("This workstation has printer extension enabled. You can disable it by clicking below."));
155         }else{
156           $display= $this->show_header(_("Remove printer extension"),
157               _("This terminal has printer extension enabled. You can disable it by clicking below."));
158         }
159       }else{
160         if($this->type=="station"){
161           $display= $this->show_header(_("Add printer extension"),
162               _("This workstation has printer extension disabled. You can enable it by clicking below."));
163         }else{
164           $display= $this->show_header(_("Add printer extension"),
165               _("This terminal has printer extension disabled. You can enable it by clicking below."));
166         }  
167         return ($display);
168       }
169     }
172     /* Do we represent a valid printer? */
173     if (!$this->is_account && $this->parent == NULL){
174       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
175         _("This 'dn' has no printer features.")."</b>";
176       return($display);
177     }
179     /* Fill templating stuff */
180     $smarty->assign("bases", $this->config->idepartments);
181     $smarty->assign("base_select", $this->base);
183     /* Assign attributes */
184     foreach ($this->attributes as $attr){
185       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
186       $smarty->assign("$attr", $this->$attr);
187     }
188     
189     if(isset($_POST['AddUser'])){
190       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
191     }
192     if(isset($_POST['AddGroup'])){
193       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
194     }
195     if(isset($_POST['AddAdminUser'])){
196       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
197     }
198     if(isset($_POST['AddAdminGroup'])){
199       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
200     }
202     if(isset($_POST['EditDriver'])){
203       $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
204     }
206     if(isset($_POST['PrinterCancel'])){
207       unset($this->dialog);
208       $this->dialog= NULL;
209     }
211     if(isset($_POST['SavePPD'])){
212       if(count($this->dialog->check())){
213         foreach($this->dialog->check() as $msg){
214           print_red($msg);
215         }
216       }else{
217         $this->gotoPrinterPPD = array();
218         $this->gotoPrinterPPD = $this->dialog->save();
219         unset($this->dialog);
220         $this->dialog=NULL;
221       }
223     }
224     
225     if(isset($_POST['ClosePPD'])){
226       unset($this->dialog);
227       $this->dialog=NULL;
228     }
230     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
231       if(isset($this->member['AddUser'][$_POST['UserMember']])){
232         unset($this->member['AddUser'][$_POST['UserMember']]);
233       }
234     }
236     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
237       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
238         unset($this->member['AddGroup'][$_POST['UserMember']]);
239       }
240     }
242     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
243       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
244         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
245       }
246     }
248     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
249       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
250         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
251       }
252     }
254     if(isset($_POST['PrinterSave'])){
255       $this->dialog->save_object();
256       if(count($this->dialog->check())){
257         foreach($this->dialog->check() as $msg){
258           print_red($msg);
259         }
260       }else{
261         $new = $this->dialog->save();
262         $data = $new;
263         unset($data['type']);
265         if(preg_match("/User/",$new['type'])){
266           $use = "uid";
267         }else{
268           $use = "cn";
269         }
271         foreach($data as $mem){
272           $this->member[$new['type']][$mem[$use][0]]=$mem;    
273         }
274         unset($this->dialog);
275         $this->dialog=NULL; 
276       }
277     }
279     if($this->dialog != NULL){
280       $display = $this->dialog->execute();
281       return $display;
282     }
284     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
285     $smarty->assign("staticAddress", "");
288     require_once ("class_ppdManager.inc");
289     $ppdManager= new ppdManager('/var/spool/ppd/');
290     if(!empty($this->gotoPrinterPPD)){
291       $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
292     }else{
293       $smarty->assign("driverInfo", _("Undefined"));
294     }
296     $list=$this->generateList();
297     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
298     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
300     asort($userlist);
301     asort($adminlist);
303     $smarty->assign("UserMember"    ,$this->UserMember);
304     $smarty->assign("UserMembers"   ,$userlist);
305     $smarty->assign("UserMemberKeys",array_flip($userlist));
307     $smarty->assign("AdminMember"    ,$this->AdminMember);
308     $smarty->assign("AdminMembers"   ,$adminlist);
309     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
310     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
312     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
313   }
315   function remove_from_parent()
316   {
317     $ldap= $this->config->get_ldap_link();
318     $ldap->rmdir($this->dn);
319     show_ldap_error($ldap->get_error());
320     $this->handle_post_events("remove");
322     /* Delete references to object groups */
323     $ldap->cd ($this->config->current['BASE']);
324     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
325     while ($ldap->fetch()){
326       $og= new ogroup($this->config, $ldap->getDN());
327       unset($og->member[$this->dn]);
328       $og->save ();
329     }
330   }
332   /* Save data to object */
333   function save_object()
334   {
335     plugin::save_object();
337     /* Save base, since this is no LDAP attribute */
338     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
339       $this->base= $_POST['base'];
340     }
341   }
343   /* Check supplied data */
344   function check()
345   {
346     $message= array();
347     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
349     /* must: cn */
350     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
351       $message[]= "The required field 'Printer name' is not set.";
352     }
354     $ui= get_userinfo();
355     $acl= get_permissions ($this->dn, $ui->subtreeACL);
356     $acl= get_module_permission($acl, "printer", $this->dn);
357     if (chkacl($acl, "create") != ""){
358       $message[]= _("You have no permissions to create a printer on this 'Base'.");
359     }
361     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
362       $ldap= $this->config->get_ldap_link();
363       $ldap->cd ($this->base);
364       $ldap->search ("(cn=".$this->cn.")", array("cn"));
365       if ($ldap->count() != 0){
366         while ($attrs= $ldap->fetch()){
367           if ($attrs['dn'] != $this->orig_dn){
368             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
369             break;
370           }
371         }
372       }
373     }
375     return ($message);
376   }
379   /* Save to LDAP */
380   function save()
381   {
382     $dn= $this->dn;
383     plugin::save();
384     $ldap= $this->config->get_ldap_link();
385    
386     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
387       $this->dn= preg_replace("/ou=terminal/","ou=printer",$this->dn);
388     }
389     
390     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
391       $this->dn= preg_replace("/ou=workstation/","ou=printer",$this->dn);
392     }
394     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
395     */
396     if($this->is_terminal){
397       
398       /* reduce objectClasses to minimun */
399       $this->attrs['objectClass']= $this->objectclasses;
401       /* If a printer with the given dn exists, modify else create new one */
402       $ldap->cat($this->dn);
403       if($ldap->fetch()){
404         $this->orig_dn= $this->dn;
405       }else{
406         $this->orig_dn = "new";
407       }
408     }
410     /* Remove all empty values */
411     if ($this->orig_dn == 'new'){
412       $attrs= array();
413       foreach ($this->attrs as $key => $val){
414         if (is_array($val) && count($val) == 0){
415           continue;
416         }
417         $attrs[$key]= $val;
418       }
419       $this->attrs= $attrs;
420     }
423     /* Append printer user 
424      */
425     foreach($this->member['AddUser'] as $mem){
426       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
427     }
429     /* Append printer group 
430      */
431     foreach($this->member['AddGroup'] as $mem){
432       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
433     }
435     /* Append printer admin user 
436      */
437     foreach($this->member['AddAdminUser'] as $mem){
438       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
439     }
440     
441     /* Append printer admin group 
442      */
443     foreach($this->member['AddAdminGroup'] as $mem){
444       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
445     }
447     /* Write back to ldap */
448     $ldap= $this->config->get_ldap_link();
449     if ($this->orig_dn == 'new'){
450       $ldap->cd($this->config->current['BASE']);
451       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
452       $ldap->cd($this->dn);
453       $ldap->add($this->attrs);
454       $this->handle_post_events("add");
455     } else {
456       if ($this->orig_dn != $this->dn){
457         $this->move($this->orig_dn, $this->dn);
458       }
460       $ldap->cd($this->dn);
461       $ldap->modify($this->attrs);
462       $this->handle_post_events("modify");
463     }
464     show_ldap_error($ldap->get_error());
466     /* Optionally execute a command after we're done */
467     $this->postcreate();
468   }
470   function generateList(){
471     $a_return=array();
472     foreach($this->member as $type => $values){
473       $a_return[$type]=array();
474       foreach($values as $value){
475         if((preg_match("/Group/i",$type))){
476           if(!isset($value['description'])){
477             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
478           }else{
479             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
480           }
481         }else{
482           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
483         }
484       }
485     }
486     return($a_return);
487   }
492 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
493 ?>