Code

1e107aa93a05a028649752b12e7ab86c8f38a0a9
[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   function printgeneric ($config, $dn,$parent)
53   {
54     $this->config = $config;
55     $this->dn = $dn; 
56     
57     /* If parent was posted(the tabs object) we can detect the printer type. */
58     if($parent){
59       $this->parent = $parent;
60       $this->getTypeOfPrinter();
61     }else{
62       $this->BelongsTo = "unknown";
63       return;
64     }
66     /* Update dn, to ensure storing as printer instead of WS / terminal */
67     if($this->BelongsTo == "Terminal"){
68       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
69     }
71     if($this->BelongsTo == "Workstation"){
72       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
73     }
75     $this->orig_dn = $this->dn;
77     /* Get printer settings, possibly dn has changed */
78     plugin::plugin ($config, $this->dn);
80     /* Get is_account initially_was_account status */
81     $this->getTypeOfPrinter(true);
83     /* set orig dn to new if object is new */
84     $ldap= $this->config->get_ldap_link();
85     $ldap->cat($this->dn, array('dn'));
86     if(!$ldap->count()){
87       $this->orig_dn = "new";
88     }
89     
90     /* create dns object */
91     $this->netConfigDNS = new termDNS($this->config, $this->dn,$this->objectclasses);
93     /* Set base */
94     if ($this->dn == "new"){
95       $ui= get_userinfo();
96       $this->base= dn2base($ui->dn);
97       $this->cn= "";
98     } else {
99     
100       /* Set base and check if the extracted base exists */
101       if(preg_match("/ou=incoming,/",$this->dn)){
102         $this->base= preg_replace("/ou=incoming,/","",dn2base($this->dn));
103       }else{
104         $this->base= preg_replace("/ou=printers,ou=systems,/","",dn2base($this->dn));
105       }
107       if(!isset($this->config->idepartments[$this->base])){
108         print_red(_("Can't extract a valid base out of object dn, setting base to '%s'."),$_SESSION['CurrentMainBase']);
109         $this->base = $_SESSION['CurrentMainBase'];
110       }
111     }
113     /* Extract selected ppd */
114     if(isset($this->gotoPrinterPPD)){
115       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
116     }
118     /* Prepare different member types */ 
119     foreach(array("AddUser"       =>"gotoUserPrinter",
120           "AddGroup"      =>"gotoGroupPrinter",
121           "AddAdminUser"  =>"gotoUserAdminPrinter",
122           "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
124       /* $this->members contains all members */
125       $this->member[$type]=array();
127       if (isset($this->attrs[$attr]['count'])) {
128         unset($this->attrs[$attr]['count']);
129       }
131       if(isset($this->attrs[$attr])){
132         foreach($this->attrs[$attr] as $mem){
133           if(preg_match("/Group/",$type)){
134             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
135             if($ldap->count()){
136               $entry = $ldap->fetch();
137               $this->member[$type][$entry['cn'][0]]=$entry;
138             }
139           }else{
140             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
141             if($ldap->count()){
142               $entry = $ldap->fetch();
143               $this->member[$type][$entry['uid'][0]]=$entry;
144             }
145           }
146         }
147       }
148     }
149   }
152   /* Detect type of printer.
153    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
154    * We can detect the type printer type when comparing the tabs objects
155    */
156   function getTypeOfPrinter($UpdateAccountStatus = false)
157   {
158     /* Disable account as default
159      */  
160     $this->is_account = $this->initially_was_account = false;
162     /* Detect type of printer via parent tabs.
163      */
164     if(isset($this->parent->by_object['workgeneric'])){
166       /* Exclude templates 
167        */
168       $this->cn = $this->parent->by_object['workgeneric']->cn;
169       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
170         $this->BelongsTo = "WorkstationTemplate";
171       }else{
172         $this->BelongsTo = "Workstation";
173       }
174     }elseif(isset($this->parent->by_object['termgeneric'])){
176       /* Exclude templates 
177        */
178       $this->cn = $this->parent->by_object['termgeneric']->cn;
179       if($this->parent->by_object['termgeneric']->cn == "default"){
180         $this->BelongsTo = "TerminalTemplate";  
181       }else{
182         $this->BelongsTo = "Terminal";
183       }
184     }elseif(isset($this->parent->by_name['printgeneric'])){
185       $this->BelongsTo  = "Printer";
186     }
188     if($UpdateAccountStatus){
190       /* Set is_account / was account 
191        */
192       if($this->dn == "new"){
193         $this->initially_was_account = false;
194       }
196       /* If is printer it must be a true account.
197        */
198       if($this->BelongsTo == "Printer"){
199         $this->is_account = true;
200       }
202       /* Update dn, to ensure storing as printer instead of WS / terminal
203        */
204       if($this->BelongsTo == "Terminal"){
205         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
206       }
208       if($this->BelongsTo == "Workstation"){
209         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
210       }
212       /* Detect if this is a valid printer account;
213        */
214       $ldap = $this->config->get_ldap_link();
215       $ldap->cat($this->dn, array('objectClass'));
217       if($ldap->count()){
218         $attrs = $ldap->fetch();
219         if(in_array("gotoPrinter",$attrs['objectClass'])){
220           $this->initially_was_account = true;
221           $this->is_account             = true;
222         }else{
223           $this->is_account = false;
224         }
225       }
226     }
227   }
229   function execute()
230   {
231     /* Call parent execute */
232     plugin::execute();
234     /* If type of printer couldn't be detected (because of missing parent object in construction) 
235      * hide this tab.
236      */
237     if($this->BelongsTo == "unknown"){
238       $display= $this->show_enable_header(_("Add printer extension"),
239           _("Could not intialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
240       return($display);
241     }
243     /* Templates can't have printer extensions 
244      */
245     if($this->BelongsTo == "WorkstationTemplate"){
246       $display= $this->show_enable_header(_("Add printer extension"),
247           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
248       return($display);
249     }
250     if($this->BelongsTo == "TerminalTemplate"){
251       $display= $this->show_enable_header(_("Add printer extension"),
252           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
253       return($display);
254     }
256     /* Get cn from base object */
257     if($this->BelongsTo == "Workstation"){
258       $this->cn = $this->parent->by_object['workgeneric']->cn;
259     }
260     if($this->BelongsTo == "Terminal"){
261       $this->cn = $this->parent->by_object['termgeneric']->cn;
262     }
264     $smarty= get_smarty();
266     /* Assign acls */
267     $tmp = $this->plInfo();
268     foreach($tmp['plProvidedAcls'] as $name => $translation){
269       $smarty->assign($name."ACL", $this->getacl($name));
270     }
272     /* Create base acls */
273     $baseACL = $this->getacl("base");
274     if(!$this->acl_is_moveable()) {
275       $baseACL = preg_replace("/w/","",$baseACL);
276     }
277     $smarty->assign("baseACL",          $baseACL);
280     $display="";
282     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
283     if($this->BelongsTo == "Printer"){    
284       $smarty->assign("StandAlone",true);
285     }else{
286       $smarty->assign("StandAlone",false);
287     }
289     /* Do we need to flip is_account state? */
290     if(isset($_POST['modify_state'])){
291       if($this->is_account && $this->acl_is_removeable()){
292         $this->is_account= FALSE;
293       }elseif(!$this->is_account && $this->acl_is_createable()){
294         $this->is_account= TRUE;
295       }
296     }
298     /* Do we represent a valid printer? */
299     if (!$this->is_account && $this->parent == NULL){
300       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
301         _("This 'dn' has no printer features.")."</b>";
302       return($display);
303     }
305     /* If this is a WS / Terminal depending printer, display account state button */
306     if($this->BelongsTo != "Printer"){
307       if((empty($this->cn)) && ($this->dn != "new")){
308         $display= $this->show_enable_header(_("Add printer extension"),
309             _("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);
310         $this->is_account= false;
311         return $display;
312       }
314       if (($this->is_account)){
315         if($this->BelongsTo=="Workstation"){
316           $display= $this->show_disable_header(_("Remove printer extension"),
317               _("This workstation has printer extension enabled.You can disable it by clicking below."));
318         }elseif($this->BelongsTo=="Terminal"){
319           $display= $this->show_disable_header(_("Remove printer extension"),
320               _("This terminal has printer extension enabled. You can disable it by clicking below."));
321         }
322       }else{
323         if($this->BelongsTo=="Workstation"){
324           $display= $this->show_disable_header(_("Add printer extension"),
325               _("This workstation has printer extension disabled. You can enable it by clicking below."));
326         }elseif($this->BelongsTo=="Terminal"){
327           $display= $this->show_enable_header(_("Add printer extension"),
328               _("This terminal has printer extension disabled. You can enable it by clicking below."));
329         }  
330         return ($display);
331       }
332     }
334     /* Base select dialog */
335     $once = true;
336     foreach($_POST as $name => $value){
337       if(preg_match("/^chooseBase/",$name) && $once){
338         $once = false;
339         $this->dialog = new baseSelectDialog($this->config,$this);
340         $this->dialog->setCurrentBase($this->base);
341         $this->baseSelection = true;
342       }
343     }
345     /* Dialog handling for base select dialog
346      * Check if base was selected, dialog aborted etc */
347     if(is_object($this->dialog)){
349       $this->dialog->save_object();
350       if($this->baseSelection){
351         if($this->dialog->isClosed()){
352           $this->dialog = false;
353           $this->baseSelection = false;
354         }elseif($this->dialog->isSelected()){
355           $this->base = $this->dialog->isSelected();
356           $this->dialog= false;
357           $this->baseSelection = false;
358         }else{
359           return($this->dialog->execute());
360         }
361       }
362     }
364     /* Fill templating stuff */
365     $smarty->assign("bases", $this->config->idepartments);
366     $smarty->assign("base_select", $this->base);
368     /* Assign attributes */
369     foreach ($this->attributes as $attr){
370       $smarty->assign("$attr", $this->$attr);
371     }
373     if(isset($_POST['AddUser'])){
374       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
375     }
376     if(isset($_POST['AddGroup'])){
377       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
378     }
379     if(isset($_POST['AddAdminUser'])){
380       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
381     }
382     if(isset($_POST['AddAdminGroup'])){
383       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
384     }
386     /* Display ppd configure/select dialog      */
387     if(isset($_POST['EditDriver'])){
388       if($this->PPDdialogToSave){
389         $this->dialog = $this->PPDdialogToSave;
390       }else{
391         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
392       }
393     }
395     /* remove ppd */
396     if(isset($_POST['RemoveDriver'])){
397       $this->gotoPrinterPPD = array();
398       $this->PPDdialogToSave = NULL;
399     }
401     /* Close ppd dialog */
402     if(isset($_POST['ClosePPD'])){
403       unset($this->dialog);
404       $this->dialog=NULL;
405     }
407     /* Save selected ppd */
408     if(isset($_POST['SavePPD'])){
409       $this->dialog->save_object();
410       if(count($this->dialog->check())){
411         foreach($this->dialog->check() as $msg){
412           print_red($msg);
413         }
414       }else{
415         $this->gotoPrinterPPD = array();
416         $this->gotoPrinterPPD = $this->dialog->save();
417         $this->PPDdialogToSave = $this->dialog;
418         unset($this->dialog);
419         $this->dialog=NULL;
420       }
421     }
423      /* Member management, delete user / group / admin ..*/
424     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
425       foreach($_POST['UserMember'] as $mem){
426         $this->DelMember('AddUser',$mem);
427       }
428     }
430     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
431       foreach($_POST['UserMember'] as $mem){
432         $this->DelMember('AddGroup',$mem);
433       }
434     }
436     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
437       foreach($_POST['AdminMember'] as $mem){
438         $this->DelMember('AddAdminUser',$mem);
439       }
440     }
442     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
443       foreach($_POST['AdminMember'] as $mem){
444         $this->DelMember('AddAdminGroup',$mem);
445       }
446     }
448     /* Abort user / group adding dialog */
449     if(isset($_POST['PrinterCancel'])){
450       unset($this->dialog);
451       $this->dialog= NULL;
452     }
454     /* Save selected users / groups */
455     if(isset($_POST['PrinterSave'])){
456       $this->dialog->save_object();
457       if(count($this->dialog->check())){
458         foreach($this->dialog->check() as $msg){
459           print_red($msg);
460         }
461       }else{
462         $data= $new = $this->dialog->save();
463         unset($data['type']);
464         foreach($data as $mem){
465           $this->AddMember($new['type'], $mem['dn']);
466         }
467         unset($this->dialog);
468         $this->dialog=NULL;
469       }
470     }
472     /* Display dialog, if there is currently one open*/
473     if($this->dialog != NULL){
474       $this->dialog->save_object();
475       $display = $this->dialog->execute();
476       return $display;
477     }
479     /* Parse selected ppd file */
480     require_once ("class_ppdManager.inc");
481     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
483       $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
484       if(!preg_match("/\/$/",$path)){
485         $path = $path."/";
486       }
488       $ppdManager= new ppdManager($path);
489       if(!empty($this->gotoPrinterPPD)){
490         if((!file_exists($path.$this->gotoPrinterPPD))){
491           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD))."</b>";
492         }else{
493           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
494         }
495       }else{
496         $smarty->assign("driverInfo", _("not defined"));
497       }
498     }else{
499       $smarty->assign("driverInfo",_("can't get ppd informations."));
500     }
502     /* Create user & admin user list */
503     $list=$this->generateList();
504     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
505     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
507     asort($userlist);
508     asort($adminlist);
510     if($this->BelongsTo != "Printer"){
511       if($this->BelongsTo == "Terminal"){
512         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
513       }else{
514         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
515       }
516       $smarty->assign("cnACL"    , $this->getacl("cn",true));
517     }else{
518       $smarty->assign("desc"    ,"");
519     }
520     $smarty->assign("UserMember"    ,$this->UserMember);
521     $smarty->assign("UserMembers"   ,$userlist);
522     $smarty->assign("UserMemberKeys",array_flip($userlist));
524     $smarty->assign("AdminMember"    ,$this->AdminMember);
525     $smarty->assign("AdminMembers"   ,$adminlist);
526     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
527     if($this->BelongsTo == "Printer"){
528       $smarty->assign("netconfig", $this->netConfigDNS->execute());
529     } else {
530       $smarty->assign("netconfig", "");
531     }
533     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
534   }
536   function remove_from_parent()
537   {
538     /* Only remove if there was initially an account */
539     if($this->initially_was_account){
541       /* Update dn, to ensure storing as printer instead of WS / terminal
542        */
543       if($this->BelongsTo == "Terminal"){
544         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
545       }
547       if($this->BelongsTo == "Workstation"){
548         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
549       }
551       /* Check if this dn points to a printer, to avoid deleting something else */
552       $ldap= $this->config->get_ldap_link();
553       $ldap->cat($this->dn, array('dn',"objectClass"));
554       if(!$ldap->count()){
555         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
556         return;
557       }
559       /* Check if obejct is a printer */
560       $CheckPrinter = $ldap->fetch();
561       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
562         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
563         return;
564       }
566       /* Remove account & dns extension */ 
567       $this->netConfigDNS->remove_from_parent();
568       $ldap->rmdir($this->dn);
569       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system print/generic with dn '%s' failed."),$this->dn));
570       $this->handle_post_events("remove");
572       /* Delete references to object groups */
573       $ldap->cd ($this->config->current['BASE']);
574       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
575       while ($ldap->fetch()){
576         $og= new ogroup($this->config, $ldap->getDN());
577         unset($og->member[$this->dn]);
578         $og->save ();
579       }
580     }
581   }
584   /* Save data to object */
585   function save_object()
586   {
587     plugin::save_object();
588     $this->netConfigDNS->save_object();
590     /* Save base, since this is no LDAP attribute */
591     if((isset($_POST['base'])) && ($this->acl_is_moveable()) ) {
592       $this->set_acl_base('dummy,'.$_POST['base']);
593       if($this->acl_is_moveable()){
595         if(isset($this->config->idepartments[$_POST['base']])){
596           $this->base = $_POST['base'];
597           if ($_POST['base'] != $this->base){
598             $this->is_modified= TRUE;
599           }
600         }
601       }else{
603         print_red(sprintf(_("You are not allowed to move this object to '%s'."),LDAP::fix($_POST['base'])));
604         $this->set_acl_base('dummy,'.$this->base);
605       }
606     }
607   }
609   /* Check supplied data */
610   function check()
611   {
612     /* Call common method to give check the hook */
613     $message= plugin::check();
614     if ($this->BelongsTo == 'printer'){
615       $message= array_merge($message, $this->netConfigDNS->check());
616     }
618     /* Don't display check messages if this is a template object */
619     if(isset($this->parent->by_object['workgeneric'])){
620       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
621         return $message;
622       }
623     }elseif(isset($this->parent->by_object['termgeneric'])){
624       if($this->parent->by_object['termgeneric']->cn == "default"){
625         return $message;
626       }
627     }
629     $dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
631     /* must: cn */
632     if(($this->BelongsTo == "Printer") && (empty($this->cn))){
633       $message[]= "The required field 'Printer name' is not set.";
634     }
636     if($this->BelongsTo == "Printer"){
637       if ($this->orig_dn == "new" && !$this->acl_is_createable()){
638         $message[]= _("You have no permissions to create a group on this 'Base'.");
639       }
640     }
641     
642     /* must: labeledURI */
643     if(empty($this->labeledURI)){
644       $message[]= "The required field 'Printer URL' is not set.";
645     }
646     
647     /* Check if there is already an entry with this cn*/
648     if (($this->orig_dn != $dn)&&($this->BelongsTo == "Printer")){
649       $ldap= $this->config->get_ldap_link();
650       $ldap->cd ($this->base);
651       $ldap->ls("(cn=".$this->cn.")","ou=printers,ou=systems,".$this->base, array("cn"));
652       if ($ldap->count() != 0){
653         while ($attrs= $ldap->fetch()){
654           if ($attrs['dn'] != $this->orig_dn){
655             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
656             break;
657           }
658         }
659       }
660     }
662     return ($message);
663   }
666   /* Save to LDAP */
667   function save()
668   {
669     /* Update dn, to ensure storing as printer instead of WS / terminal
670      */
671     if($this->BelongsTo == "Terminal"){
672       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
673     }
675     if($this->BelongsTo == "Workstation"){
676       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
677     }
678     
679     if(!$this->is_account) return;
680     if(isset($this->parent->by_object['workgeneric'])){
681       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
682         return;
683       }
684     }elseif(isset($this->parent->by_object['termgeneric'])){
685       if($this->parent->by_object['termgeneric']->cn == "default"){
686         return;
687       }
688     }
690     /* If type is still unknown, the initialisation of this printer failed, abort. */
691     if($this->BelongsTo == "unknown"){
692       return;
693     }
695     /* save ppd configuration */
696     if($this->PPDdialogToSave){
697       $this->PPDdialogToSave->save_ppd();
698     }
700     $dn= $this->dn;
701     plugin::save();
702     $ldap= $this->config->get_ldap_link();
704     /* reduce objectClasses to minimun */
705     $this->attrs['objectClass']= $this->objectclasses;
707     /* Remove all empty values */
708     if ($this->orig_dn == 'new'){
709       $attrs= array();
710       foreach ($this->attrs as $key => $val){
711         if (is_array($val) && count($val) == 0){
712           continue;
713         }
714         $attrs[$key]= $val;
715       }
716       $this->attrs= $attrs;
717     }
719     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
720       $method="https://";
721     }else{
722       $method="http://";
723     }
725     /* If no ppd is selected, remove this attribute */
726     if(!empty($this->gotoPrinterPPD)) {
727       $this->attrs['gotoPrinterPPD'] = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
728     }else{
729       $this->attrs['gotoPrinterPPD'] = array();
730     }
732     /* Append printer user 
733      */
734     $this->attrs['gotoUserPrinter']=array();
735     foreach($this->member['AddUser'] as $mem){
736       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
737     }
739     /* Append printer group 
740      */
741     $this->attrs['gotoGroupPrinter'] = array();
742     foreach($this->member['AddGroup'] as $mem){
743       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
744     }
746     /* Append printer admin user 
747      */
748     $this->attrs['gotoUserAdminPrinter'] = array();
749     foreach($this->member['AddAdminUser'] as $mem){
750       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
751     }
753     /* Append printer admin group 
754      */
755     $this->attrs['gotoGroupAdminPrinter']= array();
756     foreach($this->member['AddAdminGroup'] as $mem){
757       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
758     }
760     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
761       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
762     }
764     if($this->orig_dn == 'new'){
765       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
766         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
767           unset($this->attrs[$checkVar]);
768         }
769       }
770     }
772     /* Move object in necessary*/
773     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
774       $this->move($this->orig_dn, $this->dn);
775     }
777     /* Write back to ldap */
778     $ldap= $this->config->get_ldap_link();
779     $ldap->cat($this->dn);
780     if(!$ldap->count()){
781       $ldap->cd($this->config->current['BASE']);
782       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
783       $ldap->cd($this->dn);
784       $ldap->add($this->attrs);
785       $this->handle_post_events("add");
786     } else {
787       $ldap->cd($this->dn);
788       $this->cleanup();
789       $ldap->modify ($this->attrs); 
790       $this->handle_post_events("modify");
791     }
792     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system print/generic with dn '%s' failed."),$this->dn));
794     if($this->BelongsTo == "Printer"){
795       $this->netConfigDNS->cn = $this->cn;
796       $this->netConfigDNS->dn = $this->dn;
797       $this->netConfigDNS->save($this->dn);
798     }
800     /* Optionally execute a command after we're done */
801     $this->postcreate();
803     /* This is a multi object. Handle tagging here... */
804     $this->handle_object_tagging();
805   }
807   function generateList(){
808     $a_return=array();
809     foreach($this->member as $type => $values){
810       $a_return[$type]=array();
811       foreach($values as $value){
812         if((preg_match("/Group/i",$type))){
813           if(!isset($value['description'])){
814             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
815           }else{
816             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
817           }
818         }else{
819           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
820         }
821       }
822     }
823     return($a_return);
824   }
826   /* Return plugin informations for acl handling
827       #FIXME FAIscript seams to ununsed within this class... */
828   function plInfo()
829   {
830     return (array(
831           "plShortName"   => _("Generic"),
832           "plDescription" => _("Print generic"),
833           "plSelfModify"  => FALSE,
834           "plDepends"     => array(),
835           "plPriority"    => 0,
836           "plSection"     => array("administration"),
837           "plCategory"    => array("printer" => array("description"  => _("Printer"),
838                                                     "objectClass"  => "gotoPrinter")),
839           "plProvidedAcls"=> array(
840             "cn"                => _("Name"),
841             "base"                => _("Base") ,         
842             "description"       => _("Description"), 
843             "l"                 => _("Location"), 
844             "labeledURI"        => _("LabeledURL"), 
845             "gotoPrinterPPD"    => _("Printer PPD"),
846             "gotoUserPrinter"   => _("Permissions")) 
847           ));
848   }
851   /* Delete member */
852   function DelMember($type,$id)
853   {
854     /* Check if there was a printer "dn" given, or the "cn" */
855     foreach($this->member[$type] as $key => $printer){
856       if($printer['dn'] == $id) {
857         $id = $key;
858       }
859     }
860   
861     if(!$this->acl_is_writeable("gotoUserPrinter")){
862       print_red(sprintf(_("You are not allowed to remove the given object '%s' from the list of members of printer '%s'."),$id,$this->dn));
863       return(FALSE);
864     }
865  
866     if(isset($this->member[$type][$id])){
867       unset($this->member[$type][$id]);
868       return(TRUE);
869     }
870     return(FALSE);
871   }
874   /* Add given obejct to members */
875   function AddMember($type,$dn)
876   {
877     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
878     if(!in_array_ics($type, $types)){
879       print_red(sprintf(_("Illegal printer type while adding '%s' to the list of '%s' printers,"),$dn,$type));
880       return(FALSE);
881     }
883     if(!$this->acl_is_writeable("gotoUserPrinter")){
884       print_red(sprintf(_("You are not allowed to add the given object '%s' to the list of members of '%s'."),$dn,$this->dn));
885       return(FALSE);
886     }
888     /* Get name of index attributes */
889     if(preg_match("/user/i",$type)){
890       $var = "uid";
891     }else{
892       $var = "cn";
893     }
895     $ldap = $this->config->get_ldap_link();
896     $ldap->cd($dn);
897     $ldap->cat($dn,array($var));
898     if($ldap->count()){
900       $attrs = $ldap->fetch();
902       if(isset($attrs[$var][0])){
903         $name = $attrs[$var][0];
905         /* Check if this uid/cn is already assigned to any permission */
906         foreach($types as $ctype){
908           /* If we want to add a user, only check user/userAdmin members */
909           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
910             continue;
911           }
913           /* If we want to add a group, only check groups/adminGroups .. */
914           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
915             continue;
916           }
918           if(isset(  $this->member[$ctype][$name])){
919             print_red(sprintf(_("Can't add '%s' to the list of members, it is already used."),$attrs[$var][0]));
920             return(FALSE);
921           }
922         }
924         /* Everything is fine. So add the given object to members */
925         $this->member[$type][$attrs[$var][0]] = $attrs ;
926       }else{
927         print_a($attrs);
928       }
929     }else{
930       print_red(sprintf(_("Can't add '%s' to list of members, it is not reachable."),$dn));
931       return(FALSE);
932     }
933     return(TRUE);
934   }
939 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
940 ?>