Code

Updated terminal copy & paste
[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 $initial_PPD      = "";
22   var $orig_dn          = "";
24   var $UserMember       ="";
25   var $UserMembers      =array();
26   var $UserMemberKeys   =array();
28   var $AdminMember      ="";
29   var $AdminMembers     =array();
30   var $AdminMemberKeys  =array();
32   var $PPDdialogToSave  = NULL;
33   var $BelongsTo        = "unknown"; //  Specifies if this is a standalone printer, or belongs to a terminal / WS
35   var $member           =array();
36   var $strings          = "";
37   var $dialog           =NULL;
39   var $netConfigDNS;
40   var $baseSelection    = false;
41   var $macAddress       = "";
43   var $gotoUserAdminPrinter;
44   var $gotoGroupAdminPrinter ;
45   var $gotoGroupPrinter;
46   var $gotoUserPrinter ;
48   /* attribute list for save action */
49   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress", 
50                               "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter");
51   var $objectclasses  = array("top", "gotoPrinter");
52   var $view_logged    = FALSE;
53   var $parent;
55   function printgeneric ($config, $dn,$parent_init,$parent)
56   {
57     $this->config = $config;
58     $this->dn = $dn; 
59  
60     /* If parent was posted(the tabs object) we can detect the printer type. */
61     if($parent){
62       $this->parent = $parent;
63       $this->getTypeOfPrinter();
64     }else{
65       $this->BelongsTo = "unknown";
66       return;
67     }
69     /* Update dn, to ensure storing as printer instead of WS / terminal */
70     if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
71       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
72     }
74     if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
75       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
76     }
78     $this->orig_dn = $this->dn;
80     /* Get printer settings, possibly dn has changed */
81     plugin::plugin ($config, $this->dn);
83     /* Get is_account initially_was_account status */
84     $this->getTypeOfPrinter(true);
86     /* set orig dn to new if object is new */
87     $ldap= $this->config->get_ldap_link();
88     $ldap->cat($this->dn, array('dn'));
89     if(!$ldap->count()){
90       $this->orig_dn = "new";
91     }
92     
93     /* create dns object */
94     $this->netConfigDNS = new termDNS($this->config, $this->dn,$this->objectclasses);
96     /* Set base */
97     if ($this->dn == "new"){
98       $ui= get_userinfo();
99       $this->base= dn2base($ui->dn);
100       $this->cn= "";
101     } else {
102     
103       /* Set base and check if the extracted base exists */
104       if(preg_match("/ou=incoming,/",$this->dn)){
105         $this->base= preg_replace("/ou=incoming,/","",dn2base($this->dn));
106       }else{
107         $this->base= preg_replace("/ou=printers,ou=systems,/","",dn2base($this->dn));
108       }
110       if(!isset($this->config->idepartments[$this->base])){
111         print_red(_("Can't extract a valid base out of object dn, setting base to '%s'."),$_SESSION['CurrentMainBase']);
112         $this->base = $_SESSION['CurrentMainBase'];
113       }
114     }
116     /* Extract selected ppd */
117     if(isset($this->gotoPrinterPPD)){
118       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
119     }
121     $this->initial_PPD = $this->gotoPrinterPPD;
123     /* Prepare different member types */ 
124     foreach(array("AddUser"       =>"gotoUserPrinter",
125           "AddGroup"      =>"gotoGroupPrinter",
126           "AddAdminUser"  =>"gotoUserAdminPrinter",
127           "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
129       /* $this->members contains all members */
130       $this->member[$type]=array();
132       if (isset($this->attrs[$attr]['count'])) {
133         unset($this->attrs[$attr]['count']);
134       }
136       if(isset($this->attrs[$attr])){
137         foreach($this->attrs[$attr] as $mem){
138           if(preg_match("/Group/",$type)){
139             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
140             if($ldap->count()){
141               $entry = $ldap->fetch();
142               $this->member[$type][$entry['cn'][0]]=$entry;
143             }
144           }else{
145             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
146             if($ldap->count()){
147               $entry = $ldap->fetch();
148               $this->member[$type][$entry['uid'][0]]=$entry;
149             }
150           }
151         }
152       }
153     }
154   }
156   function set_acl_base($base)
157   {
158     plugin::set_acl_base($base);
159     if(is_object($this->netConfigDNS)){
160       $this->netConfigDNS->set_acl_base($base);
161     }
162   }
164   function set_acl_category($cat)
165   {
166     plugin::set_acl_category($cat);
167     if(is_object($this->netConfigDNS)){
168       $this->netConfigDNS->set_acl_category($cat);
169     }
170   }
172   /* Detect type of printer.
173    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
174    * We can detect the type printer type when comparing the tabs objects
175    */
176   function getTypeOfPrinter($UpdateAccountStatus = false)
177   {
178     /* Disable account as default
179      */  
180     $this->is_account = $this->initially_was_account = false;
182     /* Detect type of printer via parent tabs.
183      */
185     $class = get_class($this->parent);
186     if(isset($this->parent->by_object['workgeneric'])){
188       /* Exclude templates 
189        */
190       $this->cn = $this->parent->by_object['workgeneric']->cn;
191       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
192         $this->BelongsTo = "WorkstationTemplate";
193       }else{
194         $this->BelongsTo = "Workstation";
195       }
196     }elseif(isset($this->parent->by_object['termgeneric'])){
198       /* Exclude templates 
199        */
200       $this->cn = $this->parent->by_object['termgeneric']->cn;
201       if($this->parent->by_object['termgeneric']->cn == "default"){
202         $this->BelongsTo = "TerminalTemplate";  
203       }else{
204         $this->BelongsTo = "Terminal";
205       }
206     }elseif(isset($this->parent->by_name['printgeneric'])){
207       $this->BelongsTo  = "Printer";
208     }
210     if($UpdateAccountStatus){
212       /* Set is_account / was account 
213        */
214       if($this->dn == "new"){
215         $this->initially_was_account = false;
216       }
218       /* If is printer it must be a true account.
219        */
220       if(preg_match("/printer/i",$this->BelongsTo)){
221         $this->is_account = true;
222       }
224       /* Update dn, to ensure storing as printer instead of WS / terminal
225        */
226       if(preg_match("/terminal/i",$this->BelongsTo)){
227         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
228       }
230       if(preg_match("/workstation/i",$this->BelongsTo)){
231         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
232       }
234       /* Detect if this is a valid printer account;
235        */
236       $ldap = $this->config->get_ldap_link();
237       $ldap->cat($this->dn, array('objectClass'));
239       if($ldap->count()){
240         $attrs = $ldap->fetch();
241         if(in_array("gotoPrinter",$attrs['objectClass'])){
242           $this->initially_was_account = true;
243           $this->is_account             = true;
244         }else{
245           $this->is_account = false;
246         }
247       }
248     }
249   }
251   function execute()
252   {
253     /* Call parent execute */
254     plugin::execute();
256     if($this->is_account && !$this->view_logged){
257       $this->view_logged = TRUE;
258       new log("view","printer/".get_class($this),$this->dn);
259     }
262     /* If type of printer couldn't be detected (because of missing parent object in construction) 
263      * hide this tab.
264      */
265     if(preg_match("/unknown/i",$this->BelongsTo)){
266       $display= $this->show_enable_header(_("Add printer extension"),
267           _("Could not intialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
268       return($display);
269     }
271     /* Templates can't have printer extensions 
272      */
273     if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
274       $display= $this->show_enable_header(_("Add printer extension"),
275           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
276       return($display);
277     }
278     if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
279       $display= $this->show_enable_header(_("Add printer extension"),
280           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
281       return($display);
282     }
284     /* Get cn from base object */
285     if(preg_match("/^Workstation$/i",$this->BelongsTo)){
286       $this->cn = $this->parent->by_object['workgeneric']->cn;
287     }
288     if(preg_match("/^Terminal$/i",$this->BelongsTo)){
289       $this->cn = $this->parent->by_object['termgeneric']->cn;
290     }
292     $smarty= get_smarty();
294     /* Assign acls */
295     $tmp = $this->plInfo();
296     foreach($tmp['plProvidedAcls'] as $name => $translation){
297       $smarty->assign($name."ACL", $this->getacl($name));
298     }
300     $display="";
302     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
303     if(preg_match("/^Printer$/i",$this->BelongsTo)){    
304       $smarty->assign("StandAlone",true);
305     }else{
306       $smarty->assign("StandAlone",false);
307     }
309     /* Do we need to flip is_account state? */
310     if(isset($_POST['modify_state'])){
311       if($this->is_account && $this->acl_is_removeable()){
312         $this->is_account= FALSE;
313       }elseif(!$this->is_account && $this->acl_is_createable()){
314         $this->is_account= TRUE;
315       }
316     }
318     /* Do we represent a valid printer? */
319     if (!$this->is_account && $this->parent == NULL){
320       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
321         _("This 'dn' has no printer features.")."</b>";
322       return($display);
323     }
325     /* If this is a WS / Terminal depending printer, display account state button */
326     if(!preg_match("/^Printer$/i",$this->BelongsTo)){
327       if((empty($this->cn)) && ($this->dn != "new")){
328         $display= $this->show_enable_header(_("Add printer extension"),
329             _("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);
330         $this->is_account= false;
331         return $display;
332       }
334       if (($this->is_account)){
335         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
336           $display= $this->show_disable_header(_("Remove printer extension"),
337               _("This workstation has printer extension enabled.You can disable it by clicking below."));
338         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
339           $display= $this->show_disable_header(_("Remove printer extension"),
340               _("This terminal has printer extension enabled. You can disable it by clicking below."));
341         }
342       }else{
343         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
344           $display= $this->show_disable_header(_("Add printer extension"),
345               _("This workstation has printer extension disabled. You can enable it by clicking below."));
346         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
347           $display= $this->show_enable_header(_("Add printer extension"),
348               _("This terminal has printer extension disabled. You can enable it by clicking below."));
349         }  
350         return ($display);
351       }
352     }
354     /* Base select dialog */
355     $once = true;
356     foreach($_POST as $name => $value){
357       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
358         $once = false;
359         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
360         $this->dialog->setCurrentBase($this->base);
361         $this->baseSelection = true;
362       }
363     }
365     /* Dialog handling for base select dialog
366      * Check if base was selected, dialog aborted etc */
367     if(is_object($this->dialog)){
369       $this->dialog->save_object();
370       if($this->baseSelection){
371         if($this->dialog->isClosed()){
372           $this->dialog = false;
373           $this->baseSelection = false;
374         }elseif($this->dialog->isSelected()){
376           /* A new base was selected, check if it is a valid one */
377           $tmp = $this->get_allowed_bases();
378           if(isset($tmp[$this->dialog->isSelected()])){
379             $this->base = $this->dialog->isSelected();
380           }
382           $this->dialog= false;
383           $this->baseSelection = false;
384         }else{
385           return($this->dialog->execute());
386         }
387       }
388     }
390     /* Fill templating stuff */
391     $smarty->assign("bases", $this->get_allowed_bases());
392     $smarty->assign("base_select", $this->base);
394     /* Assign attributes */
395     foreach ($this->attributes as $attr){
396       $smarty->assign("$attr", $this->$attr);
397     }
399     if(isset($_POST['AddUser'])){
400       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
401     }
402     if(isset($_POST['AddGroup'])){
403       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
404     }
405     if(isset($_POST['AddAdminUser'])){
406       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
407     }
408     if(isset($_POST['AddAdminGroup'])){
409       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
410     }
412     /* Display ppd configure/select dialog      */
413     if(isset($_POST['EditDriver'])){
414       if($this->PPDdialogToSave){
415         $this->dialog = $this->PPDdialogToSave;
416       }else{
417         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
418         $this->dialog->cn= $this->cn;
419       }
420     }
422     /* remove ppd */
423     if(isset($_POST['RemoveDriver'])){
424       $this->gotoPrinterPPD = array();
425       $this->PPDdialogToSave = NULL;
426     }
428     /* Close ppd dialog */
429     if(isset($_POST['ClosePPD'])){
430       unset($this->dialog);
431       $this->dialog=NULL;
432     }
434     /* Save selected ppd */
435     if(isset($_POST['SavePPD'])){
436       $this->dialog->save_object();
437       if(count($this->dialog->check())){
438         foreach($this->dialog->check() as $msg){
439           print_red($msg);
440         }
441       }else{
442         $this->gotoPrinterPPD = array();
443         $this->gotoPrinterPPD = $this->dialog->save();
444         $this->PPDdialogToSave = $this->dialog;
445         unset($this->dialog);
446         $this->dialog=NULL;
447       }
448     }
450      /* Member management, delete user / group / admin ..*/
451     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
452       foreach($_POST['UserMember'] as $mem){
453         $this->DelMember('AddUser',$mem);
454       }
455     }
457     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
458       foreach($_POST['UserMember'] as $mem){
459         $this->DelMember('AddGroup',$mem);
460       }
461     }
463     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
464       foreach($_POST['AdminMember'] as $mem){
465         $this->DelMember('AddAdminUser',$mem);
466       }
467     }
469     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
470       foreach($_POST['AdminMember'] as $mem){
471         $this->DelMember('AddAdminGroup',$mem);
472       }
473     }
475     /* Abort user / group adding dialog */
476     if(isset($_POST['PrinterCancel'])){
477       unset($this->dialog);
478       $this->dialog= NULL;
479     }
481     /* Save selected users / groups */
482     if(isset($_POST['PrinterSave'])){
483       $this->dialog->save_object();
484       if(count($this->dialog->check())){
485         foreach($this->dialog->check() as $msg){
486           print_red($msg);
487         }
488       }else{
489         $data= $new = $this->dialog->save();
490         unset($data['type']);
491         foreach($data as $mem){
492           $this->AddMember($new['type'], $mem['dn']);
493         }
494         unset($this->dialog);
495         $this->dialog=NULL;
496       }
497     }
499     /* Display dialog, if there is currently one open*/
500     if($this->dialog != NULL){
501       $this->dialog->save_object();
502       $display = $this->dialog->execute();
503       return $display;
504     }
506     /* Parse selected ppd file */
507     require_once ("class_ppdManager.inc");
508     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
510       $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
511       if(!preg_match("/\/$/",$path)){
512         $path = $path."/";
513       }
515       $ppdManager= new ppdManager($path);
516       if(!empty($this->gotoPrinterPPD)){
517         if((!file_exists($path.$this->gotoPrinterPPD))){
518           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
519         }else{
520           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
521         }
522       }else{
523         $smarty->assign("driverInfo", _("not defined"));
524       }
525     }else{
526       $smarty->assign("driverInfo",_("can't get ppd informations."));
527     }
529     /* Create user & admin user list */
530     $list=$this->generateList();
531     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
532     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
534     asort($userlist);
535     asort($adminlist);
537     if(!preg_match("/Printer/i",$this->BelongsTo)){
538       if(preg_match("/Terminal/i",$this->BelongsTo)){
539         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
540       }else{
541         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
542       }
543       $smarty->assign("cnACL"    , $this->getacl("cn",true));
544     }else{
545       $smarty->assign("desc"    ,"");
546     }
547     $smarty->assign("UserMember"    ,$this->UserMember);
548     $smarty->assign("UserMembers"   ,$userlist);
549     $smarty->assign("UserMemberKeys",array_flip($userlist));
551     $smarty->assign("AdminMember"    ,$this->AdminMember);
552     $smarty->assign("AdminMembers"   ,$adminlist);
553     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
554     if(preg_match("/Printer/i",$this->BelongsTo)){
555       $smarty->assign("netconfig", $this->netConfigDNS->execute());
556     } else {
557       $smarty->assign("netconfig", "");
558     }
560     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
561   }
563   function remove_from_parent()
564   {
565     /* Only remove if there was initially an account */
566     if($this->initially_was_account){
568       /* Update dn, to ensure storing as printer instead of WS / terminal
569        */
570       if(preg_match("/terminal/i",$this->BelongsTo)){
571         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
572       }
574       if(preg_match("/workstation/i",$this->BelongsTo)){
575         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
576       }
578       /* Check if this dn points to a printer, to avoid deleting something else */
579       $ldap= $this->config->get_ldap_link();
580       $ldap->cat($this->dn, array('dn',"objectClass"));
581       if(!$ldap->count()){
582         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
583         return;
584       }
586       /* Check if obejct is a printer */
587       $CheckPrinter = $ldap->fetch();
588       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
589         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
590         return;
591       }
593       /* Remove account & dns extension */ 
594       $this->netConfigDNS->remove_from_parent();
595       $ldap->rmdir($this->dn);
597       new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
598   
599       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system print/generic with dn '%s' failed."),$this->dn));
600       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
602       /* Delete references to object groups */
603       $ldap->cd ($this->config->current['BASE']);
604       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
605       while ($ldap->fetch()){
606         $og= new ogroup($this->config, $ldap->getDN());
607         unset($og->member[$this->dn]);
608         $og->save ();
609       }
611       /* Remove previously selected ppd file.*/
612       if(!empty($this->initial_PPD)){
613         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
614         $tmp->removeModifiedPPD();
615       }
616     }
617   }
620   /* Save data to object */
621   function save_object()
622   {
623     /* Create a base backup and reset the
624        base directly after calling plugin::save_object();
625        Base will be set seperatly a few lines below */
626     $base_tmp = $this->base;
627     plugin::save_object();
628     $this->base = $base_tmp;
630     if(is_object($this->netConfigDNS)){
631       $this->netConfigDNS->save_object();
632     }
633     
634     /* Set new base if allowed */
635     $tmp = $this->get_allowed_bases();
636     if(isset($_POST['base'])){
637       if(isset($tmp[$_POST['base']])){
638         $this->base= $_POST['base'];
639       }
640     }
641   }
643   /* Check supplied data */
644   function check()
645   {
646     /* Call common method to give check the hook */
647     $message= plugin::check();
648     if (preg_match("/printer/i",$this->BelongsTo)){
649       $message= array_merge($message, $this->netConfigDNS->check());
650     }
652     /* Don't display check messages if this is a template object */
653     if(isset($this->parent->by_object['workgeneric'])){
654       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
655         return $message;
656       }
657     }elseif(isset($this->parent->by_object['termgeneric'])){
658       if($this->parent->by_object['termgeneric']->cn == "default"){
659         return $message;
660       }
661     }
663     $dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
665     /* must: cn */
666     if(preg_match("/printer/i",$this->BelongsTo) && (empty($this->cn))){
667       $message[]= "The required field 'Printer name' is not set.";
668     }
670     /* must: labeledURI */
671     if(empty($this->labeledURI)){
672       $message[]= "The required field 'Printer URL' is not set.";
673     }
674     
675     /* Check if there is already an entry with this cn*/
676     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
677       $ldap= $this->config->get_ldap_link();
678       $ldap->cd ($this->base);
679       $ldap->ls("(cn=".$this->cn.")","ou=printers,ou=systems,".$this->base, array("cn"));
680       if ($ldap->count() != 0){
681         while ($attrs= $ldap->fetch()){
682           if ($attrs['dn'] != $this->orig_dn){
683             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
684             break;
685           }
686         }
687       }
688     }
690     return ($message);
691   }
694   /* Save to LDAP */
695   function save()
696   {
697     /* Update dn, to ensure storing as printer instead of WS / terminal
698      */
699     if(preg_match("/terminal/i",$this->BelongsTo)){
700       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
701     }
703     if(preg_match("/workstation/i",$this->BelongsTo)){
704       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
705     }
706     
707     if(!$this->is_account) return;
708     if(isset($this->parent->by_object['workgeneric'])){
709       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
710         return;
711       }
712     }elseif(isset($this->parent->by_object['termgeneric'])){
713       if($this->parent->by_object['termgeneric']->cn == "default"){
714         return;
715       }
716     }
718     /* If type is still unknown, the initialisation of this printer failed, abort. */
719     if(preg_match("/unknown/i",$this->BelongsTo)){
720       return;
721     }
723     /* save ppd configuration */
724     if($this->PPDdialogToSave){
725       $this->PPDdialogToSave->save_ppd();
726     }
728     /* Remove previously selected ppd file.*/
729     if($this->initial_PPD != $this->gotoPrinterPPD){
730       if(!empty($this->initial_PPD)){
731         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
732         $tmp->removeModifiedPPD();
733       }
734     }
736     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
737       $method="https://";
738     }else{
739       $method="http://";
740     }
742     /* If no ppd is selected, remove this attribute */
743     if(!empty($this->gotoPrinterPPD)) {
744       $this->gotoPrinterPPD = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
745     }else{
746       $this->gotoPrinterPPD = array();
747     }
749     $dn= $this->dn;
750     plugin::save();
751     $ldap= $this->config->get_ldap_link();
753     /* reduce objectClasses to minimun */
754     $this->attrs['objectClass']= $this->objectclasses;
756     /* Remove all empty values */
757     if ($this->orig_dn == 'new'){
758       $attrs= array();
759       foreach ($this->attrs as $key => $val){
760         if (is_array($val) && count($val) == 0){
761           continue;
762         }
763         $attrs[$key]= $val;
764       }
765       $this->attrs= $attrs;
766     }
768     /* Append printer user 
769      */
770     $this->attrs['gotoUserPrinter']=array();
771     foreach($this->member['AddUser'] as $mem){
772       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
773     }
775     /* Append printer group 
776      */
777     $this->attrs['gotoGroupPrinter'] = array();
778     foreach($this->member['AddGroup'] as $mem){
779       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
780     }
782     /* Append printer admin user 
783      */
784     $this->attrs['gotoUserAdminPrinter'] = array();
785     foreach($this->member['AddAdminUser'] as $mem){
786       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
787     }
789     /* Append printer admin group 
790      */
791     $this->attrs['gotoGroupAdminPrinter']= array();
792     foreach($this->member['AddAdminGroup'] as $mem){
793       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
794     }
796     if($this->orig_dn == 'new'){
797       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
798         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
799           unset($this->attrs[$checkVar]);
800         }
801       }
802     }else{
803       if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
804         $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
805       }
806     }
808     /* Ensure to create a new object */
809     if(preg_match("/ou=incoming,/",$this->orig_dn)){
810       $this->orig_dn = "new";
811     }
813     /* Move object in necessary*/
814     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
815       $this->move($this->orig_dn, $this->dn);
816     }
818     /* Write back to ldap */
819     $ldap= $this->config->get_ldap_link();
820     $ldap->cat($this->dn);
821     if(!$ldap->count()){
822       $ldap->cd($this->config->current['BASE']);
823       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
824       $ldap->cd($this->dn);
826       /* Remove empty values */ 
827       foreach($this->attrs as $name => $value){
828         if(empty($value)){
829           unset($this->attrs[$name]);
830         }
831       }
833       $ldap->add($this->attrs);
834       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
835       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
836     } else {
837       $ldap->cd($this->dn);
838       $this->cleanup();
839       $ldap->modify ($this->attrs); 
840       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
841       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
842     }
843     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system print/generic with dn '%s' failed."),$this->dn));
845     if(preg_match("/printer/i",$this->BelongsTo)){
846       $this->netConfigDNS->cn = $this->cn;
847       $this->netConfigDNS->dn = $this->dn;
848       $this->netConfigDNS->save($this->dn);
849     }
851     /* This is a multi object. Handle tagging here... */
852     $this->handle_object_tagging();
853   }
855   function generateList(){
856     $a_return=array();
858     foreach($this->member as $type => $values){
859       $a_return[$type]=array();
860       foreach($values as $value){
861         if((preg_match("/Group/i",$type))){
862           if(!isset($value['description'])){
863             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
864           }else{
865             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
866           }
867         }else{
868           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
869         }
870       }
871     }
872     return($a_return);
873   }
875   /* Return plugin informations for acl handling
876       #FIXME FAIscript seams to ununsed within this class... */
877   function plInfo()
878   {
879     return (array(
880           "plShortName"   => _("Generic"),
881           "plDescription" => _("Print generic"),
882           "plSelfModify"  => FALSE,
883           "plDepends"     => array(),
884           "plPriority"    => 4,
885           "plSection"     => array("administration"),
886           "plCategory"    => array("printer" => array("description"  => _("Printer"),
887                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
888           "plProvidedAcls"=> array(
889             "cn"                => _("Name"),
890             "base"                => _("Base") ,         
891             "description"       => _("Description"), 
892             "l"                 => _("Location"), 
893             "labeledURI"        => _("LabeledURL"), 
894             "gotoPrinterPPD"    => _("Printer PPD"),
895             "gotoUserPrinter"   => _("Permissions")) 
896           ));
897   }
900   /* Delete member */
901   function DelMember($type,$id)
902   {
903     /* Check if there was a printer "dn" given, or the "cn" */
904     foreach($this->member[$type] as $key => $printer){
905       if($printer['dn'] == $id) {
906         $id = $key;
907       }
908     }
909   
910     if(!$this->acl_is_writeable("gotoUserPrinter")){
911       print_red(sprintf(_("You are not allowed to remove the given object '%s' from the list of members of printer '%s'."),$id,$this->dn));
912       return(FALSE);
913     }
914  
915     if(isset($this->member[$type][$id])){
916       unset($this->member[$type][$id]);
917       return(TRUE);
918     }
919     return(FALSE);
920   }
923   /* Add given obejct to members */
924   function AddMember($type,$dn)
925   {
926     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
927     if(!in_array_ics($type, $types)){
928       print_red(sprintf(_("Illegal printer type while adding '%s' to the list of '%s' printers,"),$dn,$type));
929       return(FALSE);
930     }
932     if(!$this->acl_is_writeable("gotoUserPrinter")){
933       print_red(sprintf(_("You are not allowed to add the given object '%s' to the list of members of '%s'."),$dn,$this->dn));
934       return(FALSE);
935     }
937     /* Get name of index attributes */
938     if(preg_match("/user/i",$type)){
939       $var = "uid";
940     }else{
941       $var = "cn";
942     }
944     $ldap = $this->config->get_ldap_link();
945     $ldap->cd($dn);
946     $ldap->cat($dn,array($var,"cn"));
947     if($ldap->count()){
949       $attrs = $ldap->fetch();
951       if(isset($attrs[$var][0])){
952         $name = $attrs[$var][0];
954         /* Check if this uid/cn is already assigned to any permission */
955         foreach($types as $ctype){
957           /* If we want to add a user, only check user/userAdmin members */
958           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
959             continue;
960           }
962           /* If we want to add a group, only check groups/adminGroups .. */
963           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
964             continue;
965           }
967           if(isset(  $this->member[$ctype][$name])){
968             print_red(sprintf(_("Can't add '%s' to the list of members, it is already used."),$attrs[$var][0]));
969             return(FALSE);
970           }
971         }
973         /* Everything is fine. So add the given object to members */
974         $this->member[$type][$attrs[$var][0]] = $attrs ;
975       }else{
976         print_a($attrs);
977       }
978     }else{
979       print_red(sprintf(_("Can't add '%s' to list of members, it is not reachable."),$dn));
980       return(FALSE);
981     }
982     return(TRUE);
983   }
988 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
989 ?>