Code

Updated member selection for printer
[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;
53   // Memeber dialog object
54   var $userSelect;
55   var $adminUserSelect;
57   function printgeneric (&$config, $dn,$parent_init,$parent)
58   {
59     $this->config = &$config;
60     $this->dn = $dn; 
61  
62     /* If parent was posted(the tabs object) we can detect the printer type. */
63     if($parent){
64       $this->parent = $parent;
65       $this->getTypeOfPrinter();
66     }else{
67       $this->BelongsTo = "unknown";
68       return;
69     }
71     /* Update dn, to ensure storing as printer instead of WS / terminal */
72     if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
73       $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
74     }
76     if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
77       $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
78     }
80     $this->orig_dn = $this->dn;
82     /* Get printer settings, possibly dn has changed */
83     plugin::plugin ($config, $this->dn);
85     /* Get is_account initially_was_account status */
86     $this->getTypeOfPrinter(true);
88     /* set orig dn to new if object is new */
89     $ldap= $this->config->get_ldap_link();
90     $ldap->cat($this->dn, array('dn'));
91     if(!$ldap->count()){
92       $this->orig_dn = "new";
93     }
94     
95     /* create dns object */
96     $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
98     /* Set base */
99     if ($this->dn == "new"){
100       $ui= get_userinfo();
101       $this->base= dn2base($ui->dn);
102       $this->cn= "";
103     } else {
104     
105       /* Set base and check if the extracted base exists */
106       if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i",$this->dn)){
107         $this->base= preg_replace("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i","",dn2base($this->dn));
108       }else{
109         $this->base= preg_replace("/".preg_quote(get_ou('printerRDN'), '/')."/i","",dn2base($this->dn));
110       }
112       if(!isset($this->config->idepartments[$this->base])){
113         msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
114         $this->base  = session::get('CurrentMainBase');
115       }
116     }
118     /* If no ppd is selected, remove this attribute */
119     if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
120       $this->ppdServerPart = preg_replace("/^(http.*ppd)\/.*$/i","\\1",$this->gotoPrinterPPD);
121     }else{
123       /* Detect PPD server part */
124       if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
125         $method="https://";
126       }else{
127         $method="http://";
128       }
129      
130       /* Get servername */
131       $server = $_SERVER['SERVER_NAME'];
132       if(tests::is_ip($server)){  
133         $server_name = @gethostbyaddr($server);
134       }else{
135         $server_name = @gethostbyaddr(gethostbyname($server));
136       }
137       $this->ppdServerPart  = $method.str_replace("//","/",$server_name."/ppd");
138     }
140     /* Extract selected ppd */
141     if(isset($this->gotoPrinterPPD)){
142       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
143     }
145     $this->initial_PPD = $this->gotoPrinterPPD;
147     /* Prepare different member types */ 
148     foreach(array("AddUser"       =>"gotoUserPrinter",
149           "AddGroup"      =>"gotoGroupPrinter",
150           "AddAdminUser"  =>"gotoUserAdminPrinter",
151           "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
153       /* $this->members contains all members */
154       $this->member[$type]=array();
156       if(isset($this->attrs[$attr])){
157         $ldap->cd($this->config->current['BASE']) ;
158         for($i = 0 ;  $i < $this->attrs[$attr]['count']; $i++){
159         
160           $mem = $this->attrs[$attr][$i];
161           if(preg_match("/Group/",$type)){
162             $ldap->search("(&(|(objectClass=posixGroup)(objectClass=gosaGroupOfNames))(cn=".$mem."))",array("cn","description"));
163             if($ldap->count()){
164               $entry = $ldap->fetch();
165               $this->member[$type][$entry['cn'][0]]=$entry;
166             }
167           }else{
168             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
169             if($ldap->count()){
170               $entry = $ldap->fetch();
171               $this->member[$type][$entry['uid'][0]]=$entry;
172             }
173           }
174         }
175       }
176     }
177     $this->orig_cn    = $this->cn;
178     $this->orig_base  = $this->base;
179   }
181   function set_acl_base($base)
182   {
183     plugin::set_acl_base($base);
184     if(is_object($this->netConfigDNS)){
185       $this->netConfigDNS->set_acl_base($base);
186     }
187   }
189   function set_acl_category($cat)
190   {
191     plugin::set_acl_category($cat);
192     if(is_object($this->netConfigDNS)){
193       $this->netConfigDNS->set_acl_category($cat);
194     }
195   }
197   /* Detect type of printer.
198    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
199    * We can detect the type printer type when comparing the tabs objects
200    */
201   function getTypeOfPrinter($UpdateAccountStatus = false)
202   {
203     /* Disable account as default
204      */  
205     $this->is_account = $this->initially_was_account = false;
207     /* Detect type of printer via parent tabs.
208      */
210     $class = get_class($this->parent);
211     if(isset($this->parent->by_object['workgeneric'])){
213       /* Exclude templates 
214        */
215       $this->cn = $this->parent->by_object['workgeneric']->cn;
216       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
217         $this->BelongsTo = "WorkstationTemplate";
218       }else{
219         $this->BelongsTo = "Workstation";
220       }
221     }elseif(isset($this->parent->by_object['termgeneric'])){
223       /* Exclude templates 
224        */
225       $this->cn = $this->parent->by_object['termgeneric']->cn;
226       if($this->parent->by_object['termgeneric']->cn == "default"){
227         $this->BelongsTo = "TerminalTemplate";  
228       }else{
229         $this->BelongsTo = "Terminal";
230       }
231     }elseif(isset($this->parent->by_name['printgeneric'])){
232       $this->BelongsTo  = "Printer";
233     }
235     if($UpdateAccountStatus){
237       /* Set is_account / was account 
238        */
239       if($this->dn == "new"){
240         $this->initially_was_account = false;
241       }
243       /* If is printer it must be a true account.
244        */
245       if(preg_match("/printer/i",$this->BelongsTo)){
246         $this->is_account = true;
247       }
249       /* Update dn, to ensure storing as printer instead of WS / terminal
250        */
251       if(preg_match("/terminal/i",$this->BelongsTo)){
252         $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
253       }
255       if(preg_match("/workstation/i",$this->BelongsTo)){
256         $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
257       }
259       /* Detect if this is a valid printer account;
260        */
261       $ldap = $this->config->get_ldap_link();
262       $ldap->cat($this->dn, array('objectClass'));
264       if($ldap->count()){
265         $attrs = $ldap->fetch();
266         if(in_array("gotoPrinter",$attrs['objectClass'])){
267           $this->initially_was_account = true;
268           $this->is_account             = true;
269         }else{
270           $this->is_account = false;
271         }
272       }
273     }
274   }
276   function execute()
277   {
278     /* Call parent execute */
279     plugin::execute();
281     if($this->is_account && !$this->view_logged){
282       $this->view_logged = TRUE;
283       new log("view","printer/".get_class($this),$this->dn);
284     }
286     /* If type of printer couldn't be detected (because of missing parent object in construction) 
287      * hide this tab.
288      */
289     if(preg_match("/unknown/i",$this->BelongsTo)){
290       $display= $this->show_enable_header(_("Add printer extension"),
291           _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
292       return($display);
293     }
295     /* Templates can't have printer extensions 
296      */
297     if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
298       $display= $this->show_enable_header(_("Add printer extension"),
299           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
300       return($display);
301     }
302     if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
303       $display= $this->show_enable_header(_("Add printer extension"),
304           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
305       return($display);
306     }
308     /* Get cn from base object */
309     if(preg_match("/^Workstation$/i",$this->BelongsTo)){
310       $this->cn = $this->parent->by_object['workgeneric']->cn;
311     }
312     if(preg_match("/^Terminal$/i",$this->BelongsTo)){
313       $this->cn = $this->parent->by_object['termgeneric']->cn;
314     }
316     $smarty= get_smarty();
318     /* Assign acls */
319     $tmp = $this->plInfo();
320     foreach($tmp['plProvidedAcls'] as $name => $translation){
321       $smarty->assign($name."ACL", $this->getacl($name));
322     }
324     $display="";
326     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
327     if(preg_match("/^Printer$/i",$this->BelongsTo)){    
328       $smarty->assign("StandAlone",true);
329     }else{
330       $smarty->assign("StandAlone",false);
331     }
333     /* Do we need to flip is_account state? */
334     if(isset($_POST['modify_state'])){
335       if($this->is_account && $this->acl_is_removeable()){
336         $this->is_account= FALSE;
337       }elseif(!$this->is_account && $this->acl_is_createable()){
338         $this->is_account= TRUE;
339       }
340     }
342     /* Do we represent a valid printer? */
343     if (!$this->is_account && $this->parent === NULL){
344       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
345         msgPool::noValidExtension(_("printer"))."</b>";
346       return($display);
347     }
349     /* If this is a WS / Terminal depending printer, display account state button */
350     if(!preg_match("/^Printer$/i",$this->BelongsTo)){
351       if($this->cn == "" && ($this->dn != "new")){
352         $display= $this->show_enable_header(_("Add printer extension"),
353             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);
354         $this->is_account= false;
355         return $display;
356       }
358       if (($this->is_account)){
359         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
360           $display= $this->show_disable_header(_("Remove printer extension"),
361               msgPool::featuresEnabled(_("printer")));
362         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
363           $display= $this->show_disable_header(_("Remove printer extension"),
364               msgPool::featuresDisabled(_("printer")));
365         }
366       }else{
367         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
368           $display= $this->show_enable_header(_("Add printer extension"),
369               msgPool::featuresEnabled(_("printer")));
370         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
371           $display= $this->show_enable_header(_("Add printer extension"),
372               msgPool::featuresDisabled(_("printer")));
373         }  
374         return ($display);
375       }
376     }
378     /* Base select dialog */
379     $once = true;
380     foreach($_POST as $name => $value){
381       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
382         $once = false;
383         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
384         $this->dialog->setCurrentBase($this->base);
385         $this->baseSelection = true;
386       }
387     }
389     /* Dialog handling for base select dialog
390      * Check if base was selected, dialog aborted etc */
391     if(is_object($this->dialog)){
393       $this->dialog->save_object();
394       if($this->baseSelection){
395         if($this->dialog->isClosed()){
396           $this->dialog = false;
397           $this->baseSelection = false;
398         }elseif($this->dialog->isSelected()){
400           /* A new base was selected, check if it is a valid one */
401           $tmp = $this->get_allowed_bases();
402           if(isset($tmp[$this->dialog->isSelected()])){
403             $this->base = $this->dialog->isSelected();
404           }
406           $this->dialog= false;
407           $this->baseSelection = false;
408         }else{
409           return($this->dialog->execute());
410         }
411       }
412     }
414     /* Fill templating stuff */
415     $smarty->assign("bases", $this->get_allowed_bases());
416     $smarty->assign("base_select", $this->base);
418     /* Assign attributes */
419     foreach ($this->attributes as $attr){
420       $smarty->assign("$attr", $this->$attr);
421     }
423     // Act on add user/grouo requests 
424     if(isset($_POST['AddUser'])){
425       $this->userSelect = new userGroupSelect($this->config, get_userinfo());
426       $this->dialog = TRUE;
427     }
428     if(isset($_POST['AddAdminUser'])){
429       $this->adminUserSelect = new userGroupSelect($this->config, get_userinfo());
430       $this->dialog = TRUE;
431     }
433     /* Display ppd configure/select dialog      */
434     if(isset($_POST['EditDriver'])){
435       if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
436         $this->dialog = $this->PPDdialogToSave;
437       }else{
438  
439         if(is_array($this->gotoPrinterPPD)){
440           $this->dialog = new printerPPDDialog($this->config, $this->dn,"");
441         }else{
442           $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
443         }
444         $this->dialog->cn= $this->cn;
445       }
447       // Detect edit acl base
448       $ldap = $this->config->get_ldap_link();
449       if($ldap->dn_exists($this->dn)){
450         $acl_base = $this->dn;
451       }else{
452         $acl_base = $this->base;
453       }
455       $this->dialog->set_acl_base($acl_base);
456     }
458     /* remove ppd */
459     if(isset($_POST['RemoveDriver'])){
460       /* Detect PPD server part */
461       if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
462         $method="https://";
463       }else{
464         $method="http://";
465       }
466      
467       /* Get servername */
468       $server = $_SERVER['SERVER_NAME'];
469       if(tests::is_ip($server)){  
470         $server_name = @gethostbyaddr($server);
471       }else{
472         $server_name = @gethostbyaddr(gethostbyname($server));
473       }
474       $this->ppdServerPart  = $method.str_replace("//","/",$server_name."/ppd");
475       $this->gotoPrinterPPD = array();
476       $this->PPDdialogToSave = NULL;
477     }
479     /* Close ppd dialog */
480     if(isset($_POST['ClosePPD'])){
481       unset($this->dialog);
482       $this->dialog=FALSE;
483     }
485     /* Save selected ppd */
486     if(isset($_POST['SavePPD'])){
487       $this->dialog->save_object();
488       if(count($this->dialog->check())){
489         foreach($this->dialog->check() as $msg){
490           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
491         }
492       }else{
493         $this->gotoPrinterPPD = array();
494         $this->gotoPrinterPPD = $this->dialog->save();
495         $this->PPDdialogToSave = $this->dialog;
496         unset($this->dialog);
497         $this->dialog=FALSE;
498       }
499     }
501      /* Member management, delete user / group / admin ..*/
502     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
503       foreach($_POST['UserMember'] as $mem){
504         $this->DelMember('AddUser',$mem);
505       }
506     }
508     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
509       foreach($_POST['UserMember'] as $mem){
510         $this->DelMember('AddGroup',$mem);
511       }
512     }
514     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
515       foreach($_POST['AdminMember'] as $mem){
516         $this->DelMember('AddAdminUser',$mem);
517       }
518     }
520     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
521       foreach($_POST['AdminMember'] as $mem){
522         $this->DelMember('AddAdminGroup',$mem);
523       }
524     }
526     // Abort user / group adding dialog 
527     if(isset($_POST['userGroupSelect_cancel'])){
528       $this->dialog=FALSE;
529       $this->userSelect = NULL;
530       $this->adminUserSelect = NULL;
531     }
533     // Save selected users / groups 
534     if(isset($_POST['userGroupSelect_save'])){
535       $users = array();
536       if($this->userSelect instanceOf userGroupSelect){
537         $users = $this->userSelect->save();
538         $add = '';
539       }elseif($this->adminUserSelect instanceOf userGroupSelect){
540         $users = $this->adminUserSelect->save();
541         $add = 'Admin';
542       }
543       foreach($users as $user){
544         if(in_array('gosaAccount', $user['objectClass'])){
545           $type = 'Add'.$add.'User';  
546         }else{
547           $type = 'Add'.$add.'Group';  
548         }
549         $this->AddMember($type, $user['dn']);
550       }
551       $this->dialog=FALSE;
552       $this->userSelect = NULL;
553       $this->adminUserSelect = NULL;
554     }
556     // Display add user/group dialogs 
557     if($this->userSelect instanceOf userGroupSelect || $this->adminUserSelect instanceOf userGroupSelect){
559       // Build up blocklist
560       $used = array();
561       foreach($this->member as $type => $members){
562         foreach($members as $member){
563           $used['dn'][] = $member['dn'];
564         }
565       }
566       session::set('filterBlacklist', $used);
567       if($this->userSelect instanceOf userGroupSelect){
568         return($this->userSelect->execute());
569       }elseif($this->adminUserSelect instanceOf userGroupSelect){
570         return($this->adminUserSelect->execute());
571       }
572     }
575     /* Display dialog, if there is currently one open*/
576     if(is_object($this->dialog)){
577       $this->dialog->save_object();
578       $display = $this->dialog->execute();
579       return $display;
580     }
582     /* Parse selected ppd file */
583     $config = session::get('config');
584     if ($config->get_cfg_value("ppdPath") != ""){
585       $path = $config->get_cfg_value("ppdPath");
586       if(!preg_match("/\/$/",$path)){
587         $path = $path."/";
588       }
590       $ppdManager= new ppdManager($path);
591       $smarty->assign("displayServerPath",true);
592       if(!empty($this->gotoPrinterPPD)){
593         if((!file_exists($path.$this->gotoPrinterPPD))){
594           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
595         }else{
596           $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD);
597           $smarty->assign("driverInfo", $ppdDesc['name']);
598         }
599       }else{
600         $smarty->assign("driverInfo", _("Not defined"));
601         $smarty->assign("displayServerPath",false);
602       }
603     }else{
604       $smarty->assign("driverInfo",_("Can't get ppd informations."));
605       $smarty->assign("displayServerPath",true);
606     }
607     $smarty->assign("ppdServerPart",$this->ppdServerPart);
608     
610     /* Create user & admin user list */
611     $list=$this->generateList();
612     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
613     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
615     asort($userlist);
616     asort($adminlist);
618     if(!preg_match("/Printer/i",$this->BelongsTo)){
619       if(preg_match("/Terminal/i",$this->BelongsTo)){
620         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
621       }else{
622         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
623       }
624       $smarty->assign("cnACL"    , $this->getacl("cn",true));
625     }else{
626       $smarty->assign("desc"    ,"");
627     }
628     $smarty->assign("UserMember"    ,$this->UserMember);
629     $smarty->assign("UserMembers"   ,$userlist);
630     $smarty->assign("UserMemberKeys",array_flip($userlist));
632     $smarty->assign("AdminMember"    ,$this->AdminMember);
633     $smarty->assign("AdminMembers"   ,$adminlist);
634     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
635     if(preg_match("/Printer/i",$this->BelongsTo)){
637       /* Show main page */
638       $str = $this->netConfigDNS->execute();
639       if(is_object($this->netConfigDNS->dialog)){
640         return($str);
641       }
642       $smarty->assign("netconfig", $str);
643     } else {
644       $smarty->assign("netconfig", "");
645     }
647     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
648   }
650   function remove_from_parent()
651   {
652     /* Only remove if there was initially an account */
653     if($this->initially_was_account){
655       /* Update dn, to ensure storing as printer instead of WS / terminal
656        */
657       if(preg_match("/terminal/i",$this->BelongsTo)){
658         $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/').",/i",get_ou('printerRDN'),$this->dn);
659       }
661       if(preg_match("/workstation/i",$this->BelongsTo)){
662         $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
663       }
665       /* Check if this dn points to a printer, to avoid deleting something else */
666       $ldap= $this->config->get_ldap_link();
667       $ldap->cat($this->dn, array('dn',"objectClass"));
668       if(!$ldap->count()){
669         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
670         return;
671       }
673       /* Check if obejct is a printer */
674       $CheckPrinter = $ldap->fetch();
675       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
676         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
677         return;
678       }
680       /* Remove account & dns extension */ 
681       $this->netConfigDNS->remove_from_parent();
682       $ldap->rmdir($this->dn);
684       new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
685   
686       if (!$ldap->success()){
687         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
688       }
689       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
691       /* Delete references to object groups */
692       $ldap->cd ($this->config->current['BASE']);
693       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
694       while ($ldap->fetch()){
695         $og= new ogroup($this->config, $ldap->getDN());
696         unset($og->member[$this->dn]);
697         $og->save ();
698       }
700       /* Remove previously selected ppd file.*/
701       if(!empty($this->initial_PPD)){
702         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
703         $tmp->removeModifiedPPD();
704       }
705     }
706   }
709   /* Save data to object */
710   function save_object()
711   {
712     /* Create a base backup and reset the
713        base directly after calling plugin::save_object();
714        Base will be set seperatly a few lines below */
715     $base_tmp = $this->base;
716     plugin::save_object();
717     $this->base = $base_tmp;
719     if(isset($_POST['ppdServerPart'])){
720       $this->ppdServerPart = get_post('ppdServerPart');
721     }
723     if(is_object($this->netConfigDNS)){
724       $this->netConfigDNS->save_object();
725     }
726     
727     /* Set new base if allowed */
728     $tmp = $this->get_allowed_bases();
729     if(isset($_POST['base'])){
730       if(isset($tmp[$_POST['base']])){
731         $this->base= $_POST['base'];
732       }
733     }
734   }
736   /* Check supplied data */
737   function check()
738   {
739     /* Call common method to give check the hook */
740     $message= plugin::check();
741     if (preg_match("/printer/i",$this->BelongsTo)){
742       $message= array_merge($message, $this->netConfigDNS->check());
743     }
745     /* Don't display check messages if this is a template object */
746     if(isset($this->parent->by_object['workgeneric'])){
747       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
748         return $message;
749       }
750     }elseif(isset($this->parent->by_object['termgeneric'])){
751       if($this->parent->by_object['termgeneric']->cn == "default"){
752         return $message;
753       }
754     }
756     $dn= "cn=".$this->cn.get_ou('printerRDN').",".$this->base;
758     /* must: cn */
759     if(($this->BelongsTo == "Printer") && $this->cn == ""){
760       $message[]= msgPool::required(_("Name"));
761     }
763     /* must: cn */
764     if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
765       $message[]= msgPool::invalid(_("Name"));
766     }
768     /* must: labeledURI */
769     if(empty($this->labeledURI)){
770       $message[]= msgPool::required(_("Printer URL"));
771     }
772     
773     /* Check if there is already an entry with this cn*/
774     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
775       $ldap= $this->config->get_ldap_link();
776       $ldap->cd ($this->base);
777       $ldap->ls("(cn=".$this->cn.")",get_ou('printerRDN').$this->base, array("cn"));
778       if ($ldap->count() != 0){
779         while ($attrs= $ldap->fetch()){
780           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
781             continue;
782           }
783           if ($attrs['dn'] != $this->orig_dn){
784             $message[]= msgPool::duplicated(_("Name"));
785             break;
786           }
787         }
788       }
789     }
791     /* Check if we are allowed to create or move this object
792      */
793     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
794       $message[] = msgPool::permCreate();
795     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
796       $message[] = msgPool::permMove();
797     }
799     return ($message);
800   }
803   /* Save to LDAP */
804   function save()
805   {
806     /* Update dn, to ensure storing as printer instead of WS / terminal
807      */
808     if(preg_match("/terminal/i",$this->BelongsTo)){
809       $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
810     }
812     if(preg_match("/workstation/i",$this->BelongsTo)){
813       $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
814     }
815     
816     if(!$this->is_account) return;
817     if(isset($this->parent->by_object['workgeneric'])){
818       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
819         return;
820       }
822       /* Adapt IP & mac from parent object */
823       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
824       $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
826     }elseif(isset($this->parent->by_object['termgeneric'])){
827       if($this->parent->by_object['termgeneric']->cn == "default"){
828         return;
829       }
830     
831       /* Adapt IP & mac from parent object */
832       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
833       $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
834     }
836     /* If type is still unknown, the initialisation of this printer failed, abort. */
837     if(preg_match("/unknown/i",$this->BelongsTo)){
838       return;
839     }
841     /* save ppd configuration */
842     if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
843     
844       $this->PPDdialogToSave->save_ppd();
846       /* Rename the generated ppd to match the gzip ending '.gz', if necessary.
847       */
848       $path = $this->config->get_cfg_value("ppdPath");
849       if(!preg_match("/\/$/",$path)){
850         $path = $path."/";
851       }
852       $ppdManager= new ppdManager($path);
853       if($ppdManager->useGzip && !preg_match('/\.gz$/',$this->gotoPrinterPPD)){
854         if(rename($path.$this->gotoPrinterPPD,$path.$this->gotoPrinterPPD.'.gz')){
855           $this->gotoPrinterPPD .= '.gz';
856         }
857       }elseif(!$ppdManager->useGzip && preg_match('/\.gz$/',$this->gotoPrinterPPD)){
858         $new_ppd = preg_replace('/\.gz$/','',$this->gotoPrinterPPD);
859         if(rename($path.$this->gotoPrinterPPD,$path.$new_ppd)){
860           $this->gotoPrinterPPD = $new_ppd; 
861         }
862       }
863     }
864     if($this->orig_dn != $this->dn && $this->orig_dn != "new"){
865       if(!empty($this->gotoPrinterPPD)) {
866         $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
867         $this->PPDdialogToSave->cn = $this->cn;
868         $this->PPDdialogToSave->generateProperties();
869         $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
870       }
871     }
873     /* Remove previously selected ppd file.*/
874     if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
875       if(!empty($this->initial_PPD)){
876         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
877         $tmp->removeModifiedPPD();
878       }
879     }
881     /* If no ppd is selected, remove this attribute */
882     if(!empty($this->gotoPrinterPPD)) {
883       $this->gotoPrinterPPD = $this->ppdServerPart.'/'.$this->gotoPrinterPPD;
884     }else{
885       $this->gotoPrinterPPD = array();
886     }
888     $dn= $this->dn;
890     /* reduce objectClasses to minimun */
891     $this->attrs['objectClass']= $this->objectclasses;
893     plugin::save();
894     $ldap= $this->config->get_ldap_link();
896     /* Remove all empty values */
897     if ($this->orig_dn == 'new'){
898       $attrs= array();
899       foreach ($this->attrs as $key => $val){
900         if (is_array($val) && count($val) == 0){
901           continue;
902         }
903         $attrs[$key]= $val;
904       }
905       $this->attrs= $attrs;
906     }
908     /* Append printer user 
909      */
910     $this->attrs['gotoUserPrinter']=array();
911     foreach($this->member['AddUser'] as $mem){
912       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
913     }
915     /* Append printer group 
916      */
917     $this->attrs['gotoGroupPrinter'] = array();
918     foreach($this->member['AddGroup'] as $mem){
919       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
920     }
922     /* Append printer admin user 
923      */
924     $this->attrs['gotoUserAdminPrinter'] = array();
925     foreach($this->member['AddAdminUser'] as $mem){
926       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
927     }
929     /* Append printer admin group 
930      */
931     $this->attrs['gotoGroupAdminPrinter']= array();
932     foreach($this->member['AddAdminGroup'] as $mem){
933       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
934     }
936     if($this->orig_dn == 'new'){
937       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
938         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
939           unset($this->attrs[$checkVar]);
940         }
941       }
942     }
944     /* Ensure to create a new object */
945     if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i",$this->orig_dn)){
946       $this->orig_dn = "new";
947     }
949     /* Move object in necessary*/
950     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
951       $this->move($this->orig_dn, $this->dn);
952     }
954     /* Write back to ldap */
955     $ldap= $this->config->get_ldap_link();
956     $ldap->cat($this->dn);
957     if(!$ldap->count()){
958       $ldap->cd($this->config->current['BASE']);
959       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
960       $ldap->cd($this->dn);
962       /* Remove empty values */ 
963       foreach($this->attrs as $name => $value){
964         if(empty($value)){
965           unset($this->attrs[$name]);
966         }
967       }
969       $ldap->add($this->attrs);
970       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
971       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
972     } else {
973       $ldap->cd($this->dn);
974       $this->cleanup();
975       $ldap->modify ($this->attrs); 
976       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
977       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
978     }
979     if (!$ldap->success()){
980       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
981     }
983     #if(preg_match("/printer/i",$this->BelongsTo)){
984       $this->netConfigDNS->cn = $this->cn;
985       $this->netConfigDNS->dn = $this->dn;
986       $this->netConfigDNS->save();
987     #}
988   }
990   function generateList(){
991     $a_return=array();
993     foreach($this->member as $type => $values){
994       $a_return[$type]=array();
995       foreach($values as $value){
996         if((preg_match("/Group/i",$type))){
997           if(!isset($value['description'])){
998             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
999           }else{
1000             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
1001           }
1002         }else{
1003           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
1004         }
1005       }
1006     }
1007     return($a_return);
1008   }
1010   /* Return plugin informations for acl handling
1011       #FIXME FAIscript seams to ununsed within this class... */
1012   static function plInfo()
1013   {
1014     return (array(
1015           "plShortName"   => _("Generic"),
1016           "plDescription" => _("Print generic"),
1017           "plSelfModify"  => FALSE,
1018           "plDepends"     => array(),
1019           "plPriority"    => 4,
1020           "plSection"     => array("administration"),
1021           "plCategory"    => array("printer" => array("description"  => _("Printer"),
1022                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
1023           "plProvidedAcls"=> array(
1024             "cn"                => _("Name"),
1025             "base"                => _("Base") ,         
1026             "description"       => _("Description"), 
1027             "l"                 => _("Location"), 
1028             "labeledURI"        => _("LabeledURL"), 
1029             "gotoPrinterPPD"    => _("Printer PPD"),
1030             "gotoUserPrinter"   => _("Permissions")) 
1031           ));
1032   }
1035   /* Delete member */
1036   function DelMember($type,$id)
1037   {
1038     /* Check if there was a printer "dn" given, or the "cn" */
1039     foreach($this->member[$type] as $key => $printer){
1040       if($printer['dn'] == $id) {
1041         $id = $key;
1042       }
1043     }
1044   
1045     if(!$this->acl_is_writeable("gotoUserPrinter")){
1046       msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
1047       return(FALSE);
1048     }
1049  
1050     if(isset($this->member[$type][$id])){
1051       unset($this->member[$type][$id]);
1052       return(TRUE);
1053     }
1054     return(FALSE);
1055   }
1058   /* Add given obejct to members */
1059   function AddMember($type,$dn)
1060   {
1061     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
1062     if(!in_array_ics($type, $types)){
1063       msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
1064       return(FALSE);
1065     }
1067     if(!$this->acl_is_writeable("gotoUserPrinter")){
1068       msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
1069       return(FALSE);
1070     }
1072     /* Get name of index attributes */
1073     if(preg_match("/user/i",$type)){
1074       $var = "uid";
1075     }else{
1076       $var = "cn";
1077     }
1079     $ldap = $this->config->get_ldap_link();
1080     $ldap->cd($dn);
1081     $ldap->cat($dn,array($var,"cn"));
1082     if($ldap->count()){
1084       $attrs = $ldap->fetch();
1086       if(isset($attrs[$var][0])){
1087         $name = $attrs[$var][0];
1089         /* Check if this uid/cn is already assigned to any permission */
1090         foreach($types as $ctype){
1092           /* If we want to add a user, only check user/userAdmin members */
1093           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
1094             continue;
1095           }
1097           /* If we want to add a group, only check groups/adminGroups .. */
1098           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1099             continue;
1100           }
1102           if(isset(  $this->member[$ctype][$name])){
1103             msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1104             return(FALSE);
1105           }
1106         }
1108         /* Everything is fine. So add the given object to members */
1109         $this->member[$type][$attrs[$var][0]] = $attrs ;
1110       }else{
1111         print_a($attrs);
1112       }
1113     }else{
1114       msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1115       return(FALSE);
1116     }
1117     return(TRUE);
1118   }
1121    /* Display generic part for server copy & paste */
1122   function getCopyDialog()
1123   {
1124     $vars = array("cn");
1125     $smarty = get_smarty();
1126     $smarty->assign("cn" ,$this->cn);
1127     $smarty->assign("object","printer");
1128     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1129     $ret = array();
1130     $ret['string'] = $str;
1131     $ret['status'] = "";
1132     return($ret);
1133   }
1136   function saveCopyDialog()
1137   {
1138     if(isset($_POST['cn'])){
1139       $this->cn = $_POST['cn'];
1140     }
1141   }
1143   function PrepareForCopyPaste($source)
1144   {
1145     plugin::PrepareForCopyPaste($source);
1146     if(isset($source['macAddress'][0])){
1147       $this->netConfigDNS->macAddress = $source['macAddress'][0];
1148     }
1149     if(isset($source['ipHostNumber'][0])){
1150       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1151     }
1153     $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1154     foreach($this->attributes as $attr){
1155       $this->$attr = $source_o->$attr;
1156     }
1157     $this->member = $source_o -> member;
1159     $this->gotoPrinterPPD = "";
1160   }
1163   function is_modal_dialog()
1164   {
1165     return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
1166   }
1170 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1171 ?>