Code

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