Code

a9a2bdbebc6735f9e49cd822e6e77d9317608f32
[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();
30   var $AdminMember      ="";
31   var $AdminMembers     =array();
32   var $AdminMemberKeys  =array();
34   var $member           =array();
35   
36   var $strings          = "";
38   var $type             = "";
39   var $dialog           =NULL;
41   /* attribute list for save action */
42   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","macAddress", "ipHostNumber");
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     } else {
56       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
57     }
60     /* In case of gotoWorkstation this tab is calles from workstation plugin
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($this->is_terminal){
76       // Reload plugin with new dn... (ou=printers instead of ou=terminals)
77       plugin::plugin ($this->config, $this->dn);
78       $ldap->cat($this->dn);
79       if(count($ldap->fetch())>0){
80         $this->orig_dn= $this->dn;
81         $this->is_account=true;
82         $this->initially_was_account = true;
83       }else{
84         $this->orig_dn = "new";
85         $this->is_account=false;
86         $this->initially_was_account = false;
87       }
88     }
90    
91     /* Prepare different member types 
92      */ 
93     foreach(array("AddUser"       =>"gotoUserPrinter",
94                   "AddGroup"      =>"gotoGroupPrinter",
95                   "AddAdminUser"  =>"gotoUserAdminPrinter",
96                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
98       /* $this->members contains all members */
99       $this->member[$type]=array();
101       unset($this->attrs[$attr]['count']);
103       if(isset($this->attrs[$attr])){
104         foreach($this->attrs[$attr] as $mem){
105           if(preg_match("/Group/",$type)){
106             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
107             $entry = $ldap->fetch();
108             if(isset($entry['description'])){
109               $this->member[$type][$entry['cn'][0]]=$entry;
110             }
111           }else{
112             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
113             $entry = $ldap->fetch();
114             if(isset($entry['uid'])){
115               $this->member[$type][$entry['uid'][0]]=$entry;
116             }
117           }
118         }
119       }
120     }
121   }
123   function execute()
124   {
125     $smarty= get_smarty();
126     $display="";
127     /* Template management.
128      * There are two ways to call this tab.
129      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
130      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
131      *    Toggle Account state will be shown in this case, to disable or enable this tab.
132      *
133      * $this->is_terminal indecates this two different types.
134      */
135     if($this->is_terminal){    
136       $smarty->assign("is_terminal","true");
137     }else{
138       $smarty->assign("is_terminal","false");
139     }
141     /* Do we need to flip is_account state? */
142     if (isset($_POST['modify_state'])){
143       $this->is_modified = true;
144       $this->is_account= !$this->is_account;
145     }
147     if($this->is_terminal){
148       if ($this->is_account){
149         if($this->type=="station"){
150           $display= $this->show_header(_("Remove printer extension"),
151               _("This workstation has printer extension enabled. You can disable it by clicking below."));
152         }else{
153           $display= $this->show_header(_("Remove printer extension"),
154               _("This terminal has printer extension enabled. You can disable it by clicking below."));
155         }
156       }else{
157         if($this->type=="station"){
158           $display= $this->show_header(_("Add printer extension"),
159               _("This workstation has printer extension disabled. You can enable it by clicking below."));
160         }else{
161           $display= $this->show_header(_("Add printer extension"),
162               _("This terminal has printer extension disabled. You can enable it by clicking below."));
163         }  
164         return ($display);
165       }
166     }
169     /* Do we represent a valid printer? */
170     if (!$this->is_account && $this->parent == NULL){
171       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
172         _("This 'dn' has no printer features.")."</b>";
173       return($display);
174     }
176     /* Fill templating stuff */
177     $smarty->assign("bases", $this->config->idepartments);
178     $smarty->assign("base_select", $this->base);
180     /* Assign attributes */
181     foreach ($this->attributes as $attr){
182       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
183       $smarty->assign("$attr", $this->$attr);
184     }
185     
186     if(isset($_POST['AddUser'])){
187       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
188     }
189     if(isset($_POST['AddGroup'])){
190       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
191     }
192     if(isset($_POST['AddAdminUser'])){
193       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
194     }
195     if(isset($_POST['AddAdminGroup'])){
196       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
197     }
199     if(isset($_POST['PrinterCancel'])){
200       unset($this->dialog);
201       $this->dialog= NULL;
202     }
204     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
205       if(isset($this->member['AddUser'][$_POST['UserMember']])){
206         unset($this->member['AddUser'][$_POST['UserMember']]);
207       }
208     }
210     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
211       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
212         unset($this->member['AddGroup'][$_POST['UserMember']]);
213       }
214     }
216     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
217       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
218         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
219       }
220     }
222     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
223       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
224         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
225       }
226     }
228     if(isset($_POST['PrinterSave'])){
229       $this->dialog->save_object();
230       if(count($this->dialog->check())){
231         foreach($this->dialog->check() as $msg){
232           print_red($msg);
233         }
234       }else{
235         $new = $this->dialog->save();
236         $data = $new;
237         unset($data['type']);
239         if(preg_match("/User/",$new['type'])){
240           $use = "uid";
241         }else{
242           $use = "cn";
243         }
245         foreach($data as $mem){
246           $this->member[$new['type']][$mem[$use][0]]=$mem;    
247         }
248         unset($this->dialog);
249         $this->dialog=NULL; 
250       }
251     }
253     if($this->dialog != NULL){
254       $display = $this->dialog->execute();
255       return $display;
256     }
258     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
259     $smarty->assign("staticAddress", "");
260     $smarty->assign("driverInfo", "Not implemented yet");
262     $list=$this->generateList();
263     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
264     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
266     $smarty->assign("UserMember"    ,$this->UserMember);
267     $smarty->assign("UserMembers"   ,$userlist);
268     $smarty->assign("UserMemberKeys",array_flip($userlist));
270     $smarty->assign("AdminMember"    ,$this->AdminMember);
271     $smarty->assign("AdminMembers"   ,$adminlist);
272     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
273     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
276     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
277   }
279   function remove_from_parent()
280   {
281     $ldap= $this->config->get_ldap_link();
282     $ldap->rmdir($this->dn);
283     show_ldap_error($ldap->get_error());
284     $this->handle_post_events("remove");
286     /* Delete references to object groups */
287     $ldap->cd ($this->config->current['BASE']);
288     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
289     while ($ldap->fetch()){
290       $og= new ogroup($this->config, $ldap->getDN());
291       unset($og->member[$this->dn]);
292       $og->save ();
293     }
294   }
296   /* Save data to object */
297   function save_object()
298   {
299     plugin::save_object();
301     /* Save base, since this is no LDAP attribute */
302     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
303       $this->base= $_POST['base'];
304     }
305   }
307   /* Check supplied data */
308   function check()
309   {
310     $message= array();
311     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
313     /* must: cn */
314     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
315       $message[]= "The required field 'Printer name' is not set.";
316     }
318     $ui= get_userinfo();
319     $acl= get_permissions ($this->dn, $ui->subtreeACL);
320     $acl= get_module_permission($acl, "printer", $this->dn);
321     if (chkacl($acl, "create") != ""){
322       $message[]= _("You have no permissions to create a printer on this 'Base'.");
323     }
325     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
326       $ldap= $this->config->get_ldap_link();
327       $ldap->cd ($this->base);
328       $ldap->search ("(cn=".$this->cn.")", array("cn"));
329       if ($ldap->count() != 0){
330         while ($attrs= $ldap->fetch()){
331           if ($attrs['dn'] != $this->orig_dn){
332             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
333             break;
334           }
335         }
336       }
337     }
339     return ($message);
340   }
343   /* Save to LDAP */
344   function save()
345   {
346     $dn= $this->dn;
347     plugin::save();
348    
349     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
350       $this->dn= preg_replace("/ou=terminal/","ou=printer",$this->dn);
351     }
352  
353     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
354       $this->dn= preg_replace("/ou=workstation/","ou=printer",$this->dn);
355     }
357     $ldap= $this->config->get_ldap_link();
358  
359     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
360     */
361     if($this->is_terminal){
362       
363       /* reduce objectClasses to minimun */
364       $this->attrs['objectClass']= $this->objectclasses;
366       /* If a printer with the given dn exists, modify else create new one */
367       $ldap->cat($this->dn);
368       if($ldap->fetch()){
369         $this->orig_dn= $this->dn;
370       }else{
371         $this->orig_dn = "new";
372       }
373     }
375     /* Remove all empty values */
376     if ($this->orig_dn == 'new'){
377       $attrs= array();
378       foreach ($this->attrs as $key => $val){
379         if (is_array($val) && count($val) == 0){
380           continue;
381         }
382         $attrs[$key]= $val;
383       }
384       $this->attrs= $attrs;
385     }
388     /* Append printer user 
389      */
390     foreach($this->member['AddUser'] as $mem){
391       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
392     }
394     /* Append printer group 
395      */
396     foreach($this->member['AddGroup'] as $mem){
397       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
398     }
400     /* Append printer admin user 
401      */
402     foreach($this->member['AddAdminUser'] as $mem){
403       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
404     }
405     
406     /* Append printer admin group 
407      */
408     foreach($this->member['AddAdminGroup'] as $mem){
409       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
410     }
412     /* Write back to ldap */
413     $ldap= $this->config->get_ldap_link();
414     if ($this->orig_dn == 'new'){
415       $ldap->cd($this->config->current['BASE']);
416       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
417       $ldap->cd($this->dn);
418       $ldap->add($this->attrs);
419       $this->handle_post_events("add");
420     } else {
421       if ($this->orig_dn != $this->dn){
422         $this->move($this->orig_dn, $this->dn);
423       }
425       $ldap->cd($this->dn);
426       $ldap->modify($this->attrs);
427       $this->handle_post_events("modify");
428     }
429     show_ldap_error($ldap->get_error());
431     /* Optionally execute a command after we're done */
432     $this->postcreate();
433   }
435   function generateList(){
436     $a_return=array();
437     foreach($this->member as $type => $values){
438       $a_return[$type]=array();
439       foreach($values as $value){
440         if((preg_match("/Group/i",$type))){
441           if(!isset($value['description'])){
442             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
443           }else{
444             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
445           }
446         }else{
447           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
448         }
449       }
450     }
451     return($a_return);
452   }
459 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
460 ?>