Code

ACL: Skip listing system snapshot that we are not able to restore.
[gosa.git] / plugins / admin / systems / class_printGeneric.inc
1 <?php
3 class printgeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage terminal base objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* Generic terminal attributes */
11   var $interfaces     = array();
12   var $ignore_account = FALSE;
14   /* Needed values and lists */
15   var $base             = "";
16   var $cn               = "";
17   var $l                = "";
18   var $description      = "";
19   var $labeledURI       = "";
20   var $gotoPrinterPPD   = "";
21   var $orig_dn          = "";
23   var $UserMember       ="";
24   var $UserMembers      =array();
25   var $UserMemberKeys   =array();
27   var $AdminMember      ="";
28   var $AdminMembers     =array();
29   var $AdminMemberKeys  =array();
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 $dialog           =NULL;
38   var $netConfigDNS;
39   var $baseSelection    = false;
40   var $macAddress       = "";
42   var $gotoUserAdminPrinter;
43   var $gotoGroupAdminPrinter ;
44   var $gotoGroupPrinter;
45   var $gotoUserPrinter ;
47   /* attribute list for save action */
48   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress", 
49                               "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter");
50   var $objectclasses  = array("top", "gotoPrinter");
52   var $parent;
54   function printgeneric ($config, $dn,$parent_init,$parent)
55   {
56     $this->config = $config;
57     $this->dn = $dn; 
58  
59     /* If parent was posted(the tabs object) we can detect the printer type. */
60     if($parent){
61       $this->parent = $parent;
62       $this->getTypeOfPrinter();
63     }else{
64       $this->BelongsTo = "unknown";
65       return;
66     }
68     /* Update dn, to ensure storing as printer instead of WS / terminal */
69     if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
70       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
71     }
73     if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
74       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
75     }
77     $this->orig_dn = $this->dn;
79     /* Get printer settings, possibly dn has changed */
80     plugin::plugin ($config, $this->dn);
82     /* Get is_account initially_was_account status */
83     $this->getTypeOfPrinter(true);
85     /* set orig dn to new if object is new */
86     $ldap= $this->config->get_ldap_link();
87     $ldap->cat($this->dn, array('dn'));
88     if(!$ldap->count()){
89       $this->orig_dn = "new";
90     }
91     
92     /* create dns object */
93     $this->netConfigDNS = new termDNS($this->config, $this->dn,$this->objectclasses);
95     /* Set base */
96     if ($this->dn == "new"){
97       $ui= get_userinfo();
98       $this->base= dn2base($ui->dn);
99       $this->cn= "";
100     } else {
101     
102       /* Set base and check if the extracted base exists */
103       if(preg_match("/ou=incoming,/",$this->dn)){
104         $this->base= preg_replace("/ou=incoming,/","",dn2base($this->dn));
105       }else{
106         $this->base= preg_replace("/ou=printers,ou=systems,/","",dn2base($this->dn));
107       }
109       if(!isset($this->config->idepartments[$this->base])){
110         print_red(_("Can't extract a valid base out of object dn, setting base to '%s'."),$_SESSION['CurrentMainBase']);
111         $this->base = $_SESSION['CurrentMainBase'];
112       }
113     }
115     /* Extract selected ppd */
116     if(isset($this->gotoPrinterPPD)){
117       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
118     }
120     /* Prepare different member types */ 
121     foreach(array("AddUser"       =>"gotoUserPrinter",
122           "AddGroup"      =>"gotoGroupPrinter",
123           "AddAdminUser"  =>"gotoUserAdminPrinter",
124           "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
126       /* $this->members contains all members */
127       $this->member[$type]=array();
129       if (isset($this->attrs[$attr]['count'])) {
130         unset($this->attrs[$attr]['count']);
131       }
133       if(isset($this->attrs[$attr])){
134         foreach($this->attrs[$attr] as $mem){
135           if(preg_match("/Group/",$type)){
136             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
137             if($ldap->count()){
138               $entry = $ldap->fetch();
139               $this->member[$type][$entry['cn'][0]]=$entry;
140             }
141           }else{
142             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
143             if($ldap->count()){
144               $entry = $ldap->fetch();
145               $this->member[$type][$entry['uid'][0]]=$entry;
146             }
147           }
148         }
149       }
150     }
151   }
153   function set_acl_base($base)
154   {
155     plugin::set_acl_base($base);
156     if(is_object($this->netConfigDNS)){
157       $this->netConfigDNS->set_acl_base($base);
158     }
159   }
161   function set_acl_category($cat)
162   {
163     plugin::set_acl_category($cat);
164     if(is_object($this->netConfigDNS)){
165       $this->netConfigDNS->set_acl_category($cat);
166     }
167   }
169   /* Detect type of printer.
170    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
171    * We can detect the type printer type when comparing the tabs objects
172    */
173   function getTypeOfPrinter($UpdateAccountStatus = false)
174   {
175     /* Disable account as default
176      */  
177     $this->is_account = $this->initially_was_account = false;
179     /* Detect type of printer via parent tabs.
180      */
182     $class = get_class($this->parent);
183     if(isset($this->parent->by_object['workgeneric'])){
185       /* Exclude templates 
186        */
187       $this->cn = $this->parent->by_object['workgeneric']->cn;
188       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
189         $this->BelongsTo = "WorkstationTemplate";
190       }else{
191         $this->BelongsTo = "Workstation";
192       }
193     }elseif(isset($this->parent->by_object['termgeneric'])){
195       /* Exclude templates 
196        */
197       $this->cn = $this->parent->by_object['termgeneric']->cn;
198       if($this->parent->by_object['termgeneric']->cn == "default"){
199         $this->BelongsTo = "TerminalTemplate";  
200       }else{
201         $this->BelongsTo = "Terminal";
202       }
203     }elseif(isset($this->parent->by_name['printgeneric'])){
204       $this->BelongsTo  = "Printer";
205     }
207     if($UpdateAccountStatus){
209       /* Set is_account / was account 
210        */
211       if($this->dn == "new"){
212         $this->initially_was_account = false;
213       }
215       /* If is printer it must be a true account.
216        */
217       if(preg_match("/printer/i",$this->BelongsTo)){
218         $this->is_account = true;
219       }
221       /* Update dn, to ensure storing as printer instead of WS / terminal
222        */
223       if(preg_match("/terminal/i",$this->BelongsTo)){
224         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
225       }
227       if(preg_match("/workstation/i",$this->BelongsTo)){
228         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
229       }
231       /* Detect if this is a valid printer account;
232        */
233       $ldap = $this->config->get_ldap_link();
234       $ldap->cat($this->dn, array('objectClass'));
236       if($ldap->count()){
237         $attrs = $ldap->fetch();
238         if(in_array("gotoPrinter",$attrs['objectClass'])){
239           $this->initially_was_account = true;
240           $this->is_account             = true;
241         }else{
242           $this->is_account = false;
243         }
244       }
245     }
246   }
248   function execute()
249   {
250     /* Call parent execute */
251     plugin::execute();
253     /* If type of printer couldn't be detected (because of missing parent object in construction) 
254      * hide this tab.
255      */
256     if(preg_match("/unknown/i",$this->BelongsTo)){
257       $display= $this->show_enable_header(_("Add printer extension"),
258           _("Could not intialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
259       return($display);
260     }
262     /* Templates can't have printer extensions 
263      */
264     if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
265       $display= $this->show_enable_header(_("Add printer extension"),
266           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
267       return($display);
268     }
269     if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
270       $display= $this->show_enable_header(_("Add printer extension"),
271           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
272       return($display);
273     }
275     /* Get cn from base object */
276     if(preg_match("/^Workstation$/i",$this->BelongsTo)){
277       $this->cn = $this->parent->by_object['workgeneric']->cn;
278     }
279     if(preg_match("/^Terminal$/i",$this->BelongsTo)){
280       $this->cn = $this->parent->by_object['termgeneric']->cn;
281     }
283     $smarty= get_smarty();
285     /* Assign acls */
286     $tmp = $this->plInfo();
287     foreach($tmp['plProvidedAcls'] as $name => $translation){
288       $smarty->assign($name."ACL", $this->getacl($name));
289     }
291     $display="";
293     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
294     if(preg_match("/^Printer$/i",$this->BelongsTo)){    
295       $smarty->assign("StandAlone",true);
296     }else{
297       $smarty->assign("StandAlone",false);
298     }
300     /* Do we need to flip is_account state? */
301     if(isset($_POST['modify_state'])){
302       if($this->is_account && $this->acl_is_removeable()){
303         $this->is_account= FALSE;
304       }elseif(!$this->is_account && $this->acl_is_createable()){
305         $this->is_account= TRUE;
306       }
307     }
309     /* Do we represent a valid printer? */
310     if (!$this->is_account && $this->parent == NULL){
311       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
312         _("This 'dn' has no printer features.")."</b>";
313       return($display);
314     }
316     /* If this is a WS / Terminal depending printer, display account state button */
317     if(!preg_match("/^Printer$/i",$this->BelongsTo)){
318       if((empty($this->cn)) && ($this->dn != "new")){
319         $display= $this->show_enable_header(_("Add printer extension"),
320             _("This object has printer extension disabled. You can't enable it while 'cn' is not present in entry. Possibly you are currently creating a new terminal template"),TRUE,TRUE);
321         $this->is_account= false;
322         return $display;
323       }
325       if (($this->is_account)){
326         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
327           $display= $this->show_disable_header(_("Remove printer extension"),
328               _("This workstation has printer extension enabled.You can disable it by clicking below."));
329         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
330           $display= $this->show_disable_header(_("Remove printer extension"),
331               _("This terminal has printer extension enabled. You can disable it by clicking below."));
332         }
333       }else{
334         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
335           $display= $this->show_disable_header(_("Add printer extension"),
336               _("This workstation has printer extension disabled. You can enable it by clicking below."));
337         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
338           $display= $this->show_enable_header(_("Add printer extension"),
339               _("This terminal has printer extension disabled. You can enable it by clicking below."));
340         }  
341         return ($display);
342       }
343     }
345     /* Base select dialog */
346     $once = true;
347     foreach($_POST as $name => $value){
348       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
349         $once = false;
350         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
351         $this->dialog->setCurrentBase($this->base);
352         $this->baseSelection = true;
353       }
354     }
356     /* Dialog handling for base select dialog
357      * Check if base was selected, dialog aborted etc */
358     if(is_object($this->dialog)){
360       $this->dialog->save_object();
361       if($this->baseSelection){
362         if($this->dialog->isClosed()){
363           $this->dialog = false;
364           $this->baseSelection = false;
365         }elseif($this->dialog->isSelected()){
367           /* A new base was selected, check if it is a valid one */
368           $tmp = $this->get_allowed_bases();
369           if(isset($tmp[$this->dialog->isSelected()])){
370             $this->base = $this->dialog->isSelected();
371           }
373           $this->dialog= false;
374           $this->baseSelection = false;
375         }else{
376           return($this->dialog->execute());
377         }
378       }
379     }
381     /* Fill templating stuff */
382     $smarty->assign("bases", $this->get_allowed_bases());
383     $smarty->assign("base_select", $this->base);
385     /* Assign attributes */
386     foreach ($this->attributes as $attr){
387       $smarty->assign("$attr", $this->$attr);
388     }
390     if(isset($_POST['AddUser'])){
391       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
392     }
393     if(isset($_POST['AddGroup'])){
394       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
395     }
396     if(isset($_POST['AddAdminUser'])){
397       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
398     }
399     if(isset($_POST['AddAdminGroup'])){
400       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
401     }
403     /* Display ppd configure/select dialog      */
404     if(isset($_POST['EditDriver'])){
405       if($this->PPDdialogToSave){
406         $this->dialog = $this->PPDdialogToSave;
407       }else{
408         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
409       }
410     }
412     /* remove ppd */
413     if(isset($_POST['RemoveDriver'])){
414       $this->gotoPrinterPPD = array();
415       $this->PPDdialogToSave = NULL;
416     }
418     /* Close ppd dialog */
419     if(isset($_POST['ClosePPD'])){
420       unset($this->dialog);
421       $this->dialog=NULL;
422     }
424     /* Save selected ppd */
425     if(isset($_POST['SavePPD'])){
426       $this->dialog->save_object();
427       if(count($this->dialog->check())){
428         foreach($this->dialog->check() as $msg){
429           print_red($msg);
430         }
431       }else{
432         $this->gotoPrinterPPD = array();
433         $this->gotoPrinterPPD = $this->dialog->save();
434         $this->PPDdialogToSave = $this->dialog;
435         unset($this->dialog);
436         $this->dialog=NULL;
437       }
438     }
440      /* Member management, delete user / group / admin ..*/
441     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
442       foreach($_POST['UserMember'] as $mem){
443         $this->DelMember('AddUser',$mem);
444       }
445     }
447     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
448       foreach($_POST['UserMember'] as $mem){
449         $this->DelMember('AddGroup',$mem);
450       }
451     }
453     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
454       foreach($_POST['AdminMember'] as $mem){
455         $this->DelMember('AddAdminUser',$mem);
456       }
457     }
459     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
460       foreach($_POST['AdminMember'] as $mem){
461         $this->DelMember('AddAdminGroup',$mem);
462       }
463     }
465     /* Abort user / group adding dialog */
466     if(isset($_POST['PrinterCancel'])){
467       unset($this->dialog);
468       $this->dialog= NULL;
469     }
471     /* Save selected users / groups */
472     if(isset($_POST['PrinterSave'])){
473       $this->dialog->save_object();
474       if(count($this->dialog->check())){
475         foreach($this->dialog->check() as $msg){
476           print_red($msg);
477         }
478       }else{
479         $data= $new = $this->dialog->save();
480         unset($data['type']);
481         foreach($data as $mem){
482           $this->AddMember($new['type'], $mem['dn']);
483         }
484         unset($this->dialog);
485         $this->dialog=NULL;
486       }
487     }
489     /* Display dialog, if there is currently one open*/
490     if($this->dialog != NULL){
491       $this->dialog->save_object();
492       $display = $this->dialog->execute();
493       return $display;
494     }
496     /* Parse selected ppd file */
497     require_once ("class_ppdManager.inc");
498     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
500       $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
501       if(!preg_match("/\/$/",$path)){
502         $path = $path."/";
503       }
505       $ppdManager= new ppdManager($path);
506       if(!empty($this->gotoPrinterPPD)){
507         if((!file_exists($path.$this->gotoPrinterPPD))){
508           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
509         }else{
510           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
511         }
512       }else{
513         $smarty->assign("driverInfo", _("not defined"));
514       }
515     }else{
516       $smarty->assign("driverInfo",_("can't get ppd informations."));
517     }
519     /* Create user & admin user list */
520     $list=$this->generateList();
521     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
522     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
524     asort($userlist);
525     asort($adminlist);
527     if(!preg_match("/Printer/i",$this->BelongsTo)){
528       if(preg_match("/Terminal/i",$this->BelongsTo)){
529         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
530       }else{
531         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
532       }
533       $smarty->assign("cnACL"    , $this->getacl("cn",true));
534     }else{
535       $smarty->assign("desc"    ,"");
536     }
537     $smarty->assign("UserMember"    ,$this->UserMember);
538     $smarty->assign("UserMembers"   ,$userlist);
539     $smarty->assign("UserMemberKeys",array_flip($userlist));
541     $smarty->assign("AdminMember"    ,$this->AdminMember);
542     $smarty->assign("AdminMembers"   ,$adminlist);
543     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
544     if(preg_match("/Printer/i",$this->BelongsTo)){
545       $smarty->assign("netconfig", $this->netConfigDNS->execute());
546     } else {
547       $smarty->assign("netconfig", "");
548     }
550     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
551   }
553   function remove_from_parent()
554   {
555     /* Only remove if there was initially an account */
556     if($this->initially_was_account){
558       /* Update dn, to ensure storing as printer instead of WS / terminal
559        */
560       if(preg_match("/terminal/i",$this->BelongsTo)){
561         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
562       }
564       if(preg_match("/workstation/i",$this->BelongsTo)){
565         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
566       }
568       /* Check if this dn points to a printer, to avoid deleting something else */
569       $ldap= $this->config->get_ldap_link();
570       $ldap->cat($this->dn, array('dn',"objectClass"));
571       if(!$ldap->count()){
572         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
573         return;
574       }
576       /* Check if obejct is a printer */
577       $CheckPrinter = $ldap->fetch();
578       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
579         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
580         return;
581       }
583       /* Remove account & dns extension */ 
584       $this->netConfigDNS->remove_from_parent();
585       $ldap->rmdir($this->dn);
586       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system print/generic with dn '%s' failed."),$this->dn));
587       $this->handle_post_events("remove");
589       /* Delete references to object groups */
590       $ldap->cd ($this->config->current['BASE']);
591       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
592       while ($ldap->fetch()){
593         $og= new ogroup($this->config, $ldap->getDN());
594         unset($og->member[$this->dn]);
595         $og->save ();
596       }
597     }
598   }
601   /* Save data to object */
602   function save_object()
603   {
604     /* Create a base backup and reset the
605        base directly after calling plugin::save_object();
606        Base will be set seperatly a few lines below */
607     $base_tmp = $this->base;
608     plugin::save_object();
609     $this->base = $base_tmp;
611     if(is_object($this->netConfigDNS)){
612       $this->netConfigDNS->save_object();
613     }
614     
615     /* Set new base if allowed */
616     $tmp = $this->get_allowed_bases();
617     if(isset($_POST['base'])){
618       if(isset($tmp[$_POST['base']])){
619         $this->base= $_POST['base'];
620       }
621     }
622   }
624   /* Check supplied data */
625   function check()
626   {
627     /* Call common method to give check the hook */
628     $message= plugin::check();
629     if (preg_match("/printer/i",$this->BelongsTo)){
630       $message= array_merge($message, $this->netConfigDNS->check());
631     }
633     /* Don't display check messages if this is a template object */
634     if(isset($this->parent->by_object['workgeneric'])){
635       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
636         return $message;
637       }
638     }elseif(isset($this->parent->by_object['termgeneric'])){
639       if($this->parent->by_object['termgeneric']->cn == "default"){
640         return $message;
641       }
642     }
644     $dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
646     /* must: cn */
647     if(preg_match("/printer/i",$this->BelongsTo) && (empty($this->cn))){
648       $message[]= "The required field 'Printer name' is not set.";
649     }
651     /* Set new acl base */
652     if($this->dn == "new") {
653       $this->set_acl_base($this->base);
654     }
656     if(preg_match("/printer/i",$this->BelongsTo)){
657       if ($this->orig_dn == "new" && !$this->acl_is_createable()){
658         $message[]= _("You have no permissions to create a group on this 'Base'.");
659       }
660     }
661     
662     /* must: labeledURI */
663     if(empty($this->labeledURI)){
664       $message[]= "The required field 'Printer URL' is not set.";
665     }
666     
667     /* Check if there is already an entry with this cn*/
668     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
669       $ldap= $this->config->get_ldap_link();
670       $ldap->cd ($this->base);
671       $ldap->ls("(cn=".$this->cn.")","ou=printers,ou=systems,".$this->base, array("cn"));
672       if ($ldap->count() != 0){
673         while ($attrs= $ldap->fetch()){
674           if ($attrs['dn'] != $this->orig_dn){
675             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
676             break;
677           }
678         }
679       }
680     }
682     return ($message);
683   }
686   /* Save to LDAP */
687   function save()
688   {
689     /* Update dn, to ensure storing as printer instead of WS / terminal
690      */
691     if(preg_match("/terminal/i",$this->BelongsTo)){
692       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
693     }
695     if(preg_match("/workstation/i",$this->BelongsTo)){
696       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
697     }
698     
699     if(!$this->is_account) return;
700     if(isset($this->parent->by_object['workgeneric'])){
701       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
702         return;
703       }
704     }elseif(isset($this->parent->by_object['termgeneric'])){
705       if($this->parent->by_object['termgeneric']->cn == "default"){
706         return;
707       }
708     }
710     /* If type is still unknown, the initialisation of this printer failed, abort. */
711     if(preg_match("/unknown/i",$this->BelongsTo)){
712       return;
713     }
715     /* save ppd configuration */
716     if($this->PPDdialogToSave){
717       $this->PPDdialogToSave->save_ppd();
718     }
720     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
721       $method="https://";
722     }else{
723       $method="http://";
724     }
726     /* If no ppd is selected, remove this attribute */
727     if(!empty($this->gotoPrinterPPD)) {
728       $this->gotoPrinterPPD = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
729     }else{
730       $this->gotoPrinterPPD = array();
731     }
733     $dn= $this->dn;
734     plugin::save();
735     $ldap= $this->config->get_ldap_link();
737     /* reduce objectClasses to minimun */
738     $this->attrs['objectClass']= $this->objectclasses;
740     /* Remove all empty values */
741     if ($this->orig_dn == 'new'){
742       $attrs= array();
743       foreach ($this->attrs as $key => $val){
744         if (is_array($val) && count($val) == 0){
745           continue;
746         }
747         $attrs[$key]= $val;
748       }
749       $this->attrs= $attrs;
750     }
752     /* Append printer user 
753      */
754     $this->attrs['gotoUserPrinter']=array();
755     foreach($this->member['AddUser'] as $mem){
756       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
757     }
759     /* Append printer group 
760      */
761     $this->attrs['gotoGroupPrinter'] = array();
762     foreach($this->member['AddGroup'] as $mem){
763       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
764     }
766     /* Append printer admin user 
767      */
768     $this->attrs['gotoUserAdminPrinter'] = array();
769     foreach($this->member['AddAdminUser'] as $mem){
770       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
771     }
773     /* Append printer admin group 
774      */
775     $this->attrs['gotoGroupAdminPrinter']= array();
776     foreach($this->member['AddAdminGroup'] as $mem){
777       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
778     }
780     if($this->orig_dn == 'new'){
781       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
782         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
783           unset($this->attrs[$checkVar]);
784         }
785       }
786     }else{
787       if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
788         $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
789       }
790     }
792     /* Move object in necessary*/
793     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
794       $this->move($this->orig_dn, $this->dn);
795     }
797     /* Write back to ldap */
798     $ldap= $this->config->get_ldap_link();
799     $ldap->cat($this->dn);
800     if(!$ldap->count()){
801       $ldap->cd($this->config->current['BASE']);
802       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
803       $ldap->cd($this->dn);
804       $ldap->add($this->attrs);
805       $this->handle_post_events("add");
806     } else {
807       $ldap->cd($this->dn);
808       $this->cleanup();
809       $ldap->modify ($this->attrs); 
810       $this->handle_post_events("modify");
811     }
812     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system print/generic with dn '%s' failed."),$this->dn));
814     if(preg_match("/printer/i",$this->BelongsTo)){
815       $this->netConfigDNS->cn = $this->cn;
816       $this->netConfigDNS->dn = $this->dn;
817       $this->netConfigDNS->save($this->dn);
818     }
820     /* Optionally execute a command after we're done */
821     $this->postcreate();
823     /* This is a multi object. Handle tagging here... */
824     $this->handle_object_tagging();
825   }
827   function generateList(){
828     $a_return=array();
830     foreach($this->member as $type => $values){
831       $a_return[$type]=array();
832       foreach($values as $value){
833         if((preg_match("/Group/i",$type))){
834           if(!isset($value['description'])){
835             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
836           }else{
837             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
838           }
839         }else{
840           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
841         }
842       }
843     }
844     return($a_return);
845   }
847   /* Return plugin informations for acl handling
848       #FIXME FAIscript seams to ununsed within this class... */
849   function plInfo()
850   {
851     return (array(
852           "plShortName"   => _("Generic"),
853           "plDescription" => _("Print generic"),
854           "plSelfModify"  => FALSE,
855           "plDepends"     => array(),
856           "plPriority"    => 4,
857           "plSection"     => array("administration"),
858           "plCategory"    => array("printer" => array("description"  => _("Printer"),
859                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
860           "plProvidedAcls"=> array(
861             "cn"                => _("Name"),
862             "base"                => _("Base") ,         
863             "description"       => _("Description"), 
864             "l"                 => _("Location"), 
865             "labeledURI"        => _("LabeledURL"), 
866             "gotoPrinterPPD"    => _("Printer PPD"),
867             "gotoUserPrinter"   => _("Permissions")) 
868           ));
869   }
872   /* Delete member */
873   function DelMember($type,$id)
874   {
875     /* Check if there was a printer "dn" given, or the "cn" */
876     foreach($this->member[$type] as $key => $printer){
877       if($printer['dn'] == $id) {
878         $id = $key;
879       }
880     }
881   
882     if(!$this->acl_is_writeable("gotoUserPrinter")){
883       print_red(sprintf(_("You are not allowed to remove the given object '%s' from the list of members of printer '%s'."),$id,$this->dn));
884       return(FALSE);
885     }
886  
887     if(isset($this->member[$type][$id])){
888       unset($this->member[$type][$id]);
889       return(TRUE);
890     }
891     return(FALSE);
892   }
895   /* Add given obejct to members */
896   function AddMember($type,$dn)
897   {
898     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
899     if(!in_array_ics($type, $types)){
900       print_red(sprintf(_("Illegal printer type while adding '%s' to the list of '%s' printers,"),$dn,$type));
901       return(FALSE);
902     }
904     if(!$this->acl_is_writeable("gotoUserPrinter")){
905       print_red(sprintf(_("You are not allowed to add the given object '%s' to the list of members of '%s'."),$dn,$this->dn));
906       return(FALSE);
907     }
909     /* Get name of index attributes */
910     if(preg_match("/user/i",$type)){
911       $var = "uid";
912     }else{
913       $var = "cn";
914     }
916     $ldap = $this->config->get_ldap_link();
917     $ldap->cd($dn);
918     $ldap->cat($dn,array($var,"cn"));
919     if($ldap->count()){
921       $attrs = $ldap->fetch();
923       if(isset($attrs[$var][0])){
924         $name = $attrs[$var][0];
926         /* Check if this uid/cn is already assigned to any permission */
927         foreach($types as $ctype){
929           /* If we want to add a user, only check user/userAdmin members */
930           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
931             continue;
932           }
934           /* If we want to add a group, only check groups/adminGroups .. */
935           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
936             continue;
937           }
939           if(isset(  $this->member[$ctype][$name])){
940             print_red(sprintf(_("Can't add '%s' to the list of members, it is already used."),$attrs[$var][0]));
941             return(FALSE);
942           }
943         }
945         /* Everything is fine. So add the given object to members */
946         $this->member[$type][$attrs[$var][0]] = $attrs ;
947       }else{
948         print_a($attrs);
949       }
950     }else{
951       print_red(sprintf(_("Can't add '%s' to list of members, it is not reachable."),$dn));
952       return(FALSE);
953     }
954     return(TRUE);
955   }
960 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
961 ?>