Code

Keep PPD server path when editing ppd options.
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_printGeneric.inc
1 <?php
3 class printgeneric extends plugin
4 {
5   /* Generic terminal attributes */
6   var $interfaces     = array();
7   var $ignore_account = FALSE;
9   /* Needed values and lists */
10   var $base             = "";
11   var $cn               = "";
12   var $l                = "";
13   var $description      = "";
14   var $labeledURI       = "";
15   var $gotoPrinterPPD   = "";
16   var $initial_PPD      = "";
17   var $orig_dn          = "";
18   var $orig_cn          = "";
19   var $orig_base        = "";
21   var $UserMember       ="";
22   var $UserMembers      =array();
23   var $UserMemberKeys   =array();
25   var $AdminMember      ="";
26   var $AdminMembers     =array();
27   var $AdminMemberKeys  =array();
29   var $ppdServerPart   = "";
31   var $PPDdialogToSave  = NULL;
32   var $BelongsTo        = "unknown"; //  Specifies if this is a standalone printer, or belongs to a terminal / WS
34   var $member           =array();
35   var $strings          = "";
36   var $netConfigDNS;
37   var $baseSelection    = false;
38   var $macAddress       = "";
40   var $gotoUserAdminPrinter;
41   var $gotoGroupAdminPrinter ;
42   var $gotoGroupPrinter;
43   var $gotoUserPrinter ;
45   /* attribute list for save action */
46   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress", 
47                               "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter","gosaUnitTag");
48   var $objectclasses  = array("top", "gotoPrinter");
49   var $view_logged    = FALSE;
50   var $parent;
52   function printgeneric (&$config, $dn,$parent_init,$parent)
53   {
54     $this->config = &$config;
55     $this->dn = $dn; 
56  
57     /* If parent was posted(the tabs object) we can detect the printer type. */
58     if($parent){
59       $this->parent = $parent;
60       $this->getTypeOfPrinter();
61     }else{
62       $this->BelongsTo = "unknown";
63       return;
64     }
66     /* Update dn, to ensure storing as printer instead of WS / terminal */
67     if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
68       $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
69     }
71     if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
72       $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
73     }
75     $this->orig_dn = $this->dn;
77     /* Get printer settings, possibly dn has changed */
78     plugin::plugin ($config, $this->dn);
80     /* Get is_account initially_was_account status */
81     $this->getTypeOfPrinter(true);
83     /* set orig dn to new if object is new */
84     $ldap= $this->config->get_ldap_link();
85     $ldap->cat($this->dn, array('dn'));
86     if(!$ldap->count()){
87       $this->orig_dn = "new";
88     }
89     
90     /* create dns object */
91     $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
93     /* Set base */
94     if ($this->dn == "new"){
95       $ui= get_userinfo();
96       $this->base= dn2base($ui->dn);
97       $this->cn= "";
98     } else {
99     
100       /* Set base and check if the extracted base exists */
101       if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/",$this->dn)){
102         $this->base= preg_replace("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/","",dn2base($this->dn));
103       }else{
104         $this->base= preg_replace("/".preg_quote(get_ou('printerRDN'), '/')."/","",dn2base($this->dn));
105       }
107       if(!isset($this->config->idepartments[$this->base])){
108         msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
109         $this->base  = session::get('CurrentMainBase');
110       }
111     }
113     /* If no ppd is selected, remove this attribute */
114     if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
115       $this->ppdServerPart = preg_replace("/^(http.*ppd)\/.*$/i","\\1",$this->gotoPrinterPPD);
116     }else{
118       /* Detect PPD server part */
119       if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
120         $method="https://";
121       }else{
122         $method="http://";
123       }
124      
125       /* Get servername */
126       $server = $_SERVER['SERVER_NAME'];
127       if(tests::is_ip($server)){  
128         $server_name = @gethostbyaddr($server);
129       }else{
130         $server_name = @gethostbyaddr(gethostbyname($server));
131       }
132       $this->ppdServerPart  = $method.str_replace("//","/",$server_name."/ppd");
133     }
135     /* Extract selected ppd */
136     if(isset($this->gotoPrinterPPD)){
137       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
138     }
140     $this->initial_PPD = $this->gotoPrinterPPD;
142     /* Prepare different member types */ 
143     foreach(array("AddUser"       =>"gotoUserPrinter",
144           "AddGroup"      =>"gotoGroupPrinter",
145           "AddAdminUser"  =>"gotoUserAdminPrinter",
146           "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
148       /* $this->members contains all members */
149       $this->member[$type]=array();
151       if(isset($this->attrs[$attr])){
152         $ldap->cd($this->config->current['BASE']) ;
153         for($i = 0 ;  $i < $this->attrs[$attr]['count']; $i++){
154         
155           $mem = $this->attrs[$attr][$i];
156           if(preg_match("/Group/",$type)){
157             $ldap->search("(&(|(objectClass=posixGroup)(objectClass=gosaGroupOfNames))(cn=".$mem."))",array("cn","description"));
158             if($ldap->count()){
159               $entry = $ldap->fetch();
160               $this->member[$type][$entry['cn'][0]]=$entry;
161             }
162           }else{
163             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
164             if($ldap->count()){
165               $entry = $ldap->fetch();
166               $this->member[$type][$entry['uid'][0]]=$entry;
167             }
168           }
169         }
170       }
171     }
172     $this->orig_cn    = $this->cn;
173     $this->orig_base  = $this->base;
174   }
176   function set_acl_base($base)
177   {
178     plugin::set_acl_base($base);
179     if(is_object($this->netConfigDNS)){
180       $this->netConfigDNS->set_acl_base($base);
181     }
182   }
184   function set_acl_category($cat)
185   {
186     plugin::set_acl_category($cat);
187     if(is_object($this->netConfigDNS)){
188       $this->netConfigDNS->set_acl_category($cat);
189     }
190   }
192   /* Detect type of printer.
193    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
194    * We can detect the type printer type when comparing the tabs objects
195    */
196   function getTypeOfPrinter($UpdateAccountStatus = false)
197   {
198     /* Disable account as default
199      */  
200     $this->is_account = $this->initially_was_account = false;
202     /* Detect type of printer via parent tabs.
203      */
205     $class = get_class($this->parent);
206     if(isset($this->parent->by_object['workgeneric'])){
208       /* Exclude templates 
209        */
210       $this->cn = $this->parent->by_object['workgeneric']->cn;
211       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
212         $this->BelongsTo = "WorkstationTemplate";
213       }else{
214         $this->BelongsTo = "Workstation";
215       }
216     }elseif(isset($this->parent->by_object['termgeneric'])){
218       /* Exclude templates 
219        */
220       $this->cn = $this->parent->by_object['termgeneric']->cn;
221       if($this->parent->by_object['termgeneric']->cn == "default"){
222         $this->BelongsTo = "TerminalTemplate";  
223       }else{
224         $this->BelongsTo = "Terminal";
225       }
226     }elseif(isset($this->parent->by_name['printgeneric'])){
227       $this->BelongsTo  = "Printer";
228     }
230     if($UpdateAccountStatus){
232       /* Set is_account / was account 
233        */
234       if($this->dn == "new"){
235         $this->initially_was_account = false;
236       }
238       /* If is printer it must be a true account.
239        */
240       if(preg_match("/printer/i",$this->BelongsTo)){
241         $this->is_account = true;
242       }
244       /* Update dn, to ensure storing as printer instead of WS / terminal
245        */
246       if(preg_match("/terminal/i",$this->BelongsTo)){
247         $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
248       }
250       if(preg_match("/workstation/i",$this->BelongsTo)){
251         $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
252       }
254       /* Detect if this is a valid printer account;
255        */
256       $ldap = $this->config->get_ldap_link();
257       $ldap->cat($this->dn, array('objectClass'));
259       if($ldap->count()){
260         $attrs = $ldap->fetch();
261         if(in_array("gotoPrinter",$attrs['objectClass'])){
262           $this->initially_was_account = true;
263           $this->is_account             = true;
264         }else{
265           $this->is_account = false;
266         }
267       }
268     }
269   }
271   function execute()
272   {
273     /* Call parent execute */
274     plugin::execute();
276     if($this->is_account && !$this->view_logged){
277       $this->view_logged = TRUE;
278       new log("view","printer/".get_class($this),$this->dn);
279     }
281     /* If type of printer couldn't be detected (because of missing parent object in construction) 
282      * hide this tab.
283      */
284     if(preg_match("/unknown/i",$this->BelongsTo)){
285       $display= $this->show_enable_header(_("Add printer extension"),
286           _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
287       return($display);
288     }
290     /* Templates can't have printer extensions 
291      */
292     if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
293       $display= $this->show_enable_header(_("Add printer extension"),
294           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
295       return($display);
296     }
297     if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
298       $display= $this->show_enable_header(_("Add printer extension"),
299           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
300       return($display);
301     }
303     /* Get cn from base object */
304     if(preg_match("/^Workstation$/i",$this->BelongsTo)){
305       $this->cn = $this->parent->by_object['workgeneric']->cn;
306     }
307     if(preg_match("/^Terminal$/i",$this->BelongsTo)){
308       $this->cn = $this->parent->by_object['termgeneric']->cn;
309     }
311     $smarty= get_smarty();
313     /* Assign acls */
314     $tmp = $this->plInfo();
315     foreach($tmp['plProvidedAcls'] as $name => $translation){
316       $smarty->assign($name."ACL", $this->getacl($name));
317     }
319     $display="";
321     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
322     if(preg_match("/^Printer$/i",$this->BelongsTo)){    
323       $smarty->assign("StandAlone",true);
324     }else{
325       $smarty->assign("StandAlone",false);
326     }
328     /* Do we need to flip is_account state? */
329     if(isset($_POST['modify_state'])){
330       if($this->is_account && $this->acl_is_removeable()){
331         $this->is_account= FALSE;
332       }elseif(!$this->is_account && $this->acl_is_createable()){
333         $this->is_account= TRUE;
334       }
335     }
337     /* Do we represent a valid printer? */
338     if (!$this->is_account && $this->parent === NULL){
339       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
340         msgPool::noValidExtension(_("printer"))."</b>";
341       return($display);
342     }
344     /* If this is a WS / Terminal depending printer, display account state button */
345     if(!preg_match("/^Printer$/i",$this->BelongsTo)){
346       if($this->cn == "" && ($this->dn != "new")){
347         $display= $this->show_enable_header(_("Add printer extension"),
348             msgPool::featuresDisabled(_("printer"))._("You can't enable it while 'cn' is not present in entry. Possibly you are currently creating a new terminal template."),TRUE,TRUE);
349         $this->is_account= false;
350         return $display;
351       }
353       if (($this->is_account)){
354         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
355           $display= $this->show_disable_header(_("Remove printer extension"),
356               msgPool::featuresEnabled(_("printer")));
357         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
358           $display= $this->show_disable_header(_("Remove printer extension"),
359               msgPool::featuresDisabled(_("printer")));
360         }
361       }else{
362         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
363           $display= $this->show_enable_header(_("Add printer extension"),
364               msgPool::featuresEnabled(_("printer")));
365         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
366           $display= $this->show_enable_header(_("Add printer extension"),
367               msgPool::featuresDisabled(_("printer")));
368         }  
369         return ($display);
370       }
371     }
373     /* Base select dialog */
374     $once = true;
375     foreach($_POST as $name => $value){
376       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
377         $once = false;
378         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
379         $this->dialog->setCurrentBase($this->base);
380         $this->baseSelection = true;
381       }
382     }
384     /* Dialog handling for base select dialog
385      * Check if base was selected, dialog aborted etc */
386     if(is_object($this->dialog)){
388       $this->dialog->save_object();
389       if($this->baseSelection){
390         if($this->dialog->isClosed()){
391           $this->dialog = false;
392           $this->baseSelection = false;
393         }elseif($this->dialog->isSelected()){
395           /* A new base was selected, check if it is a valid one */
396           $tmp = $this->get_allowed_bases();
397           if(isset($tmp[$this->dialog->isSelected()])){
398             $this->base = $this->dialog->isSelected();
399           }
401           $this->dialog= false;
402           $this->baseSelection = false;
403         }else{
404           return($this->dialog->execute());
405         }
406       }
407     }
409     /* Fill templating stuff */
410     $smarty->assign("bases", $this->get_allowed_bases());
411     $smarty->assign("base_select", $this->base);
413     /* Assign attributes */
414     foreach ($this->attributes as $attr){
415       $smarty->assign("$attr", $this->$attr);
416     }
418     if(isset($_POST['AddUser'])){
419       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser",$this->member);
420     }
421     if(isset($_POST['AddGroup'])){
422       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup",$this->member);
423     }
424     if(isset($_POST['AddAdminUser'])){
425       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser",$this->member);
426     }
427     if(isset($_POST['AddAdminGroup'])){
428       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup",$this->member);
429     }
431     /* Display ppd configure/select dialog      */
432     if(isset($_POST['EditDriver'])){
433       if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
434         $this->dialog = $this->PPDdialogToSave;
435       }else{
436   
437         if(is_array($this->gotoPrinterPPD)){
438           $this->dialog = new printerPPDDialog($this->config, $this->dn,"");
439         }else{
440           $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
441         }
442         $this->dialog->cn= $this->cn;
443       }
444     }
446     /* remove ppd */
447     if(isset($_POST['RemoveDriver'])){
448       /* Detect PPD server part */
449       if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
450         $method="https://";
451       }else{
452         $method="http://";
453       }
454      
455       /* Get servername */
456       $server = $_SERVER['SERVER_NAME'];
457       if(tests::is_ip($server)){  
458         $server_name = @gethostbyaddr($server);
459       }else{
460         $server_name = @gethostbyaddr(gethostbyname($server));
461       }
462       $this->ppdServerPart  = $method.str_replace("//","/",$server_name."/ppd");
463       $this->gotoPrinterPPD = array();
464       $this->PPDdialogToSave = NULL;
465     }
467     /* Close ppd dialog */
468     if(isset($_POST['ClosePPD'])){
469       unset($this->dialog);
470       $this->dialog=FALSE;
471     }
473     /* Save selected ppd */
474     if(isset($_POST['SavePPD'])){
475       $this->dialog->save_object();
476       if(count($this->dialog->check())){
477         foreach($this->dialog->check() as $msg){
478           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
479         }
480       }else{
481         $this->gotoPrinterPPD = array();
482         $this->gotoPrinterPPD = $this->dialog->save();
483         $this->PPDdialogToSave = $this->dialog;
484         unset($this->dialog);
485         $this->dialog=FALSE;
486       }
487     }
489      /* Member management, delete user / group / admin ..*/
490     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
491       foreach($_POST['UserMember'] as $mem){
492         $this->DelMember('AddUser',$mem);
493       }
494     }
496     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
497       foreach($_POST['UserMember'] as $mem){
498         $this->DelMember('AddGroup',$mem);
499       }
500     }
502     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
503       foreach($_POST['AdminMember'] as $mem){
504         $this->DelMember('AddAdminUser',$mem);
505       }
506     }
508     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
509       foreach($_POST['AdminMember'] as $mem){
510         $this->DelMember('AddAdminGroup',$mem);
511       }
512     }
514     /* Abort user / group adding dialog */
515     if(isset($_POST['PrinterCancel'])){
516       unset($this->dialog);
517       $this->dialog= FALSE;
518     }
520     /* Save selected users / groups */
521     if(isset($_POST['PrinterSave'])){
522       $this->dialog->save_object();
523       if(count($this->dialog->check())){
524         foreach($this->dialog->check() as $msg){
525           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
526         }
527       }else{
528         $data= $new = $this->dialog->save();
529         unset($data['type']);
530         foreach($data as $mem){
531           $this->AddMember($new['type'], $mem['dn']);
532         }
533         unset($this->dialog);
534         $this->dialog=FALSE;
535       }
536     }
538     /* Display dialog, if there is currently one open*/
539     if(is_object($this->dialog)){
540       $this->dialog->save_object();
541       $display = $this->dialog->execute();
542       return $display;
543     }
545     /* Parse selected ppd file */
546     $config = session::get('config');
547     if ($config->get_cfg_value("ppdPath") != ""){
548       $path = $config->get_cfg_value("ppdPath");
549       if(!preg_match("/\/$/",$path)){
550         $path = $path."/";
551       }
553       $ppdManager= new ppdManager($path);
554       $smarty->assign("displayServerPath",true);
555       if(!empty($this->gotoPrinterPPD)){
556         if((!file_exists($path.$this->gotoPrinterPPD))){
557           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
558         }else{
559           $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD);
560           $smarty->assign("driverInfo", $ppdDesc['name']);
561         }
562       }else{
563         $smarty->assign("driverInfo", _("Not defined"));
564         $smarty->assign("displayServerPath",false);
565       }
566     }else{
567       $smarty->assign("driverInfo",_("Can't get ppd informations."));
568       $smarty->assign("displayServerPath",true);
569     }
570     $smarty->assign("ppdServerPart",$this->ppdServerPart);
571     
573     /* Create user & admin user list */
574     $list=$this->generateList();
575     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
576     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
578     asort($userlist);
579     asort($adminlist);
581     if(!preg_match("/Printer/i",$this->BelongsTo)){
582       if(preg_match("/Terminal/i",$this->BelongsTo)){
583         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
584       }else{
585         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
586       }
587       $smarty->assign("cnACL"    , $this->getacl("cn",true));
588     }else{
589       $smarty->assign("desc"    ,"");
590     }
591     $smarty->assign("UserMember"    ,$this->UserMember);
592     $smarty->assign("UserMembers"   ,$userlist);
593     $smarty->assign("UserMemberKeys",array_flip($userlist));
595     $smarty->assign("AdminMember"    ,$this->AdminMember);
596     $smarty->assign("AdminMembers"   ,$adminlist);
597     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
598     if(preg_match("/Printer/i",$this->BelongsTo)){
600       /* Show main page */
601       $str = $this->netConfigDNS->execute();
602       if(is_object($this->netConfigDNS->dialog)){
603         return($str);
604       }
605       $smarty->assign("netconfig", $str);
606     } else {
607       $smarty->assign("netconfig", "");
608     }
610     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
611   }
613   function remove_from_parent()
614   {
615     /* Only remove if there was initially an account */
616     if($this->initially_was_account){
618       /* Update dn, to ensure storing as printer instead of WS / terminal
619        */
620       if(preg_match("/terminal/i",$this->BelongsTo)){
621         $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/').",/",get_ou('printerRDN'),$this->dn);
622       }
624       if(preg_match("/workstation/i",$this->BelongsTo)){
625         $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
626       }
628       /* Check if this dn points to a printer, to avoid deleting something else */
629       $ldap= $this->config->get_ldap_link();
630       $ldap->cat($this->dn, array('dn',"objectClass"));
631       if(!$ldap->count()){
632         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
633         return;
634       }
636       /* Check if obejct is a printer */
637       $CheckPrinter = $ldap->fetch();
638       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
639         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
640         return;
641       }
643       /* Remove account & dns extension */ 
644       $this->netConfigDNS->remove_from_parent();
645       $ldap->rmdir($this->dn);
647       new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
648   
649       if (!$ldap->success()){
650         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
651       }
652       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
654       /* Delete references to object groups */
655       $ldap->cd ($this->config->current['BASE']);
656       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
657       while ($ldap->fetch()){
658         $og= new ogroup($this->config, $ldap->getDN());
659         unset($og->member[$this->dn]);
660         $og->save ();
661       }
663       /* Remove previously selected ppd file.*/
664       if(!empty($this->initial_PPD)){
665         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
666         $tmp->removeModifiedPPD();
667       }
668     }
669   }
672   /* Save data to object */
673   function save_object()
674   {
675     /* Create a base backup and reset the
676        base directly after calling plugin::save_object();
677        Base will be set seperatly a few lines below */
678     $base_tmp = $this->base;
679     plugin::save_object();
680     $this->base = $base_tmp;
682     if(isset($_POST['ppdServerPart'])){
683       $this->ppdServerPart = get_post('ppdServerPart');
684     }
686     if(is_object($this->netConfigDNS)){
687       $this->netConfigDNS->save_object();
688     }
689     
690     /* Set new base if allowed */
691     $tmp = $this->get_allowed_bases();
692     if(isset($_POST['base'])){
693       if(isset($tmp[$_POST['base']])){
694         $this->base= $_POST['base'];
695       }
696     }
697   }
699   /* Check supplied data */
700   function check()
701   {
702     /* Call common method to give check the hook */
703     $message= plugin::check();
704     if (preg_match("/printer/i",$this->BelongsTo)){
705       $message= array_merge($message, $this->netConfigDNS->check());
706     }
708     /* Don't display check messages if this is a template object */
709     if(isset($this->parent->by_object['workgeneric'])){
710       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
711         return $message;
712       }
713     }elseif(isset($this->parent->by_object['termgeneric'])){
714       if($this->parent->by_object['termgeneric']->cn == "default"){
715         return $message;
716       }
717     }
719     $dn= "cn=".$this->cn.get_ou('printerRDN').",".$this->base;
721     /* must: cn */
722     if(($this->BelongsTo == "Printer") && $this->cn == ""){
723       $message[]= msgPool::required(_("Name"));
724     }
726     /* must: cn */
727     if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
728       $message[]= msgPool::invalid(_("Name"));
729     }
731     /* must: labeledURI */
732     if(empty($this->labeledURI)){
733       $message[]= msgPool::required(_("Printer URL"));
734     }
735     
736     /* Check if there is already an entry with this cn*/
737     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
738       $ldap= $this->config->get_ldap_link();
739       $ldap->cd ($this->base);
740       $ldap->ls("(cn=".$this->cn.")",get_ou('printerRDN').$this->base, array("cn"));
741       if ($ldap->count() != 0){
742         while ($attrs= $ldap->fetch()){
743           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
744             continue;
745           }
746           if ($attrs['dn'] != $this->orig_dn){
747             $message[]= msgPool::duplicated(_("Name"));
748             break;
749           }
750         }
751       }
752     }
754     /* Check if we are allowed to create or move this object
755      */
756     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
757       $message[] = msgPool::permCreate();
758     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
759       $message[] = msgPool::permMove();
760     }
762     return ($message);
763   }
766   /* Save to LDAP */
767   function save()
768   {
769     /* Update dn, to ensure storing as printer instead of WS / terminal
770      */
771     if(preg_match("/terminal/i",$this->BelongsTo)){
772       $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
773     }
775     if(preg_match("/workstation/i",$this->BelongsTo)){
776       $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
777     }
778     
779     if(!$this->is_account) return;
780     if(isset($this->parent->by_object['workgeneric'])){
781       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
782         return;
783       }
785       /* Adapt IP & mac from parent object */
786       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
787       $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
789     }elseif(isset($this->parent->by_object['termgeneric'])){
790       if($this->parent->by_object['termgeneric']->cn == "default"){
791         return;
792       }
793     
794       /* Adapt IP & mac from parent object */
795       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
796       $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
797     }
799     /* If type is still unknown, the initialisation of this printer failed, abort. */
800     if(preg_match("/unknown/i",$this->BelongsTo)){
801       return;
802     }
804     /* save ppd configuration */
805     if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
806       $this->PPDdialogToSave->save_ppd();
807     }
808     if($this->orig_dn != $this->dn){
809       if(!empty($this->gotoPrinterPPD)) {
810         $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
811         $this->PPDdialogToSave->cn = $this->cn;
812         $this->PPDdialogToSave->generateProperties();
813         $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
814       }
815     }
817     /* Remove previously selected ppd file.*/
818     if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
819       if(!empty($this->initial_PPD)){
820         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
821         $tmp->removeModifiedPPD();
822       }
823     }
825     /* If no ppd is selected, remove this attribute */
826     if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
827       $this->gotoPrinterPPD = $this->ppdServerPart.'/'.$this->gotoPrinterPPD;
828     }else{
829       $this->gotoPrinterPPD = array();
830     }
832     $dn= $this->dn;
834     /* reduce objectClasses to minimun */
835     $this->attrs['objectClass']= $this->objectclasses;
837     plugin::save();
838     $ldap= $this->config->get_ldap_link();
840     /* Remove all empty values */
841     if ($this->orig_dn == 'new'){
842       $attrs= array();
843       foreach ($this->attrs as $key => $val){
844         if (is_array($val) && count($val) == 0){
845           continue;
846         }
847         $attrs[$key]= $val;
848       }
849       $this->attrs= $attrs;
850     }
852     /* Append printer user 
853      */
854     $this->attrs['gotoUserPrinter']=array();
855     foreach($this->member['AddUser'] as $mem){
856       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
857     }
859     /* Append printer group 
860      */
861     $this->attrs['gotoGroupPrinter'] = array();
862     foreach($this->member['AddGroup'] as $mem){
863       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
864     }
866     /* Append printer admin user 
867      */
868     $this->attrs['gotoUserAdminPrinter'] = array();
869     foreach($this->member['AddAdminUser'] as $mem){
870       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
871     }
873     /* Append printer admin group 
874      */
875     $this->attrs['gotoGroupAdminPrinter']= array();
876     foreach($this->member['AddAdminGroup'] as $mem){
877       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
878     }
880     if($this->orig_dn == 'new'){
881       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
882         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
883           unset($this->attrs[$checkVar]);
884         }
885       }
886     }
888     /* Ensure to create a new object */
889     if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/",$this->orig_dn)){
890       $this->orig_dn = "new";
891     }
893     /* Move object in necessary*/
894     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
895       $this->move($this->orig_dn, $this->dn);
896     }
898     /* Write back to ldap */
899     $ldap= $this->config->get_ldap_link();
900     $ldap->cat($this->dn);
901     if(!$ldap->count()){
902       $ldap->cd($this->config->current['BASE']);
903       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
904       $ldap->cd($this->dn);
906       /* Remove empty values */ 
907       foreach($this->attrs as $name => $value){
908         if(empty($value)){
909           unset($this->attrs[$name]);
910         }
911       }
913       $ldap->add($this->attrs);
914       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
915       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
916     } else {
917       $ldap->cd($this->dn);
918       $this->cleanup();
919       $ldap->modify ($this->attrs); 
920       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
921       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
922     }
923     if (!$ldap->success()){
924       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
925     }
927     #if(preg_match("/printer/i",$this->BelongsTo)){
928       $this->netConfigDNS->cn = $this->cn;
929       $this->netConfigDNS->dn = $this->dn;
930       $this->netConfigDNS->save();
931     #}
932   }
934   function generateList(){
935     $a_return=array();
937     foreach($this->member as $type => $values){
938       $a_return[$type]=array();
939       foreach($values as $value){
940         if((preg_match("/Group/i",$type))){
941           if(!isset($value['description'])){
942             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
943           }else{
944             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
945           }
946         }else{
947           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
948         }
949       }
950     }
951     return($a_return);
952   }
954   /* Return plugin informations for acl handling
955       #FIXME FAIscript seams to ununsed within this class... */
956   static function plInfo()
957   {
958     return (array(
959           "plShortName"   => _("Generic"),
960           "plDescription" => _("Print generic"),
961           "plSelfModify"  => FALSE,
962           "plDepends"     => array(),
963           "plPriority"    => 4,
964           "plSection"     => array("administration"),
965           "plCategory"    => array("printer" => array("description"  => _("Printer"),
966                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
967           "plProvidedAcls"=> array(
968             "cn"                => _("Name"),
969             "base"                => _("Base") ,         
970             "description"       => _("Description"), 
971             "l"                 => _("Location"), 
972             "labeledURI"        => _("LabeledURL"), 
973             "gotoPrinterPPD"    => _("Printer PPD"),
974             "gotoUserPrinter"   => _("Permissions")) 
975           ));
976   }
979   /* Delete member */
980   function DelMember($type,$id)
981   {
982     /* Check if there was a printer "dn" given, or the "cn" */
983     foreach($this->member[$type] as $key => $printer){
984       if($printer['dn'] == $id) {
985         $id = $key;
986       }
987     }
988   
989     if(!$this->acl_is_writeable("gotoUserPrinter")){
990       msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
991       return(FALSE);
992     }
993  
994     if(isset($this->member[$type][$id])){
995       unset($this->member[$type][$id]);
996       return(TRUE);
997     }
998     return(FALSE);
999   }
1002   /* Add given obejct to members */
1003   function AddMember($type,$dn)
1004   {
1005     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
1006     if(!in_array_ics($type, $types)){
1007       msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
1008       return(FALSE);
1009     }
1011     if(!$this->acl_is_writeable("gotoUserPrinter")){
1012       msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
1013       return(FALSE);
1014     }
1016     /* Get name of index attributes */
1017     if(preg_match("/user/i",$type)){
1018       $var = "uid";
1019     }else{
1020       $var = "cn";
1021     }
1023     $ldap = $this->config->get_ldap_link();
1024     $ldap->cd($dn);
1025     $ldap->cat($dn,array($var,"cn"));
1026     if($ldap->count()){
1028       $attrs = $ldap->fetch();
1030       if(isset($attrs[$var][0])){
1031         $name = $attrs[$var][0];
1033         /* Check if this uid/cn is already assigned to any permission */
1034         foreach($types as $ctype){
1036           /* If we want to add a user, only check user/userAdmin members */
1037           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
1038             continue;
1039           }
1041           /* If we want to add a group, only check groups/adminGroups .. */
1042           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1043             continue;
1044           }
1046           if(isset(  $this->member[$ctype][$name])){
1047             msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1048             return(FALSE);
1049           }
1050         }
1052         /* Everything is fine. So add the given object to members */
1053         $this->member[$type][$attrs[$var][0]] = $attrs ;
1054       }else{
1055         print_a($attrs);
1056       }
1057     }else{
1058       msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1059       return(FALSE);
1060     }
1061     return(TRUE);
1062   }
1065    /* Display generic part for server copy & paste */
1066   function getCopyDialog()
1067   {
1068     $vars = array("cn");
1069     $smarty = get_smarty();
1070     $smarty->assign("cn" ,$this->cn);
1071     $smarty->assign("object","printer");
1072     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1073     $ret = array();
1074     $ret['string'] = $str;
1075     $ret['status'] = "";
1076     return($ret);
1077   }
1080   function saveCopyDialog()
1081   {
1082     if(isset($_POST['cn'])){
1083       $this->cn = $_POST['cn'];
1084     }
1085   }
1087   function PrepareForCopyPaste($source)
1088   {
1089     plugin::PrepareForCopyPaste($source);
1090     if(isset($source['macAddress'][0])){
1091       $this->netConfigDNS->macAddress = $source['macAddress'][0];
1092     }
1093     if(isset($source['ipHostNumber'][0])){
1094       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1095     }
1097     $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1098     foreach($this->attributes as $attr){
1099       $this->$attr = $source_o->$attr;
1100     }
1101     $this->member = $source_o -> member;
1103     $this->gotoPrinterPPD = "";
1104   }
1107 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1108 ?>