Code

Fixed Translation Strings.
[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]['count'])) {
126         unset($this->attrs[$attr]['count']);
127       }
129       if(isset($this->attrs[$attr])){
130         foreach($this->attrs[$attr] as $mem){
131           if(preg_match("/Group/",$type)){
132             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
133             if($ldap->count()){
134               $entry = $ldap->fetch();
135               $this->member[$type][$entry['cn'][0]]=$entry;
136             }
137           }else{
138             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
139             if($ldap->count()){
140               $entry = $ldap->fetch();
141               $this->member[$type][$entry['uid'][0]]=$entry;
142             }
143           }
144         }
145       }
146     }
147   }
149   function set_acl_base($base)
150   {
151     plugin::set_acl_base($base);
152     if(is_object($this->netConfigDNS)){
153       $this->netConfigDNS->set_acl_base($base);
154     }
155   }
157   function set_acl_category($cat)
158   {
159     plugin::set_acl_category($cat);
160     if(is_object($this->netConfigDNS)){
161       $this->netConfigDNS->set_acl_category($cat);
162     }
163   }
165   /* Detect type of printer.
166    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
167    * We can detect the type printer type when comparing the tabs objects
168    */
169   function getTypeOfPrinter($UpdateAccountStatus = false)
170   {
171     /* Disable account as default
172      */  
173     $this->is_account = $this->initially_was_account = false;
175     /* Detect type of printer via parent tabs.
176      */
178     $class = get_class($this->parent);
179     if(isset($this->parent->by_object['workgeneric'])){
181       /* Exclude templates 
182        */
183       $this->cn = $this->parent->by_object['workgeneric']->cn;
184       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
185         $this->BelongsTo = "WorkstationTemplate";
186       }else{
187         $this->BelongsTo = "Workstation";
188       }
189     }elseif(isset($this->parent->by_object['termgeneric'])){
191       /* Exclude templates 
192        */
193       $this->cn = $this->parent->by_object['termgeneric']->cn;
194       if($this->parent->by_object['termgeneric']->cn == "default"){
195         $this->BelongsTo = "TerminalTemplate";  
196       }else{
197         $this->BelongsTo = "Terminal";
198       }
199     }elseif(isset($this->parent->by_name['printgeneric'])){
200       $this->BelongsTo  = "Printer";
201     }
203     if($UpdateAccountStatus){
205       /* Set is_account / was account 
206        */
207       if($this->dn == "new"){
208         $this->initially_was_account = false;
209       }
211       /* If is printer it must be a true account.
212        */
213       if(preg_match("/printer/i",$this->BelongsTo)){
214         $this->is_account = true;
215       }
217       /* Update dn, to ensure storing as printer instead of WS / terminal
218        */
219       if(preg_match("/terminal/i",$this->BelongsTo)){
220         $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou'))."/",get_ou('printerou'),$this->dn);
221       }
223       if(preg_match("/workstation/i",$this->BelongsTo)){
224         $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
225       }
227       /* Detect if this is a valid printer account;
228        */
229       $ldap = $this->config->get_ldap_link();
230       $ldap->cat($this->dn, array('objectClass'));
232       if($ldap->count()){
233         $attrs = $ldap->fetch();
234         if(in_array("gotoPrinter",$attrs['objectClass'])){
235           $this->initially_was_account = true;
236           $this->is_account             = true;
237         }else{
238           $this->is_account = false;
239         }
240       }
241     }
242   }
244   function execute()
245   {
246     /* Call parent execute */
247     plugin::execute();
249     if($this->is_account && !$this->view_logged){
250       $this->view_logged = TRUE;
251       new log("view","printer/".get_class($this),$this->dn);
252     }
255     /* If type of printer couldn't be detected (because of missing parent object in construction) 
256      * hide this tab.
257      */
258     if(preg_match("/unknown/i",$this->BelongsTo)){
259       $display= $this->show_enable_header(_("Add printer extension"),
260           _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
261       return($display);
262     }
264     /* Templates can't have printer extensions 
265      */
266     if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
267       $display= $this->show_enable_header(_("Add printer extension"),
268           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
269       return($display);
270     }
271     if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
272       $display= $this->show_enable_header(_("Add printer extension"),
273           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
274       return($display);
275     }
277     /* Get cn from base object */
278     if(preg_match("/^Workstation$/i",$this->BelongsTo)){
279       $this->cn = $this->parent->by_object['workgeneric']->cn;
280     }
281     if(preg_match("/^Terminal$/i",$this->BelongsTo)){
282       $this->cn = $this->parent->by_object['termgeneric']->cn;
283     }
285     $smarty= get_smarty();
287     /* Assign acls */
288     $tmp = $this->plInfo();
289     foreach($tmp['plProvidedAcls'] as $name => $translation){
290       $smarty->assign($name."ACL", $this->getacl($name));
291     }
293     $display="";
295     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
296     if(preg_match("/^Printer$/i",$this->BelongsTo)){    
297       $smarty->assign("StandAlone",true);
298     }else{
299       $smarty->assign("StandAlone",false);
300     }
302     /* Do we need to flip is_account state? */
303     if(isset($_POST['modify_state'])){
304       if($this->is_account && $this->acl_is_removeable()){
305         $this->is_account= FALSE;
306       }elseif(!$this->is_account && $this->acl_is_createable()){
307         $this->is_account= TRUE;
308       }
309     }
311     /* Do we represent a valid printer? */
312     if (!$this->is_account && $this->parent === NULL){
313       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
314         msgPool::noValidExtension(_("printer"))."</b>";
315       return($display);
316     }
318     /* If this is a WS / Terminal depending printer, display account state button */
319     if(!preg_match("/^Printer$/i",$this->BelongsTo)){
320       if((empty($this->cn)) && ($this->dn != "new")){
321         $display= $this->show_enable_header(_("Add printer extension"),
322             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);
323         $this->is_account= false;
324         return $display;
325       }
327       if (($this->is_account)){
328         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
329           $display= $this->show_disable_header(_("Remove printer extension"),
330               msgPool::featuresEnabled(_("printer")));
331         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
332           $display= $this->show_disable_header(_("Remove printer extension"),
333               msgPool::featuresDisabled(_("printer")));
334         }
335       }else{
336         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
337           $display= $this->show_disable_header(_("Add printer extension"),
338               msgPool::featuresEnabled(_("printer")));
339         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
340           $display= $this->show_enable_header(_("Add printer extension"),
341               msgPool::featuresDisabled(_("printer")));
342         }  
343         return ($display);
344       }
345     }
347     /* Base select dialog */
348     $once = true;
349     foreach($_POST as $name => $value){
350       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
351         $once = false;
352         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
353         $this->dialog->setCurrentBase($this->base);
354         $this->baseSelection = true;
355       }
356     }
358     /* Dialog handling for base select dialog
359      * Check if base was selected, dialog aborted etc */
360     if(is_object($this->dialog)){
362       $this->dialog->save_object();
363       if($this->baseSelection){
364         if($this->dialog->isClosed()){
365           $this->dialog = false;
366           $this->baseSelection = false;
367         }elseif($this->dialog->isSelected()){
369           /* A new base was selected, check if it is a valid one */
370           $tmp = $this->get_allowed_bases();
371           if(isset($tmp[$this->dialog->isSelected()])){
372             $this->base = $this->dialog->isSelected();
373           }
375           $this->dialog= false;
376           $this->baseSelection = false;
377         }else{
378           return($this->dialog->execute());
379         }
380       }
381     }
383     /* Fill templating stuff */
384     $smarty->assign("bases", $this->get_allowed_bases());
385     $smarty->assign("base_select", $this->base);
387     /* Assign attributes */
388     foreach ($this->attributes as $attr){
389       $smarty->assign("$attr", $this->$attr);
390     }
392     if(isset($_POST['AddUser'])){
393       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
394     }
395     if(isset($_POST['AddGroup'])){
396       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
397     }
398     if(isset($_POST['AddAdminUser'])){
399       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
400     }
401     if(isset($_POST['AddAdminGroup'])){
402       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
403     }
405     /* Display ppd configure/select dialog      */
406     if(isset($_POST['EditDriver'])){
407       if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
408         $this->dialog = $this->PPDdialogToSave;
409       }else{
410         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
411         $this->dialog->cn= $this->cn;
412       }
413     }
415     /* remove ppd */
416     if(isset($_POST['RemoveDriver'])){
417       $this->gotoPrinterPPD = array();
418       $this->PPDdialogToSave = NULL;
419     }
421     /* Close ppd dialog */
422     if(isset($_POST['ClosePPD'])){
423       unset($this->dialog);
424       $this->dialog=FALSE;
425     }
427     /* Save selected ppd */
428     if(isset($_POST['SavePPD'])){
429       $this->dialog->save_object();
430       if(count($this->dialog->check())){
431         foreach($this->dialog->check() as $msg){
432           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
433         }
434       }else{
435         $this->gotoPrinterPPD = array();
436         $this->gotoPrinterPPD = $this->dialog->save();
437         $this->PPDdialogToSave = $this->dialog;
438         unset($this->dialog);
439         $this->dialog=FALSE;
440       }
441     }
443      /* Member management, delete user / group / admin ..*/
444     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
445       foreach($_POST['UserMember'] as $mem){
446         $this->DelMember('AddUser',$mem);
447       }
448     }
450     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
451       foreach($_POST['UserMember'] as $mem){
452         $this->DelMember('AddGroup',$mem);
453       }
454     }
456     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
457       foreach($_POST['AdminMember'] as $mem){
458         $this->DelMember('AddAdminUser',$mem);
459       }
460     }
462     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
463       foreach($_POST['AdminMember'] as $mem){
464         $this->DelMember('AddAdminGroup',$mem);
465       }
466     }
468     /* Abort user / group adding dialog */
469     if(isset($_POST['PrinterCancel'])){
470       unset($this->dialog);
471       $this->dialog= FALSE;
472     }
474     /* Save selected users / groups */
475     if(isset($_POST['PrinterSave'])){
476       $this->dialog->save_object();
477       if(count($this->dialog->check())){
478         foreach($this->dialog->check() as $msg){
479           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
480         }
481       }else{
482         $data= $new = $this->dialog->save();
483         unset($data['type']);
484         foreach($data as $mem){
485           $this->AddMember($new['type'], $mem['dn']);
486         }
487         unset($this->dialog);
488         $this->dialog=FALSE;
489       }
490     }
492     /* Display dialog, if there is currently one open*/
493     if(is_object($this->dialog)){
494       $this->dialog->save_object();
495       $display = $this->dialog->execute();
496       return $display;
497     }
499     /* Parse selected ppd file */
500     $config = session::get('config');
501     if((isset($config->data['MAIN']['PPD_PATH']))&&(is_dir($config->data['MAIN']['PPD_PATH']))){
503       $path = $config->data['MAIN']['PPD_PATH'];
504       if(!preg_match("/\/$/",$path)){
505         $path = $path."/";
506       }
508       $ppdManager= new ppdManager($path);
509       if(!empty($this->gotoPrinterPPD)){
510         if((!file_exists($path.$this->gotoPrinterPPD))){
511           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
512         }else{
513           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
514         }
515       }else{
516         $smarty->assign("driverInfo", _("Not defined"));
517       }
518     }else{
519       $smarty->assign("driverInfo",_("Can't get ppd informations."));
520     }
522     /* Create user & admin user list */
523     $list=$this->generateList();
524     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
525     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
527     asort($userlist);
528     asort($adminlist);
530     if(!preg_match("/Printer/i",$this->BelongsTo)){
531       if(preg_match("/Terminal/i",$this->BelongsTo)){
532         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
533       }else{
534         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
535       }
536       $smarty->assign("cnACL"    , $this->getacl("cn",true));
537     }else{
538       $smarty->assign("desc"    ,"");
539     }
540     $smarty->assign("UserMember"    ,$this->UserMember);
541     $smarty->assign("UserMembers"   ,$userlist);
542     $smarty->assign("UserMemberKeys",array_flip($userlist));
544     $smarty->assign("AdminMember"    ,$this->AdminMember);
545     $smarty->assign("AdminMembers"   ,$adminlist);
546     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
547     if(preg_match("/Printer/i",$this->BelongsTo)){
549       /* Show main page */
550       $str = $this->netConfigDNS->execute();
551       if(is_object($this->netConfigDNS->dialog)){
552         return($str);
553       }
554       $smarty->assign("netconfig", $str);
555     } else {
556       $smarty->assign("netconfig", "");
557     }
559     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
560   }
562   function remove_from_parent()
563   {
564     /* Only remove if there was initially an account */
565     if($this->initially_was_account){
567       /* Update dn, to ensure storing as printer instead of WS / terminal
568        */
569       if(preg_match("/terminal/i",$this->BelongsTo)){
570         $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou')).",/",get_ou('printerou'),$this->dn);
571       }
573       if(preg_match("/workstation/i",$this->BelongsTo)){
574         $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
575       }
577       /* Check if this dn points to a printer, to avoid deleting something else */
578       $ldap= $this->config->get_ldap_link();
579       $ldap->cat($this->dn, array('dn',"objectClass"));
580       if(!$ldap->count()){
581         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
582         return;
583       }
585       /* Check if obejct is a printer */
586       $CheckPrinter = $ldap->fetch();
587       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
588         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
589         return;
590       }
592       /* Remove account & dns extension */ 
593       $this->netConfigDNS->remove_from_parent();
594       $ldap->rmdir($this->dn);
596       new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
597   
598       if (!$ldap->success()){
599         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
600       }
601       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
603       /* Delete references to object groups */
604       $ldap->cd ($this->config->current['BASE']);
605       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
606       while ($ldap->fetch()){
607         $og= new ogroup($this->config, $ldap->getDN());
608         unset($og->member[$this->dn]);
609         $og->save ();
610       }
612       /* Remove previously selected ppd file.*/
613       if(!empty($this->initial_PPD)){
614         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
615         $tmp->removeModifiedPPD();
616       }
617     }
618   }
621   /* Save data to object */
622   function save_object()
623   {
624     /* Create a base backup and reset the
625        base directly after calling plugin::save_object();
626        Base will be set seperatly a few lines below */
627     $base_tmp = $this->base;
628     plugin::save_object();
629     $this->base = $base_tmp;
631     if(is_object($this->netConfigDNS)){
632       $this->netConfigDNS->save_object();
633     }
634     
635     /* Set new base if allowed */
636     $tmp = $this->get_allowed_bases();
637     if(isset($_POST['base'])){
638       if(isset($tmp[$_POST['base']])){
639         $this->base= $_POST['base'];
640       }
641     }
642   }
644   /* Check supplied data */
645   function check()
646   {
647     /* Call common method to give check the hook */
648     $message= plugin::check();
649     if (preg_match("/printer/i",$this->BelongsTo)){
650       $message= array_merge($message, $this->netConfigDNS->check());
651     }
653     /* Don't display check messages if this is a template object */
654     if(isset($this->parent->by_object['workgeneric'])){
655       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
656         return $message;
657       }
658     }elseif(isset($this->parent->by_object['termgeneric'])){
659       if($this->parent->by_object['termgeneric']->cn == "default"){
660         return $message;
661       }
662     }
664     $dn= "cn=".$this->cn.get_ou('printerou').",".$this->base;
666     /* must: cn */
667     if(($this->BelongsTo == "Printer") && (empty($this->cn))){
668       $message[]= msgPool::required(_("Name"));
669     }
671     /* must: cn */
672     if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
673       $message[]= msgPool::invalid(_("Name"));
674     }
676     /* must: labeledURI */
677     if(empty($this->labeledURI)){
678       $message[]= msgPool::required(_("Printer URL"));
679     }
680     
681     /* Check if there is already an entry with this cn*/
682     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
683       $ldap= $this->config->get_ldap_link();
684       $ldap->cd ($this->base);
685       $ldap->ls("(cn=".$this->cn.")",get_ou('printerou').$this->base, array("cn"));
686       if ($ldap->count() != 0){
687         while ($attrs= $ldap->fetch()){
688           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
689             continue;
690           }
691           if ($attrs['dn'] != $this->orig_dn){
692             $message[]= msgPool::duplicated(_("Name"));
693             break;
694           }
695         }
696       }
697     }
699     return ($message);
700   }
703   /* Save to LDAP */
704   function save()
705   {
706     /* Update dn, to ensure storing as printer instead of WS / terminal
707      */
708     if(preg_match("/terminal/i",$this->BelongsTo)){
709       $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou')).",/",get_ou('printerou'),$this->dn);
710     }
712     if(preg_match("/workstation/i",$this->BelongsTo)){
713       $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
714     }
715     
716     if(!$this->is_account) return;
717     if(isset($this->parent->by_object['workgeneric'])){
718       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
719         return;
720       }
721     }elseif(isset($this->parent->by_object['termgeneric'])){
722       if($this->parent->by_object['termgeneric']->cn == "default"){
723         return;
724       }
725     }
727     /* If type is still unknown, the initialisation of this printer failed, abort. */
728     if(preg_match("/unknown/i",$this->BelongsTo)){
729       return;
730     }
732     /* save ppd configuration */
733     if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
734       $this->PPDdialogToSave->save_ppd();
735     }
736     if($this->orig_dn != $this->dn){
737       if(!empty($this->gotoPrinterPPD)) {
738         $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
739         $this->PPDdialogToSave->cn = $this->cn;
740         $this->PPDdialogToSave->generateProperties();
741         $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
742       }
743     }
745     /* Remove previously selected ppd file.*/
746     if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
747       if(!empty($this->initial_PPD)){
748         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
749         $tmp->removeModifiedPPD();
750       }
751     }
753     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
754       $method="https://";
755     }else{
756       $method="http://";
757     }
759     /* If no ppd is selected, remove this attribute */
760     if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
761       $this->gotoPrinterPPD = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
762     }else{
763       $this->gotoPrinterPPD = array();
764     }
766     $dn= $this->dn;
768     /* reduce objectClasses to minimun */
769     $this->attrs['objectClass']= $this->objectclasses;
771     plugin::save();
772     $ldap= $this->config->get_ldap_link();
774     /* Remove all empty values */
775     if ($this->orig_dn == 'new'){
776       $attrs= array();
777       foreach ($this->attrs as $key => $val){
778         if (is_array($val) && count($val) == 0){
779           continue;
780         }
781         $attrs[$key]= $val;
782       }
783       $this->attrs= $attrs;
784     }
786     /* Append printer user 
787      */
788     $this->attrs['gotoUserPrinter']=array();
789     foreach($this->member['AddUser'] as $mem){
790       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
791     }
793     /* Append printer group 
794      */
795     $this->attrs['gotoGroupPrinter'] = array();
796     foreach($this->member['AddGroup'] as $mem){
797       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
798     }
800     /* Append printer admin user 
801      */
802     $this->attrs['gotoUserAdminPrinter'] = array();
803     foreach($this->member['AddAdminUser'] as $mem){
804       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
805     }
807     /* Append printer admin group 
808      */
809     $this->attrs['gotoGroupAdminPrinter']= array();
810     foreach($this->member['AddAdminGroup'] as $mem){
811       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
812     }
814     if($this->orig_dn == 'new'){
815       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
816         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
817           unset($this->attrs[$checkVar]);
818         }
819       }
820     }
822     /* Ensure to create a new object */
823     if(preg_match("/".normalizePreg(get_ou('incomingou'))."/",$this->orig_dn)){
824       $this->orig_dn = "new";
825     }
827     /* Move object in necessary*/
828     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
829       $this->move($this->orig_dn, $this->dn);
830     }
832     /* Write back to ldap */
833     $ldap= $this->config->get_ldap_link();
834     $ldap->cat($this->dn);
835     if(!$ldap->count()){
836       $ldap->cd($this->config->current['BASE']);
837       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
838       $ldap->cd($this->dn);
840       /* Remove empty values */ 
841       foreach($this->attrs as $name => $value){
842         if(empty($value)){
843           unset($this->attrs[$name]);
844         }
845       }
847       $ldap->add($this->attrs);
848       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
849       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
850     } else {
851       $ldap->cd($this->dn);
852       $this->cleanup();
853       $ldap->modify ($this->attrs); 
854       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
855       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
856     }
857     if (!$ldap->success()){
858       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
859     }
861     if(preg_match("/printer/i",$this->BelongsTo)){
862       $this->netConfigDNS->cn = $this->cn;
863       $this->netConfigDNS->dn = $this->dn;
864       $this->netConfigDNS->save();
865     }
866   }
868   function generateList(){
869     $a_return=array();
871     foreach($this->member as $type => $values){
872       $a_return[$type]=array();
873       foreach($values as $value){
874         if((preg_match("/Group/i",$type))){
875           if(!isset($value['description'])){
876             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
877           }else{
878             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
879           }
880         }else{
881           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
882         }
883       }
884     }
885     return($a_return);
886   }
888   /* Return plugin informations for acl handling
889       #FIXME FAIscript seams to ununsed within this class... */
890   static function plInfo()
891   {
892     return (array(
893           "plShortName"   => _("Generic"),
894           "plDescription" => _("Print generic"),
895           "plSelfModify"  => FALSE,
896           "plDepends"     => array(),
897           "plPriority"    => 4,
898           "plSection"     => array("administration"),
899           "plCategory"    => array("printer" => array("description"  => _("Printer"),
900                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
901           "plProvidedAcls"=> array(
902             "cn"                => _("Name"),
903             "base"                => _("Base") ,         
904             "description"       => _("Description"), 
905             "l"                 => _("Location"), 
906             "labeledURI"        => _("LabeledURL"), 
907             "gotoPrinterPPD"    => _("Printer PPD"),
908             "gotoUserPrinter"   => _("Permissions")) 
909           ));
910   }
913   /* Delete member */
914   function DelMember($type,$id)
915   {
916     /* Check if there was a printer "dn" given, or the "cn" */
917     foreach($this->member[$type] as $key => $printer){
918       if($printer['dn'] == $id) {
919         $id = $key;
920       }
921     }
922   
923     if(!$this->acl_is_writeable("gotoUserPrinter")){
924       msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
925       return(FALSE);
926     }
927  
928     if(isset($this->member[$type][$id])){
929       unset($this->member[$type][$id]);
930       return(TRUE);
931     }
932     return(FALSE);
933   }
936   /* Add given obejct to members */
937   function AddMember($type,$dn)
938   {
939     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
940     if(!in_array_ics($type, $types)){
941       msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
942       return(FALSE);
943     }
945     if(!$this->acl_is_writeable("gotoUserPrinter")){
946       msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
947       return(FALSE);
948     }
950     /* Get name of index attributes */
951     if(preg_match("/user/i",$type)){
952       $var = "uid";
953     }else{
954       $var = "cn";
955     }
957     $ldap = $this->config->get_ldap_link();
958     $ldap->cd($dn);
959     $ldap->cat($dn,array($var,"cn"));
960     if($ldap->count()){
962       $attrs = $ldap->fetch();
964       if(isset($attrs[$var][0])){
965         $name = $attrs[$var][0];
967         /* Check if this uid/cn is already assigned to any permission */
968         foreach($types as $ctype){
970           /* If we want to add a user, only check user/userAdmin members */
971           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
972             continue;
973           }
975           /* If we want to add a group, only check groups/adminGroups .. */
976           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
977             continue;
978           }
980           if(isset(  $this->member[$ctype][$name])){
981             msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
982             return(FALSE);
983           }
984         }
986         /* Everything is fine. So add the given object to members */
987         $this->member[$type][$attrs[$var][0]] = $attrs ;
988       }else{
989         print_a($attrs);
990       }
991     }else{
992       msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
993       return(FALSE);
994     }
995     return(TRUE);
996   }
999    /* Display generic part for server copy & paste */
1000   function getCopyDialog()
1001   {
1002     $vars = array("cn");
1003     $smarty = get_smarty();
1004     $smarty->assign("cn" ,$this->cn);
1005     $smarty->assign("object","printer");
1006     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1007     $ret = array();
1008     $ret['string'] = $str;
1009     $ret['status'] = "";
1010     return($ret);
1011   }
1014   function saveCopyDialog()
1015   {
1016     if(isset($_POST['cn'])){
1017       $this->cn = $_POST['cn'];
1018     }
1019   }
1021   function PrepareForCopyPaste($source)
1022   {
1023     plugin::PrepareForCopyPaste($source);
1024     if(isset($source['macAddress'][0])){
1025       $this->netConfigDNS->macAddress = $source['macAddress'][0];
1026     }
1027     if(isset($source['ipHostNumber'][0])){
1028       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1029     }
1031     $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1032     foreach($this->attributes as $attr){
1033       $this->$attr = $source_o->$attr;
1034     }
1035     $this->member = $source_o -> member;
1037     $this->gotoPrinterPPD = "";
1038   }
1041 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1042 ?>