Code

Fixed printer problem
[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          = "";
19   var $UserMember       ="";
20   var $UserMembers      =array();
21   var $UserMemberKeys   =array();
23   var $AdminMember      ="";
24   var $AdminMembers     =array();
25   var $AdminMemberKeys  =array();
27   var $PPDdialogToSave  = NULL;
28   var $BelongsTo        = "unknown"; //  Specifies if this is a standalone printer, or belongs to a terminal / WS
30   var $member           =array();
31   var $strings          = "";
32   var $netConfigDNS;
33   var $baseSelection    = false;
34   var $macAddress       = "";
36   var $gotoUserAdminPrinter;
37   var $gotoGroupAdminPrinter ;
38   var $gotoGroupPrinter;
39   var $gotoUserPrinter ;
41   /* attribute list for save action */
42   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress", 
43                               "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter","gosaUnitTag");
44   var $objectclasses  = array("top", "gotoPrinter");
45   var $view_logged    = FALSE;
46   var $parent;
48   function printgeneric (&$config, $dn,$parent_init,$parent)
49   {
50     $this->config = &$config;
51     $this->dn = $dn; 
52  
53     /* If parent was posted(the tabs object) we can detect the printer type. */
54     if($parent){
55       $this->parent = $parent;
56       $this->getTypeOfPrinter();
57     }else{
58       $this->BelongsTo = "unknown";
59       return;
60     }
62     /* Update dn, to ensure storing as printer instead of WS / terminal */
63     if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
64       $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou'))."/",get_ou('printerou'),$this->dn);
65     }
67     if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
68       $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
69     }
71     $this->orig_dn = $this->dn;
73     /* Get printer settings, possibly dn has changed */
74     plugin::plugin ($config, $this->dn);
76     /* Get is_account initially_was_account status */
77     $this->getTypeOfPrinter(true);
79     /* set orig dn to new if object is new */
80     $ldap= $this->config->get_ldap_link();
81     $ldap->cat($this->dn, array('dn'));
82     if(!$ldap->count()){
83       $this->orig_dn = "new";
84     }
85     
86     /* create dns object */
87     $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
89     /* Set base */
90     if ($this->dn == "new"){
91       $ui= get_userinfo();
92       $this->base= dn2base($ui->dn);
93       $this->cn= "";
94     } else {
95     
96       /* Set base and check if the extracted base exists */
97       if(preg_match("/".normalizePreg(get_ou('incomingou'))."/",$this->dn)){
98         $this->base= preg_replace("/".normalizePreg(get_ou('incomingou'))."/","",dn2base($this->dn));
99       }else{
100         $this->base= preg_replace("/".normalizePreg(get_ou('printerou'))."/","",dn2base($this->dn));
101       }
103       if(!isset($this->config->idepartments[$this->base])){
104         msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
105         $this->base  = session::get('CurrentMainBase');
106       }
107     }
109     /* Extract selected ppd */
110     if(isset($this->gotoPrinterPPD)){
111       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
112     }
114     $this->initial_PPD = $this->gotoPrinterPPD;
116     /* Prepare different member types */ 
117     foreach(array("AddUser"       =>"gotoUserPrinter",
118           "AddGroup"      =>"gotoGroupPrinter",
119           "AddAdminUser"  =>"gotoUserAdminPrinter",
120           "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
122       /* $this->members contains all members */
123       $this->member[$type]=array();
125       if(isset($this->attrs[$attr])){
126         $ldap->cd($this->config->current['BASE']) ;
127         for($i = 0 ;  $i < $this->attrs[$attr]['count']; $i++){
128         
129           $mem = $this->attrs[$attr][$i];
130           if(preg_match("/Group/",$type)){
131             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
132             if($ldap->count()){
133               $entry = $ldap->fetch();
134               $this->member[$type][$entry['cn'][0]]=$entry;
135             }
136           }else{
137             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
138             if($ldap->count()){
139               $entry = $ldap->fetch();
140               $this->member[$type][$entry['uid'][0]]=$entry;
141             }
142           }
143         }
144       }
145     }
146   }
148   function set_acl_base($base)
149   {
150     plugin::set_acl_base($base);
151     if(is_object($this->netConfigDNS)){
152       $this->netConfigDNS->set_acl_base($base);
153     }
154   }
156   function set_acl_category($cat)
157   {
158     plugin::set_acl_category($cat);
159     if(is_object($this->netConfigDNS)){
160       $this->netConfigDNS->set_acl_category($cat);
161     }
162   }
164   /* Detect type of printer.
165    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
166    * We can detect the type printer type when comparing the tabs objects
167    */
168   function getTypeOfPrinter($UpdateAccountStatus = false)
169   {
170     /* Disable account as default
171      */  
172     $this->is_account = $this->initially_was_account = false;
174     /* Detect type of printer via parent tabs.
175      */
177     $class = get_class($this->parent);
178     if(isset($this->parent->by_object['workgeneric'])){
180       /* Exclude templates 
181        */
182       $this->cn = $this->parent->by_object['workgeneric']->cn;
183       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
184         $this->BelongsTo = "WorkstationTemplate";
185       }else{
186         $this->BelongsTo = "Workstation";
187       }
188     }elseif(isset($this->parent->by_object['termgeneric'])){
190       /* Exclude templates 
191        */
192       $this->cn = $this->parent->by_object['termgeneric']->cn;
193       if($this->parent->by_object['termgeneric']->cn == "default"){
194         $this->BelongsTo = "TerminalTemplate";  
195       }else{
196         $this->BelongsTo = "Terminal";
197       }
198     }elseif(isset($this->parent->by_name['printgeneric'])){
199       $this->BelongsTo  = "Printer";
200     }
202     if($UpdateAccountStatus){
204       /* Set is_account / was account 
205        */
206       if($this->dn == "new"){
207         $this->initially_was_account = false;
208       }
210       /* If is printer it must be a true account.
211        */
212       if(preg_match("/printer/i",$this->BelongsTo)){
213         $this->is_account = true;
214       }
216       /* Update dn, to ensure storing as printer instead of WS / terminal
217        */
218       if(preg_match("/terminal/i",$this->BelongsTo)){
219         $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou'))."/",get_ou('printerou'),$this->dn);
220       }
222       if(preg_match("/workstation/i",$this->BelongsTo)){
223         $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
224       }
226       /* Detect if this is a valid printer account;
227        */
228       $ldap = $this->config->get_ldap_link();
229       $ldap->cat($this->dn, array('objectClass'));
231       if($ldap->count()){
232         $attrs = $ldap->fetch();
233         if(in_array("gotoPrinter",$attrs['objectClass'])){
234           $this->initially_was_account = true;
235           $this->is_account             = true;
236         }else{
237           $this->is_account = false;
238         }
239       }
240     }
241   }
243   function execute()
244   {
245     /* Call parent execute */
246     plugin::execute();
248     if($this->is_account && !$this->view_logged){
249       $this->view_logged = TRUE;
250       new log("view","printer/".get_class($this),$this->dn);
251     }
254     /* If type of printer couldn't be detected (because of missing parent object in construction) 
255      * hide this tab.
256      */
257     if(preg_match("/unknown/i",$this->BelongsTo)){
258       $display= $this->show_enable_header(_("Add printer extension"),
259           _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
260       return($display);
261     }
263     /* Templates can't have printer extensions 
264      */
265     if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
266       $display= $this->show_enable_header(_("Add printer extension"),
267           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
268       return($display);
269     }
270     if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
271       $display= $this->show_enable_header(_("Add printer extension"),
272           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
273       return($display);
274     }
276     /* Get cn from base object */
277     if(preg_match("/^Workstation$/i",$this->BelongsTo)){
278       $this->cn = $this->parent->by_object['workgeneric']->cn;
279     }
280     if(preg_match("/^Terminal$/i",$this->BelongsTo)){
281       $this->cn = $this->parent->by_object['termgeneric']->cn;
282     }
284     $smarty= get_smarty();
286     /* Assign acls */
287     $tmp = $this->plInfo();
288     foreach($tmp['plProvidedAcls'] as $name => $translation){
289       $smarty->assign($name."ACL", $this->getacl($name));
290     }
292     $display="";
294     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
295     if(preg_match("/^Printer$/i",$this->BelongsTo)){    
296       $smarty->assign("StandAlone",true);
297     }else{
298       $smarty->assign("StandAlone",false);
299     }
301     /* Do we need to flip is_account state? */
302     if(isset($_POST['modify_state'])){
303       if($this->is_account && $this->acl_is_removeable()){
304         $this->is_account= FALSE;
305       }elseif(!$this->is_account && $this->acl_is_createable()){
306         $this->is_account= TRUE;
307       }
308     }
310     /* Do we represent a valid printer? */
311     if (!$this->is_account && $this->parent === NULL){
312       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
313         msgPool::noValidExtension(_("printer"))."</b>";
314       return($display);
315     }
317     /* If this is a WS / Terminal depending printer, display account state button */
318     if(!preg_match("/^Printer$/i",$this->BelongsTo)){
319       if((empty($this->cn)) && ($this->dn != "new")){
320         $display= $this->show_enable_header(_("Add printer extension"),
321             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);
322         $this->is_account= false;
323         return $display;
324       }
326       if (($this->is_account)){
327         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
328           $display= $this->show_disable_header(_("Remove printer extension"),
329               msgPool::featuresEnabled(_("printer")));
330         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
331           $display= $this->show_disable_header(_("Remove printer extension"),
332               msgPool::featuresDisabled(_("printer")));
333         }
334       }else{
335         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
336           $display= $this->show_disable_header(_("Add printer extension"),
337               msgPool::featuresEnabled(_("printer")));
338         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
339           $display= $this->show_enable_header(_("Add printer extension"),
340               msgPool::featuresDisabled(_("printer")));
341         }  
342         return ($display);
343       }
344     }
346     /* Base select dialog */
347     $once = true;
348     foreach($_POST as $name => $value){
349       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
350         $once = false;
351         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
352         $this->dialog->setCurrentBase($this->base);
353         $this->baseSelection = true;
354       }
355     }
357     /* Dialog handling for base select dialog
358      * Check if base was selected, dialog aborted etc */
359     if(is_object($this->dialog)){
361       $this->dialog->save_object();
362       if($this->baseSelection){
363         if($this->dialog->isClosed()){
364           $this->dialog = false;
365           $this->baseSelection = false;
366         }elseif($this->dialog->isSelected()){
368           /* A new base was selected, check if it is a valid one */
369           $tmp = $this->get_allowed_bases();
370           if(isset($tmp[$this->dialog->isSelected()])){
371             $this->base = $this->dialog->isSelected();
372           }
374           $this->dialog= false;
375           $this->baseSelection = false;
376         }else{
377           return($this->dialog->execute());
378         }
379       }
380     }
382     /* Fill templating stuff */
383     $smarty->assign("bases", $this->get_allowed_bases());
384     $smarty->assign("base_select", $this->base);
386     /* Assign attributes */
387     foreach ($this->attributes as $attr){
388       $smarty->assign("$attr", $this->$attr);
389     }
391     if(isset($_POST['AddUser'])){
392       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
393     }
394     if(isset($_POST['AddGroup'])){
395       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
396     }
397     if(isset($_POST['AddAdminUser'])){
398       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
399     }
400     if(isset($_POST['AddAdminGroup'])){
401       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
402     }
404     /* Display ppd configure/select dialog      */
405     if(isset($_POST['EditDriver'])){
406       if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
407         $this->dialog = $this->PPDdialogToSave;
408       }else{
409         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
410         $this->dialog->cn= $this->cn;
411       }
412     }
414     /* remove ppd */
415     if(isset($_POST['RemoveDriver'])){
416       $this->gotoPrinterPPD = array();
417       $this->PPDdialogToSave = NULL;
418     }
420     /* Close ppd dialog */
421     if(isset($_POST['ClosePPD'])){
422       unset($this->dialog);
423       $this->dialog=FALSE;
424     }
426     /* Save selected ppd */
427     if(isset($_POST['SavePPD'])){
428       $this->dialog->save_object();
429       if(count($this->dialog->check())){
430         foreach($this->dialog->check() as $msg){
431           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
432         }
433       }else{
434         $this->gotoPrinterPPD = array();
435         $this->gotoPrinterPPD = $this->dialog->save();
436         $this->PPDdialogToSave = $this->dialog;
437         unset($this->dialog);
438         $this->dialog=FALSE;
439       }
440     }
442      /* Member management, delete user / group / admin ..*/
443     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
444       foreach($_POST['UserMember'] as $mem){
445         $this->DelMember('AddUser',$mem);
446       }
447     }
449     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
450       foreach($_POST['UserMember'] as $mem){
451         $this->DelMember('AddGroup',$mem);
452       }
453     }
455     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
456       foreach($_POST['AdminMember'] as $mem){
457         $this->DelMember('AddAdminUser',$mem);
458       }
459     }
461     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
462       foreach($_POST['AdminMember'] as $mem){
463         $this->DelMember('AddAdminGroup',$mem);
464       }
465     }
467     /* Abort user / group adding dialog */
468     if(isset($_POST['PrinterCancel'])){
469       unset($this->dialog);
470       $this->dialog= FALSE;
471     }
473     /* Save selected users / groups */
474     if(isset($_POST['PrinterSave'])){
475       $this->dialog->save_object();
476       if(count($this->dialog->check())){
477         foreach($this->dialog->check() as $msg){
478           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
479         }
480       }else{
481         $data= $new = $this->dialog->save();
482         unset($data['type']);
483         foreach($data as $mem){
484           $this->AddMember($new['type'], $mem['dn']);
485         }
486         unset($this->dialog);
487         $this->dialog=FALSE;
488       }
489     }
491     /* Display dialog, if there is currently one open*/
492     if(is_object($this->dialog)){
493       $this->dialog->save_object();
494       $display = $this->dialog->execute();
495       return $display;
496     }
498     /* Parse selected ppd file */
499     $config = session::get('config');
500     if((isset($config->data['MAIN']['PPD_PATH']))&&(is_dir($config->data['MAIN']['PPD_PATH']))){
502       $path = $config->data['MAIN']['PPD_PATH'];
503       if(!preg_match("/\/$/",$path)){
504         $path = $path."/";
505       }
507       $ppdManager= new ppdManager($path);
508       if(!empty($this->gotoPrinterPPD)){
509         if((!file_exists($path.$this->gotoPrinterPPD))){
510           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
511         }else{
512           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
513         }
514       }else{
515         $smarty->assign("driverInfo", _("Not defined"));
516       }
517     }else{
518       $smarty->assign("driverInfo",_("Can't get ppd informations."));
519     }
521     /* Create user & admin user list */
522     $list=$this->generateList();
523     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
524     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
526     asort($userlist);
527     asort($adminlist);
529     if(!preg_match("/Printer/i",$this->BelongsTo)){
530       if(preg_match("/Terminal/i",$this->BelongsTo)){
531         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
532       }else{
533         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
534       }
535       $smarty->assign("cnACL"    , $this->getacl("cn",true));
536     }else{
537       $smarty->assign("desc"    ,"");
538     }
539     $smarty->assign("UserMember"    ,$this->UserMember);
540     $smarty->assign("UserMembers"   ,$userlist);
541     $smarty->assign("UserMemberKeys",array_flip($userlist));
543     $smarty->assign("AdminMember"    ,$this->AdminMember);
544     $smarty->assign("AdminMembers"   ,$adminlist);
545     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
546     if(preg_match("/Printer/i",$this->BelongsTo)){
548       /* Show main page */
549       $str = $this->netConfigDNS->execute();
550       if(is_object($this->netConfigDNS->dialog)){
551         return($str);
552       }
553       $smarty->assign("netconfig", $str);
554     } else {
555       $smarty->assign("netconfig", "");
556     }
558     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
559   }
561   function remove_from_parent()
562   {
563     /* Only remove if there was initially an account */
564     if($this->initially_was_account){
566       /* Update dn, to ensure storing as printer instead of WS / terminal
567        */
568       if(preg_match("/terminal/i",$this->BelongsTo)){
569         $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou')).",/",get_ou('printerou'),$this->dn);
570       }
572       if(preg_match("/workstation/i",$this->BelongsTo)){
573         $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
574       }
576       /* Check if this dn points to a printer, to avoid deleting something else */
577       $ldap= $this->config->get_ldap_link();
578       $ldap->cat($this->dn, array('dn',"objectClass"));
579       if(!$ldap->count()){
580         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
581         return;
582       }
584       /* Check if obejct is a printer */
585       $CheckPrinter = $ldap->fetch();
586       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
587         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
588         return;
589       }
591       /* Remove account & dns extension */ 
592       $this->netConfigDNS->remove_from_parent();
593       $ldap->rmdir($this->dn);
595       new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
596   
597       if (!$ldap->success()){
598         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
599       }
600       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
602       /* Delete references to object groups */
603       $ldap->cd ($this->config->current['BASE']);
604       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
605       while ($ldap->fetch()){
606         $og= new ogroup($this->config, $ldap->getDN());
607         unset($og->member[$this->dn]);
608         $og->save ();
609       }
611       /* Remove previously selected ppd file.*/
612       if(!empty($this->initial_PPD)){
613         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
614         $tmp->removeModifiedPPD();
615       }
616     }
617   }
620   /* Save data to object */
621   function save_object()
622   {
623     /* Create a base backup and reset the
624        base directly after calling plugin::save_object();
625        Base will be set seperatly a few lines below */
626     $base_tmp = $this->base;
627     plugin::save_object();
628     $this->base = $base_tmp;
630     if(is_object($this->netConfigDNS)){
631       $this->netConfigDNS->save_object();
632     }
633     
634     /* Set new base if allowed */
635     $tmp = $this->get_allowed_bases();
636     if(isset($_POST['base'])){
637       if(isset($tmp[$_POST['base']])){
638         $this->base= $_POST['base'];
639       }
640     }
641   }
643   /* Check supplied data */
644   function check()
645   {
646     /* Call common method to give check the hook */
647     $message= plugin::check();
648     if (preg_match("/printer/i",$this->BelongsTo)){
649       $message= array_merge($message, $this->netConfigDNS->check());
650     }
652     /* Don't display check messages if this is a template object */
653     if(isset($this->parent->by_object['workgeneric'])){
654       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
655         return $message;
656       }
657     }elseif(isset($this->parent->by_object['termgeneric'])){
658       if($this->parent->by_object['termgeneric']->cn == "default"){
659         return $message;
660       }
661     }
663     $dn= "cn=".$this->cn.get_ou('printerou').",".$this->base;
665     /* must: cn */
666     if(($this->BelongsTo == "Printer") && (empty($this->cn))){
667       $message[]= msgPool::required(_("Name"));
668     }
670     /* must: cn */
671     if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
672       $message[]= msgPool::invalid(_("Name"));
673     }
675     /* must: labeledURI */
676     if(empty($this->labeledURI)){
677       $message[]= msgPool::required(_("Printer URL"));
678     }
679     
680     /* Check if there is already an entry with this cn*/
681     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
682       $ldap= $this->config->get_ldap_link();
683       $ldap->cd ($this->base);
684       $ldap->ls("(cn=".$this->cn.")",get_ou('printerou').$this->base, array("cn"));
685       if ($ldap->count() != 0){
686         while ($attrs= $ldap->fetch()){
687           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
688             continue;
689           }
690           if ($attrs['dn'] != $this->orig_dn){
691             $message[]= msgPool::duplicated(_("Name"));
692             break;
693           }
694         }
695       }
696     }
698     return ($message);
699   }
702   /* Save to LDAP */
703   function save()
704   {
705     /* Update dn, to ensure storing as printer instead of WS / terminal
706      */
707     if(preg_match("/terminal/i",$this->BelongsTo)){
708       $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou')).",/",get_ou('printerou'),$this->dn);
709     }
711     if(preg_match("/workstation/i",$this->BelongsTo)){
712       $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
713     }
714     
715     if(!$this->is_account) return;
716     if(isset($this->parent->by_object['workgeneric'])){
717       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
718         return;
719       }
721       /* Adapt IP & mac from parent object */
722       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
723       $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
725     }elseif(isset($this->parent->by_object['termgeneric'])){
726       if($this->parent->by_object['termgeneric']->cn == "default"){
727         return;
728       }
729     
730       /* Adapt IP & mac from parent object */
731       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
732       $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
733     }
735     /* If type is still unknown, the initialisation of this printer failed, abort. */
736     if(preg_match("/unknown/i",$this->BelongsTo)){
737       return;
738     }
740     /* save ppd configuration */
741     if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
742       $this->PPDdialogToSave->save_ppd();
743     }
744     if($this->orig_dn != $this->dn){
745       if(!empty($this->gotoPrinterPPD)) {
746         $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
747         $this->PPDdialogToSave->cn = $this->cn;
748         $this->PPDdialogToSave->generateProperties();
749         $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
750       }
751     }
753     /* Remove previously selected ppd file.*/
754     if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
755       if(!empty($this->initial_PPD)){
756         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
757         $tmp->removeModifiedPPD();
758       }
759     }
761     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
762       $method="https://";
763     }else{
764       $method="http://";
765     }
766    
767     /* Get servername */
768     $server = $_SERVER['SERVER_NAME'];
769     if(tests::is_ip($server)){  
770       $server_name = gethostbyaddr($server);
771     }else{
772       $server_name = gethostbyaddr(gethostbyname($server));
773     }
775     /* If no ppd is selected, remove this attribute */
776     if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
777       $this->gotoPrinterPPD = $method.str_replace("//","/",$server_name."/ppd/".$this->gotoPrinterPPD);
778     }else{
779       $this->gotoPrinterPPD = array();
780     }
782     $dn= $this->dn;
784     /* reduce objectClasses to minimun */
785     $this->attrs['objectClass']= $this->objectclasses;
787     plugin::save();
788     $ldap= $this->config->get_ldap_link();
790     /* Remove all empty values */
791     if ($this->orig_dn == 'new'){
792       $attrs= array();
793       foreach ($this->attrs as $key => $val){
794         if (is_array($val) && count($val) == 0){
795           continue;
796         }
797         $attrs[$key]= $val;
798       }
799       $this->attrs= $attrs;
800     }
802     /* Append printer user 
803      */
804     $this->attrs['gotoUserPrinter']=array();
805     foreach($this->member['AddUser'] as $mem){
806       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
807     }
809     /* Append printer group 
810      */
811     $this->attrs['gotoGroupPrinter'] = array();
812     foreach($this->member['AddGroup'] as $mem){
813       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
814     }
816     /* Append printer admin user 
817      */
818     $this->attrs['gotoUserAdminPrinter'] = array();
819     foreach($this->member['AddAdminUser'] as $mem){
820       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
821     }
823     /* Append printer admin group 
824      */
825     $this->attrs['gotoGroupAdminPrinter']= array();
826     foreach($this->member['AddAdminGroup'] as $mem){
827       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
828     }
830     if($this->orig_dn == 'new'){
831       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
832         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
833           unset($this->attrs[$checkVar]);
834         }
835       }
836     }
838     /* Ensure to create a new object */
839     if(preg_match("/".normalizePreg(get_ou('incomingou'))."/",$this->orig_dn)){
840       $this->orig_dn = "new";
841     }
843     /* Move object in necessary*/
844     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
845       $this->move($this->orig_dn, $this->dn);
846     }
848     /* Write back to ldap */
849     $ldap= $this->config->get_ldap_link();
850     $ldap->cat($this->dn);
851     if(!$ldap->count()){
852       $ldap->cd($this->config->current['BASE']);
853       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
854       $ldap->cd($this->dn);
856       /* Remove empty values */ 
857       foreach($this->attrs as $name => $value){
858         if(empty($value)){
859           unset($this->attrs[$name]);
860         }
861       }
863       $ldap->add($this->attrs);
864       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
865       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
866     } else {
867       $ldap->cd($this->dn);
868       $this->cleanup();
869       $ldap->modify ($this->attrs); 
870       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
871       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
872     }
873     if (!$ldap->success()){
874       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
875     }
877     #if(preg_match("/printer/i",$this->BelongsTo)){
878       $this->netConfigDNS->cn = $this->cn;
879       $this->netConfigDNS->dn = $this->dn;
880       $this->netConfigDNS->save();
881     #}
882   }
884   function generateList(){
885     $a_return=array();
887     foreach($this->member as $type => $values){
888       $a_return[$type]=array();
889       foreach($values as $value){
890         if((preg_match("/Group/i",$type))){
891           if(!isset($value['description'])){
892             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
893           }else{
894             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
895           }
896         }else{
897           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
898         }
899       }
900     }
901     return($a_return);
902   }
904   /* Return plugin informations for acl handling
905       #FIXME FAIscript seams to ununsed within this class... */
906   static function plInfo()
907   {
908     return (array(
909           "plShortName"   => _("Generic"),
910           "plDescription" => _("Print generic"),
911           "plSelfModify"  => FALSE,
912           "plDepends"     => array(),
913           "plPriority"    => 4,
914           "plSection"     => array("administration"),
915           "plCategory"    => array("printer" => array("description"  => _("Printer"),
916                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
917           "plProvidedAcls"=> array(
918             "cn"                => _("Name"),
919             "base"                => _("Base") ,         
920             "description"       => _("Description"), 
921             "l"                 => _("Location"), 
922             "labeledURI"        => _("LabeledURL"), 
923             "gotoPrinterPPD"    => _("Printer PPD"),
924             "gotoUserPrinter"   => _("Permissions")) 
925           ));
926   }
929   /* Delete member */
930   function DelMember($type,$id)
931   {
932     /* Check if there was a printer "dn" given, or the "cn" */
933     foreach($this->member[$type] as $key => $printer){
934       if($printer['dn'] == $id) {
935         $id = $key;
936       }
937     }
938   
939     if(!$this->acl_is_writeable("gotoUserPrinter")){
940       msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
941       return(FALSE);
942     }
943  
944     if(isset($this->member[$type][$id])){
945       unset($this->member[$type][$id]);
946       return(TRUE);
947     }
948     return(FALSE);
949   }
952   /* Add given obejct to members */
953   function AddMember($type,$dn)
954   {
955     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
956     if(!in_array_ics($type, $types)){
957       msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
958       return(FALSE);
959     }
961     if(!$this->acl_is_writeable("gotoUserPrinter")){
962       msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
963       return(FALSE);
964     }
966     /* Get name of index attributes */
967     if(preg_match("/user/i",$type)){
968       $var = "uid";
969     }else{
970       $var = "cn";
971     }
973     $ldap = $this->config->get_ldap_link();
974     $ldap->cd($dn);
975     $ldap->cat($dn,array($var,"cn"));
976     if($ldap->count()){
978       $attrs = $ldap->fetch();
980       if(isset($attrs[$var][0])){
981         $name = $attrs[$var][0];
983         /* Check if this uid/cn is already assigned to any permission */
984         foreach($types as $ctype){
986           /* If we want to add a user, only check user/userAdmin members */
987           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
988             continue;
989           }
991           /* If we want to add a group, only check groups/adminGroups .. */
992           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
993             continue;
994           }
996           if(isset(  $this->member[$ctype][$name])){
997             msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
998             return(FALSE);
999           }
1000         }
1002         /* Everything is fine. So add the given object to members */
1003         $this->member[$type][$attrs[$var][0]] = $attrs ;
1004       }else{
1005         print_a($attrs);
1006       }
1007     }else{
1008       msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1009       return(FALSE);
1010     }
1011     return(TRUE);
1012   }
1015    /* Display generic part for server copy & paste */
1016   function getCopyDialog()
1017   {
1018     $vars = array("cn");
1019     $smarty = get_smarty();
1020     $smarty->assign("cn" ,$this->cn);
1021     $smarty->assign("object","printer");
1022     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1023     $ret = array();
1024     $ret['string'] = $str;
1025     $ret['status'] = "";
1026     return($ret);
1027   }
1030   function saveCopyDialog()
1031   {
1032     if(isset($_POST['cn'])){
1033       $this->cn = $_POST['cn'];
1034     }
1035   }
1037   function PrepareForCopyPaste($source)
1038   {
1039     plugin::PrepareForCopyPaste($source);
1040     if(isset($source['macAddress'][0])){
1041       $this->netConfigDNS->macAddress = $source['macAddress'][0];
1042     }
1043     if(isset($source['ipHostNumber'][0])){
1044       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1045     }
1047     $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1048     foreach($this->attributes as $attr){
1049       $this->$attr = $source_o->$attr;
1050     }
1051     $this->member = $source_o -> member;
1053     $this->gotoPrinterPPD = "";
1054   }
1057 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1058 ?>