Code

Added a first set of reference changes - nearly untested
[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(preg_match("/cn=dhcp,/",$attrs['dn'])){
683             continue;
684           }
685           if ($attrs['dn'] != $this->orig_dn){
686             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
687             break;
688           }
689         }
690       }
691     }
693     return ($message);
694   }
697   /* Save to LDAP */
698   function save()
699   {
700     /* Update dn, to ensure storing as printer instead of WS / terminal
701      */
702     if(preg_match("/terminal/i",$this->BelongsTo)){
703       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
704     }
706     if(preg_match("/workstation/i",$this->BelongsTo)){
707       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
708     }
709     
710     if(!$this->is_account) return;
711     if(isset($this->parent->by_object['workgeneric'])){
712       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
713         return;
714       }
715     }elseif(isset($this->parent->by_object['termgeneric'])){
716       if($this->parent->by_object['termgeneric']->cn == "default"){
717         return;
718       }
719     }
721     /* If type is still unknown, the initialisation of this printer failed, abort. */
722     if(preg_match("/unknown/i",$this->BelongsTo)){
723       return;
724     }
726     /* save ppd configuration */
727     if($this->PPDdialogToSave){
728       $this->PPDdialogToSave->save_ppd();
729     }
731     /* Remove previously selected ppd file.*/
732     if($this->initial_PPD != $this->gotoPrinterPPD){
733       if(!empty($this->initial_PPD)){
734         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
735         $tmp->removeModifiedPPD();
736       }
737     }
739     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
740       $method="https://";
741     }else{
742       $method="http://";
743     }
745     /* If no ppd is selected, remove this attribute */
746     if(!empty($this->gotoPrinterPPD)) {
747       $this->gotoPrinterPPD = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
748     }else{
749       $this->gotoPrinterPPD = array();
750     }
752     $dn= $this->dn;
753     plugin::save();
754     $ldap= $this->config->get_ldap_link();
756     /* reduce objectClasses to minimun */
757     $this->attrs['objectClass']= $this->objectclasses;
759     /* Remove all empty values */
760     if ($this->orig_dn == 'new'){
761       $attrs= array();
762       foreach ($this->attrs as $key => $val){
763         if (is_array($val) && count($val) == 0){
764           continue;
765         }
766         $attrs[$key]= $val;
767       }
768       $this->attrs= $attrs;
769     }
771     /* Append printer user 
772      */
773     $this->attrs['gotoUserPrinter']=array();
774     foreach($this->member['AddUser'] as $mem){
775       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
776     }
778     /* Append printer group 
779      */
780     $this->attrs['gotoGroupPrinter'] = array();
781     foreach($this->member['AddGroup'] as $mem){
782       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
783     }
785     /* Append printer admin user 
786      */
787     $this->attrs['gotoUserAdminPrinter'] = array();
788     foreach($this->member['AddAdminUser'] as $mem){
789       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
790     }
792     /* Append printer admin group 
793      */
794     $this->attrs['gotoGroupAdminPrinter']= array();
795     foreach($this->member['AddAdminGroup'] as $mem){
796       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
797     }
799     if($this->orig_dn == 'new'){
800       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
801         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
802           unset($this->attrs[$checkVar]);
803         }
804       }
805     }else{
806       if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
807         $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
808       }
809     }
811     /* Ensure to create a new object */
812     if(preg_match("/ou=incoming,/",$this->orig_dn)){
813       $this->orig_dn = "new";
814     }
816     /* Move object in necessary*/
817     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
818       $this->move($this->orig_dn, $this->dn);
819     }
821     /* Write back to ldap */
822     $ldap= $this->config->get_ldap_link();
823     $ldap->cat($this->dn);
824     if(!$ldap->count()){
825       $ldap->cd($this->config->current['BASE']);
826       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
827       $ldap->cd($this->dn);
829       /* Remove empty values */ 
830       foreach($this->attrs as $name => $value){
831         if(empty($value)){
832           unset($this->attrs[$name]);
833         }
834       }
836       $ldap->add($this->attrs);
837       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
838       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
839     } else {
840       $ldap->cd($this->dn);
841       $this->cleanup();
842       $ldap->modify ($this->attrs); 
843       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
844       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
845     }
846     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system print/generic with dn '%s' failed."),$this->dn));
848     if(preg_match("/printer/i",$this->BelongsTo)){
849       $this->netConfigDNS->cn = $this->cn;
850       $this->netConfigDNS->dn = $this->dn;
851       $this->netConfigDNS->save($this->dn);
852     }
854     /* This is a multi object. Handle tagging here... */
855     $this->handle_object_tagging();
856   }
858   function generateList(){
859     $a_return=array();
861     foreach($this->member as $type => $values){
862       $a_return[$type]=array();
863       foreach($values as $value){
864         if((preg_match("/Group/i",$type))){
865           if(!isset($value['description'])){
866             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
867           }else{
868             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
869           }
870         }else{
871           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
872         }
873       }
874     }
875     return($a_return);
876   }
878   /* Return plugin informations for acl handling
879       #FIXME FAIscript seams to ununsed within this class... */
880   function plInfo()
881   {
882     return (array(
883           "plShortName"   => _("Generic"),
884           "plDescription" => _("Print generic"),
885           "plSelfModify"  => FALSE,
886           "plDepends"     => array(),
887           "plPriority"    => 4,
888           "plSection"     => array("administration"),
889           "plCategory"    => array("printer" => array("description"  => _("Printer"),
890                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
891           "plProvidedAcls"=> array(
892             "cn"                => _("Name"),
893             "base"                => _("Base") ,         
894             "description"       => _("Description"), 
895             "l"                 => _("Location"), 
896             "labeledURI"        => _("LabeledURL"), 
897             "gotoPrinterPPD"    => _("Printer PPD"),
898             "gotoUserPrinter"   => _("Permissions")) 
899           ));
900   }
903   /* Delete member */
904   function DelMember($type,$id)
905   {
906     /* Check if there was a printer "dn" given, or the "cn" */
907     foreach($this->member[$type] as $key => $printer){
908       if($printer['dn'] == $id) {
909         $id = $key;
910       }
911     }
912   
913     if(!$this->acl_is_writeable("gotoUserPrinter")){
914       print_red(sprintf(_("You are not allowed to remove the given object '%s' from the list of members of printer '%s'."),$id,$this->dn));
915       return(FALSE);
916     }
917  
918     if(isset($this->member[$type][$id])){
919       unset($this->member[$type][$id]);
920       return(TRUE);
921     }
922     return(FALSE);
923   }
926   /* Add given obejct to members */
927   function AddMember($type,$dn)
928   {
929     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
930     if(!in_array_ics($type, $types)){
931       print_red(sprintf(_("Illegal printer type while adding '%s' to the list of '%s' printers,"),$dn,$type));
932       return(FALSE);
933     }
935     if(!$this->acl_is_writeable("gotoUserPrinter")){
936       print_red(sprintf(_("You are not allowed to add the given object '%s' to the list of members of '%s'."),$dn,$this->dn));
937       return(FALSE);
938     }
940     /* Get name of index attributes */
941     if(preg_match("/user/i",$type)){
942       $var = "uid";
943     }else{
944       $var = "cn";
945     }
947     $ldap = $this->config->get_ldap_link();
948     $ldap->cd($dn);
949     $ldap->cat($dn,array($var,"cn"));
950     if($ldap->count()){
952       $attrs = $ldap->fetch();
954       if(isset($attrs[$var][0])){
955         $name = $attrs[$var][0];
957         /* Check if this uid/cn is already assigned to any permission */
958         foreach($types as $ctype){
960           /* If we want to add a user, only check user/userAdmin members */
961           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
962             continue;
963           }
965           /* If we want to add a group, only check groups/adminGroups .. */
966           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
967             continue;
968           }
970           if(isset(  $this->member[$ctype][$name])){
971             print_red(sprintf(_("Can't add '%s' to the list of members, it is already used."),$attrs[$var][0]));
972             return(FALSE);
973           }
974         }
976         /* Everything is fine. So add the given object to members */
977         $this->member[$type][$attrs[$var][0]] = $attrs ;
978       }else{
979         print_a($attrs);
980       }
981     }else{
982       print_red(sprintf(_("Can't add '%s' to list of members, it is not reachable."),$dn));
983       return(FALSE);
984     }
985     return(TRUE);
986   }
989    /* Display generic part for server copy & paste */
990   function getCopyDialog()
991   {
992     $vars = array("cn");
993     $smarty = get_smarty();
994     $smarty->assign("cn" ,$this->cn);
995     $smarty->assign("object","printer");
996     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
997     $ret = array();
998     $ret['string'] = $str;
999     $ret['status'] = "";
1000     return($ret);
1001   }
1004   function saveCopyDialog()
1005   {
1006     if(isset($_POST['cn'])){
1007       $this->cn = $_POST['cn'];
1008     }
1009   }
1011   function PrepareForCopyPaste($source)
1012   {
1013     plugin::PrepareForCopyPaste($source);
1014     if(isset($source['macAddress'][0])){
1015       $this->netConfigDNS->macAddress = $source['macAddress'][0];
1016     }
1017     if(isset($source['ipHostNumber'][0])){
1018       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1019     }
1021     $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1022     foreach($this->attributes as $attr){
1023       $this->$attr = $source_o->$attr;
1024     }
1025     $this->member = $source_o -> member;
1027     $this->gotoPrinterPPD = "";
1028   }
1031 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1032 ?>