Code

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