Code

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