Code

Updated a bunch of files for broken dn sensitivity
[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'), '/')."/i",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'), '/')."/i",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'), '/')."/i",$this->dn)){
102         $this->base= preg_replace("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i","",dn2base($this->dn));
103       }else{
104         $this->base= preg_replace("/".preg_quote(get_ou('printerRDN'), '/')."/i","",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'), '/')."/i",get_ou('printerRDN'),$this->dn);
248       }
250       if(preg_match("/workstation/i",$this->BelongsTo)){
251         $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",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       }
445       // Detect edit acl base
446       $ldap = $this->config->get_ldap_link();
447       if($ldap->dn_exists($this->dn)){
448         $acl_base = $this->dn;
449       }else{
450         $acl_base = $this->base;
451       }
453       $this->dialog->set_acl_base($acl_base);
454     }
456     /* remove ppd */
457     if(isset($_POST['RemoveDriver'])){
458       /* Detect PPD server part */
459       if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
460         $method="https://";
461       }else{
462         $method="http://";
463       }
464      
465       /* Get servername */
466       $server = $_SERVER['SERVER_NAME'];
467       if(tests::is_ip($server)){  
468         $server_name = @gethostbyaddr($server);
469       }else{
470         $server_name = @gethostbyaddr(gethostbyname($server));
471       }
472       $this->ppdServerPart  = $method.str_replace("//","/",$server_name."/ppd");
473       $this->gotoPrinterPPD = array();
474       $this->PPDdialogToSave = NULL;
475     }
477     /* Close ppd dialog */
478     if(isset($_POST['ClosePPD'])){
479       unset($this->dialog);
480       $this->dialog=FALSE;
481     }
483     /* Save selected ppd */
484     if(isset($_POST['SavePPD'])){
485       $this->dialog->save_object();
486       if(count($this->dialog->check())){
487         foreach($this->dialog->check() as $msg){
488           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
489         }
490       }else{
491         $this->gotoPrinterPPD = array();
492         $this->gotoPrinterPPD = $this->dialog->save();
493         $this->PPDdialogToSave = $this->dialog;
494         unset($this->dialog);
495         $this->dialog=FALSE;
496       }
497     }
499      /* Member management, delete user / group / admin ..*/
500     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
501       foreach($_POST['UserMember'] as $mem){
502         $this->DelMember('AddUser',$mem);
503       }
504     }
506     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
507       foreach($_POST['UserMember'] as $mem){
508         $this->DelMember('AddGroup',$mem);
509       }
510     }
512     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
513       foreach($_POST['AdminMember'] as $mem){
514         $this->DelMember('AddAdminUser',$mem);
515       }
516     }
518     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
519       foreach($_POST['AdminMember'] as $mem){
520         $this->DelMember('AddAdminGroup',$mem);
521       }
522     }
524     /* Abort user / group adding dialog */
525     if(isset($_POST['PrinterCancel'])){
526       unset($this->dialog);
527       $this->dialog= FALSE;
528     }
530     /* Save selected users / groups */
531     if(isset($_POST['PrinterSave'])){
532       $this->dialog->save_object();
533       if(count($this->dialog->check())){
534         foreach($this->dialog->check() as $msg){
535           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
536         }
537       }else{
538         $data= $new = $this->dialog->save();
539         unset($data['type']);
540         foreach($data as $mem){
541           $this->AddMember($new['type'], $mem['dn']);
542         }
543         unset($this->dialog);
544         $this->dialog=FALSE;
545       }
546     }
548     /* Display dialog, if there is currently one open*/
549     if(is_object($this->dialog)){
550       $this->dialog->save_object();
551       $display = $this->dialog->execute();
552       return $display;
553     }
555     /* Parse selected ppd file */
556     $config = session::get('config');
557     if ($config->get_cfg_value("ppdPath") != ""){
558       $path = $config->get_cfg_value("ppdPath");
559       if(!preg_match("/\/$/",$path)){
560         $path = $path."/";
561       }
563       $ppdManager= new ppdManager($path);
564       $smarty->assign("displayServerPath",true);
565       if(!empty($this->gotoPrinterPPD)){
566         if((!file_exists($path.$this->gotoPrinterPPD))){
567           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
568         }else{
569           $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD);
570           $smarty->assign("driverInfo", $ppdDesc['name']);
571         }
572       }else{
573         $smarty->assign("driverInfo", _("Not defined"));
574         $smarty->assign("displayServerPath",false);
575       }
576     }else{
577       $smarty->assign("driverInfo",_("Can't get ppd informations."));
578       $smarty->assign("displayServerPath",true);
579     }
580     $smarty->assign("ppdServerPart",$this->ppdServerPart);
581     
583     /* Create user & admin user list */
584     $list=$this->generateList();
585     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
586     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
588     asort($userlist);
589     asort($adminlist);
591     if(!preg_match("/Printer/i",$this->BelongsTo)){
592       if(preg_match("/Terminal/i",$this->BelongsTo)){
593         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
594       }else{
595         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
596       }
597       $smarty->assign("cnACL"    , $this->getacl("cn",true));
598     }else{
599       $smarty->assign("desc"    ,"");
600     }
601     $smarty->assign("UserMember"    ,$this->UserMember);
602     $smarty->assign("UserMembers"   ,$userlist);
603     $smarty->assign("UserMemberKeys",array_flip($userlist));
605     $smarty->assign("AdminMember"    ,$this->AdminMember);
606     $smarty->assign("AdminMembers"   ,$adminlist);
607     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
608     if(preg_match("/Printer/i",$this->BelongsTo)){
610       /* Show main page */
611       $str = $this->netConfigDNS->execute();
612       if(is_object($this->netConfigDNS->dialog)){
613         return($str);
614       }
615       $smarty->assign("netconfig", $str);
616     } else {
617       $smarty->assign("netconfig", "");
618     }
620     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
621   }
623   function remove_from_parent()
624   {
625     /* Only remove if there was initially an account */
626     if($this->initially_was_account){
628       /* Update dn, to ensure storing as printer instead of WS / terminal
629        */
630       if(preg_match("/terminal/i",$this->BelongsTo)){
631         $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/').",/i",get_ou('printerRDN'),$this->dn);
632       }
634       if(preg_match("/workstation/i",$this->BelongsTo)){
635         $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
636       }
638       /* Check if this dn points to a printer, to avoid deleting something else */
639       $ldap= $this->config->get_ldap_link();
640       $ldap->cat($this->dn, array('dn',"objectClass"));
641       if(!$ldap->count()){
642         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
643         return;
644       }
646       /* Check if obejct is a printer */
647       $CheckPrinter = $ldap->fetch();
648       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
649         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
650         return;
651       }
653       /* Remove account & dns extension */ 
654       $this->netConfigDNS->remove_from_parent();
655       $ldap->rmdir($this->dn);
657       new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
658   
659       if (!$ldap->success()){
660         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
661       }
662       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
664       /* Delete references to object groups */
665       $ldap->cd ($this->config->current['BASE']);
666       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
667       while ($ldap->fetch()){
668         $og= new ogroup($this->config, $ldap->getDN());
669         unset($og->member[$this->dn]);
670         $og->save ();
671       }
673       /* Remove previously selected ppd file.*/
674       if(!empty($this->initial_PPD)){
675         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
676         $tmp->removeModifiedPPD();
677       }
678     }
679   }
682   /* Save data to object */
683   function save_object()
684   {
685     /* Create a base backup and reset the
686        base directly after calling plugin::save_object();
687        Base will be set seperatly a few lines below */
688     $base_tmp = $this->base;
689     plugin::save_object();
690     $this->base = $base_tmp;
692     if(isset($_POST['ppdServerPart'])){
693       $this->ppdServerPart = get_post('ppdServerPart');
694     }
696     if(is_object($this->netConfigDNS)){
697       $this->netConfigDNS->save_object();
698     }
699     
700     /* Set new base if allowed */
701     $tmp = $this->get_allowed_bases();
702     if(isset($_POST['base'])){
703       if(isset($tmp[$_POST['base']])){
704         $this->base= $_POST['base'];
705       }
706     }
707   }
709   /* Check supplied data */
710   function check()
711   {
712     /* Call common method to give check the hook */
713     $message= plugin::check();
714     if (preg_match("/printer/i",$this->BelongsTo)){
715       $message= array_merge($message, $this->netConfigDNS->check());
716     }
718     /* Don't display check messages if this is a template object */
719     if(isset($this->parent->by_object['workgeneric'])){
720       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
721         return $message;
722       }
723     }elseif(isset($this->parent->by_object['termgeneric'])){
724       if($this->parent->by_object['termgeneric']->cn == "default"){
725         return $message;
726       }
727     }
729     $dn= "cn=".$this->cn.get_ou('printerRDN').",".$this->base;
731     /* must: cn */
732     if(($this->BelongsTo == "Printer") && $this->cn == ""){
733       $message[]= msgPool::required(_("Name"));
734     }
736     /* must: cn */
737     if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
738       $message[]= msgPool::invalid(_("Name"));
739     }
741     /* must: labeledURI */
742     if(empty($this->labeledURI)){
743       $message[]= msgPool::required(_("Printer URL"));
744     }
745     
746     /* Check if there is already an entry with this cn*/
747     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
748       $ldap= $this->config->get_ldap_link();
749       $ldap->cd ($this->base);
750       $ldap->ls("(cn=".$this->cn.")",get_ou('printerRDN').$this->base, array("cn"));
751       if ($ldap->count() != 0){
752         while ($attrs= $ldap->fetch()){
753           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
754             continue;
755           }
756           if ($attrs['dn'] != $this->orig_dn){
757             $message[]= msgPool::duplicated(_("Name"));
758             break;
759           }
760         }
761       }
762     }
764     /* Check if we are allowed to create or move this object
765      */
766     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
767       $message[] = msgPool::permCreate();
768     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
769       $message[] = msgPool::permMove();
770     }
772     return ($message);
773   }
776   /* Save to LDAP */
777   function save()
778   {
779     /* Update dn, to ensure storing as printer instead of WS / terminal
780      */
781     if(preg_match("/terminal/i",$this->BelongsTo)){
782       $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
783     }
785     if(preg_match("/workstation/i",$this->BelongsTo)){
786       $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
787     }
788     
789     if(!$this->is_account) return;
790     if(isset($this->parent->by_object['workgeneric'])){
791       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
792         return;
793       }
795       /* Adapt IP & mac from parent object */
796       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
797       $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
799     }elseif(isset($this->parent->by_object['termgeneric'])){
800       if($this->parent->by_object['termgeneric']->cn == "default"){
801         return;
802       }
803     
804       /* Adapt IP & mac from parent object */
805       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
806       $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
807     }
809     /* If type is still unknown, the initialisation of this printer failed, abort. */
810     if(preg_match("/unknown/i",$this->BelongsTo)){
811       return;
812     }
814     /* save ppd configuration */
815     if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
816     
817       $this->PPDdialogToSave->save_ppd();
819       /* Rename the generated ppd to match the gzip ending '.gz', if necessary.
820       */
821       $path = $this->config->get_cfg_value("ppdPath");
822       if(!preg_match("/\/$/",$path)){
823         $path = $path."/";
824       }
825       $ppdManager= new ppdManager($path);
826       if($ppdManager->useGzip && !preg_match('/\.gz$/',$this->gotoPrinterPPD)){
827         if(rename($path.$this->gotoPrinterPPD,$path.$this->gotoPrinterPPD.'.gz')){
828           $this->gotoPrinterPPD .= '.gz';
829         }
830       }elseif(!$ppdManager->useGzip && preg_match('/\.gz$/',$this->gotoPrinterPPD)){
831         $new_ppd = preg_replace('/\.gz$/','',$this->gotoPrinterPPD);
832         if(rename($path.$this->gotoPrinterPPD,$path.$new_ppd)){
833           $this->gotoPrinterPPD = $new_ppd; 
834         }
835       }
836     }
837     if($this->orig_dn != $this->dn && $this->orig_dn != "new"){
838       if(!empty($this->gotoPrinterPPD)) {
839         $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
840         $this->PPDdialogToSave->cn = $this->cn;
841         $this->PPDdialogToSave->generateProperties();
842         $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
843       }
844     }
846     /* Remove previously selected ppd file.*/
847     if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
848       if(!empty($this->initial_PPD)){
849         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
850         $tmp->removeModifiedPPD();
851       }
852     }
854     /* If no ppd is selected, remove this attribute */
855     if(!empty($this->gotoPrinterPPD)) {
856       $this->gotoPrinterPPD = $this->ppdServerPart.'/'.$this->gotoPrinterPPD;
857     }else{
858       $this->gotoPrinterPPD = array();
859     }
861     $dn= $this->dn;
863     /* reduce objectClasses to minimun */
864     $this->attrs['objectClass']= $this->objectclasses;
866     plugin::save();
867     $ldap= $this->config->get_ldap_link();
869     /* Remove all empty values */
870     if ($this->orig_dn == 'new'){
871       $attrs= array();
872       foreach ($this->attrs as $key => $val){
873         if (is_array($val) && count($val) == 0){
874           continue;
875         }
876         $attrs[$key]= $val;
877       }
878       $this->attrs= $attrs;
879     }
881     /* Append printer user 
882      */
883     $this->attrs['gotoUserPrinter']=array();
884     foreach($this->member['AddUser'] as $mem){
885       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
886     }
888     /* Append printer group 
889      */
890     $this->attrs['gotoGroupPrinter'] = array();
891     foreach($this->member['AddGroup'] as $mem){
892       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
893     }
895     /* Append printer admin user 
896      */
897     $this->attrs['gotoUserAdminPrinter'] = array();
898     foreach($this->member['AddAdminUser'] as $mem){
899       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
900     }
902     /* Append printer admin group 
903      */
904     $this->attrs['gotoGroupAdminPrinter']= array();
905     foreach($this->member['AddAdminGroup'] as $mem){
906       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
907     }
909     if($this->orig_dn == 'new'){
910       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
911         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
912           unset($this->attrs[$checkVar]);
913         }
914       }
915     }
917     /* Ensure to create a new object */
918     if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i",$this->orig_dn)){
919       $this->orig_dn = "new";
920     }
922     /* Move object in necessary*/
923     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
924       $this->move($this->orig_dn, $this->dn);
925     }
927     /* Write back to ldap */
928     $ldap= $this->config->get_ldap_link();
929     $ldap->cat($this->dn);
930     if(!$ldap->count()){
931       $ldap->cd($this->config->current['BASE']);
932       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
933       $ldap->cd($this->dn);
935       /* Remove empty values */ 
936       foreach($this->attrs as $name => $value){
937         if(empty($value)){
938           unset($this->attrs[$name]);
939         }
940       }
942       $ldap->add($this->attrs);
943       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
944       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
945     } else {
946       $ldap->cd($this->dn);
947       $this->cleanup();
948       $ldap->modify ($this->attrs); 
949       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
950       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
951     }
952     if (!$ldap->success()){
953       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
954     }
956     #if(preg_match("/printer/i",$this->BelongsTo)){
957       $this->netConfigDNS->cn = $this->cn;
958       $this->netConfigDNS->dn = $this->dn;
959       $this->netConfigDNS->save();
960     #}
961   }
963   function generateList(){
964     $a_return=array();
966     foreach($this->member as $type => $values){
967       $a_return[$type]=array();
968       foreach($values as $value){
969         if((preg_match("/Group/i",$type))){
970           if(!isset($value['description'])){
971             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
972           }else{
973             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
974           }
975         }else{
976           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
977         }
978       }
979     }
980     return($a_return);
981   }
983   /* Return plugin informations for acl handling
984       #FIXME FAIscript seams to ununsed within this class... */
985   static function plInfo()
986   {
987     return (array(
988           "plShortName"   => _("Generic"),
989           "plDescription" => _("Print generic"),
990           "plSelfModify"  => FALSE,
991           "plDepends"     => array(),
992           "plPriority"    => 4,
993           "plSection"     => array("administration"),
994           "plCategory"    => array("printer" => array("description"  => _("Printer"),
995                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
996           "plProvidedAcls"=> array(
997             "cn"                => _("Name"),
998             "base"                => _("Base") ,         
999             "description"       => _("Description"), 
1000             "l"                 => _("Location"), 
1001             "labeledURI"        => _("LabeledURL"), 
1002             "gotoPrinterPPD"    => _("Printer PPD"),
1003             "gotoUserPrinter"   => _("Permissions")) 
1004           ));
1005   }
1008   /* Delete member */
1009   function DelMember($type,$id)
1010   {
1011     /* Check if there was a printer "dn" given, or the "cn" */
1012     foreach($this->member[$type] as $key => $printer){
1013       if($printer['dn'] == $id) {
1014         $id = $key;
1015       }
1016     }
1017   
1018     if(!$this->acl_is_writeable("gotoUserPrinter")){
1019       msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
1020       return(FALSE);
1021     }
1022  
1023     if(isset($this->member[$type][$id])){
1024       unset($this->member[$type][$id]);
1025       return(TRUE);
1026     }
1027     return(FALSE);
1028   }
1031   /* Add given obejct to members */
1032   function AddMember($type,$dn)
1033   {
1034     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
1035     if(!in_array_ics($type, $types)){
1036       msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
1037       return(FALSE);
1038     }
1040     if(!$this->acl_is_writeable("gotoUserPrinter")){
1041       msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
1042       return(FALSE);
1043     }
1045     /* Get name of index attributes */
1046     if(preg_match("/user/i",$type)){
1047       $var = "uid";
1048     }else{
1049       $var = "cn";
1050     }
1052     $ldap = $this->config->get_ldap_link();
1053     $ldap->cd($dn);
1054     $ldap->cat($dn,array($var,"cn"));
1055     if($ldap->count()){
1057       $attrs = $ldap->fetch();
1059       if(isset($attrs[$var][0])){
1060         $name = $attrs[$var][0];
1062         /* Check if this uid/cn is already assigned to any permission */
1063         foreach($types as $ctype){
1065           /* If we want to add a user, only check user/userAdmin members */
1066           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
1067             continue;
1068           }
1070           /* If we want to add a group, only check groups/adminGroups .. */
1071           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1072             continue;
1073           }
1075           if(isset(  $this->member[$ctype][$name])){
1076             msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1077             return(FALSE);
1078           }
1079         }
1081         /* Everything is fine. So add the given object to members */
1082         $this->member[$type][$attrs[$var][0]] = $attrs ;
1083       }else{
1084         print_a($attrs);
1085       }
1086     }else{
1087       msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1088       return(FALSE);
1089     }
1090     return(TRUE);
1091   }
1094    /* Display generic part for server copy & paste */
1095   function getCopyDialog()
1096   {
1097     $vars = array("cn");
1098     $smarty = get_smarty();
1099     $smarty->assign("cn" ,$this->cn);
1100     $smarty->assign("object","printer");
1101     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1102     $ret = array();
1103     $ret['string'] = $str;
1104     $ret['status'] = "";
1105     return($ret);
1106   }
1109   function saveCopyDialog()
1110   {
1111     if(isset($_POST['cn'])){
1112       $this->cn = $_POST['cn'];
1113     }
1114   }
1116   function PrepareForCopyPaste($source)
1117   {
1118     plugin::PrepareForCopyPaste($source);
1119     if(isset($source['macAddress'][0])){
1120       $this->netConfigDNS->macAddress = $source['macAddress'][0];
1121     }
1122     if(isset($source['ipHostNumber'][0])){
1123       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1124     }
1126     $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1127     foreach($this->attributes as $attr){
1128       $this->$attr = $source_o->$attr;
1129     }
1130     $this->member = $source_o -> member;
1132     $this->gotoPrinterPPD = "";
1133   }
1136 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1137 ?>