Code

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