Code

* Code cleanup for non pre-defined ttag variable
[gosa.git] / plugins / admin / systems / class_printGeneric.inc
1 <?php
3 class printgeneric extends plugin
4 {
5   /* Generic terminal attributes */
6   var $interfaces     = array();
7   var $ignore_account = FALSE;
9   /* Needed values and lists */
10   var $base             = "";
11   var $cn               = "";
12   var $l                = "";
13   var $description      = "";
14   var $labeledURI       = "";
15   var $gotoPrinterPPD   = "";
16   var $initial_PPD      = "";
17   var $orig_dn          = "";
19   var $UserMember       ="";
20   var $UserMembers      =array();
21   var $UserMemberKeys   =array();
23   var $AdminMember      ="";
24   var $AdminMembers     =array();
25   var $AdminMemberKeys  =array();
27   var $PPDdialogToSave  = NULL;
28   var $BelongsTo        = "unknown"; //  Specifies if this is a standalone printer, or belongs to a terminal / WS
30   var $member           =array();
31   var $strings          = "";
32   var $dialog           =NULL;
34   var $netConfigDNS;
35   var $baseSelection    = false;
36   var $macAddress       = "";
38   /* attribute list for save action */
39   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress",
40                                 "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter");
41   var $objectclasses  = array("top", "gotoPrinter");
43     var $gotoUserAdminPrinter;
44   var $gotoGroupAdminPrinter ;
45   var $gotoGroupPrinter;
46   var $gotoUserPrinter ;
48   function printgeneric ($config, $dn= NULL,$parent = NULL)
49   {
50     $this->config = $config;
51     $this->dn = $dn; 
52     
53     /* If parent was posted(the tabs object) we can detect the printer type. */
54     if($parent){
55       $this->parent = $parent;
56       $this->getTypeOfPrinter();
57     }else{
58       $this->BelongsTo = "unknown";
59       return;
60     }
62     /* Update dn, to ensure storing as printer instead of WS / terminal */
63     if($this->BelongsTo == "Terminal" || $this->BelongsTo == "TerminalTemplate"){
64       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
65     }
67     if($this->BelongsTo == "Workstation" || $this->BelongsTo == "WorkstationTemplate"){
68       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
69     }
71     $this->orig_dn = $this->dn;
72     $ui= get_userinfo();
74     /* Get printer settings, possibly dn has changed */
75     plugin::plugin ($config, $this->dn);
77     /* Get is_account initially_was_account status */
78     $this->getTypeOfPrinter(true);
80     /* set orig dn to new if object is new */
81     $ldap= $this->config->get_ldap_link();
82     $ldap->cat($this->dn, array('dn'));
83     if(!$ldap->count()){
84       $this->orig_dn = "new";
85     }
86     
87     /* create dns object */
88     $this->netConfigDNS = new termDNS($this->config, $this->dn,$this->objectclasses);
89     $this->netConfigDNS->acl = $this->acl;
91     /* Set base */
92     if ($this->dn == "new"){
93       $this->base= dn2base($ui->dn);
94       $this->cn= "";
95     } else {
96     
97       /* Set base and check if the extracted base exists */
98       if(preg_match("/ou=incoming,/",$this->dn)){
99         $this->base= preg_replace("/ou=incoming,/","",dn2base($this->dn));
100       }else{
101         $this->base= preg_replace("/ou=printers,ou=systems,/","",dn2base($this->dn));
102       }
104       if(!isset($this->config->idepartments[$this->base])){
105         print_red(_("Can't extract a valid base out of object dn, setting base to '%s'."),$_SESSION['CurrentMainBase']);
106         $this->base = $_SESSION['CurrentMainBase'];
107       }
108     }
110     /* Extract selected ppd */
111     if(isset($this->gotoPrinterPPD)){
112       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
113     }
115     $this->initial_PPD = $this->gotoPrinterPPD;
117     /* Prepare different member types */ 
118     foreach(array("AddUser"       =>"gotoUserPrinter",
119           "AddGroup"      =>"gotoGroupPrinter",
120           "AddAdminUser"  =>"gotoUserAdminPrinter",
121           "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
123       /* $this->members contains all members */
124       $this->member[$type]=array();
126       if (isset($this->attrs[$attr]['count'])) {
127         unset($this->attrs[$attr]['count']);
128       }
130       /* Set tag attribute if we've tagging activated */
131       $tag= "";
132       if ($ui->gosaUnitTag != "" && isset($config->current['STRICT_UNITS']) &&
133           preg_match('/TRUE/i', $config->current['STRICT_UNITS'])){
134         $tag= "(gosaUnitTag=".$ui->gosaUnitTag.")";
135       }
137       if(isset($this->attrs[$attr])){
138         foreach($this->attrs[$attr] as $mem){
139           if(preg_match("/Group/",$type)){
140             $ldap->search("(&(objectClass=posixGroup)$tag(cn=".$mem."))",array("cn","description"));
141             if($ldap->count()){
142               $entry = $ldap->fetch();
143               if(isset($entry['cn'])){
144                 $this->member[$type][$entry['cn'][0]]=$entry;
145               }
146             }
147           }else{
148             $ldap->search("(&(objectClass=person)$tag(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
149             if($ldap->count()){
150               $entry = $ldap->fetch();
151               if(isset($entry['uid'])){
152                 $this->member[$type][$entry['uid'][0]]=$entry;
153               }
154             }
155           }
156         }
157       }
158     }
159   }
162   /* Detect type of printer.
163    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
164    * We can detect the type printer type when comparing the tabs objects
165    */
166   function getTypeOfPrinter($UpdateAccountStatus = false)
167   {
168     /* Disable account as default
169      */  
170     $this->is_account = $this->initially_was_account = false;
172     /* Detect type of printer via parent tabs.
173      */
174     if(isset($this->parent->by_object['workgeneric'])){
176       /* Exclude templates 
177        */
178       $this->cn = $this->parent->by_object['workgeneric']->cn;
179       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
180         $this->BelongsTo = "WorkstationTemplate";
181       }else{
182         $this->BelongsTo = "Workstation";
183       }
184     }elseif(isset($this->parent->by_object['termgeneric'])){
186       /* Exclude templates 
187        */
188       $this->cn = $this->parent->by_object['termgeneric']->cn;
189       if($this->parent->by_object['termgeneric']->cn == "default"){
190         $this->BelongsTo = "TerminalTemplate";  
191       }else{
192         $this->BelongsTo = "Terminal";
193       }
194     }elseif(isset($this->parent->by_name['printgeneric'])){
195       $this->BelongsTo  = "Printer";
196     }
198     if($UpdateAccountStatus){
200       /* Set is_account / was account 
201        */
202       if($this->dn == "new"){
203         $this->initially_was_account = false;
204       }
206       /* If is printer it must be a true account.
207        */
208       if($this->BelongsTo == "Printer"){
209         $this->is_account = true;
210       }
212       /* Update dn, to ensure storing as printer instead of WS / terminal
213        */
214       if($this->BelongsTo == "Terminal"){
215         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
216       }
218       if($this->BelongsTo == "Workstation"){
219         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
220       }
222       /* Detect if this is a valid printer account;
223        */
224       //FIXME: do we need to do this? we've already everything we need in $this->attrs...
225       $ldap = $this->config->get_ldap_link();
226       $ldap->cat($this->dn, array('objectClass'));
228       if($ldap->count()){
229         $attrs = $ldap->fetch();
230         if(in_array("gotoPrinter",$attrs['objectClass'])){
231           $this->initially_was_account = true;
232           $this->is_account             = true;
233         }else{
234           $this->is_account = false;
235         }
236       }
237     }
238   }
240   function execute()
241   {
242     $this->netConfigDNS->acl = $this->acl;
244     /* Call parent execute */
245     plugin::execute();
247     /* If type of printer couldn't be detected (because of missing parent object in construction) 
248      * hide this tab.
249      */
250     if($this->BelongsTo == "unknown"){
251       $display= $this->show_header(_("Add printer extension"),
252           _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
253       return($display);
254     }
256     /* Templates can't have printer extensions 
257      */
258     if($this->BelongsTo == "WorkstationTemplate"){
259       $display= $this->show_header(_("Add printer extension"),
260           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
261       return($display);
262     }
263     if($this->BelongsTo == "TerminalTemplate"){
264       $display= $this->show_header(_("Add printer extension"),
265           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
266       return($display);
267     }
269     /* Get cn from base object */
270     if($this->BelongsTo == "Workstation"){
271       $this->cn = $this->parent->by_object['workgeneric']->cn;
272     }
273     if($this->BelongsTo == "Terminal"){
274       $this->cn = $this->parent->by_object['termgeneric']->cn;
275     }
277     $smarty= get_smarty();
278     $display="";
280     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
281     if($this->BelongsTo == "Printer"){    
282       $smarty->assign("StandAlone",true);
283     }else{
284       $smarty->assign("StandAlone",false);
285     }
288     if (isset($_POST['modify_state'])){
290       /* Only change account state if allowed */
291       if($this->is_account && $this->acl == "#all#"){
292         $this->is_account= !$this->is_account;
293         $this->is_modified = true;
294       }elseif(!$this->is_account && chkacl($this->acl,"create") == ""  && !((empty($this->cn)) && ($this->dn != "new"))){
295         $this->is_account= !$this->is_account;
296         $this->is_modified = true;
297       }
298     }
300     /* Do we represent a valid printer? */
301     if (!$this->is_account && $this->parent == NULL){
302       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
303         _("This 'dn' has no printer features.")."</b>";
304       return($display);
305     }
307     /* If this is a WS / Terminal depending printer, display account state button */
308     if($this->BelongsTo != "Printer"){
309       if((empty($this->cn)) && ($this->dn != "new")){
310         $display= $this->show_header(_("Add printer extension"),
311             _("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);
312         $this->is_account= false;
313         return $display;
314       }
316       if (($this->is_account)){
317         if($this->BelongsTo=="Workstation"){
318           $display= $this->show_header(_("Remove printer extension"),
319               _("This workstation has printer extension enabled.You can disable it by clicking below."));
320         }elseif($this->BelongsTo=="Terminal"){
321           $display= $this->show_header(_("Remove printer extension"),
322               _("This terminal has printer extension enabled. You can disable it by clicking below."));
323         }
324       }else{
325         if($this->BelongsTo=="Workstation"){
326           $display= $this->show_header(_("Add printer extension"),
327               _("This workstation has printer extension disabled. You can enable it by clicking below."));
328         }elseif($this->BelongsTo=="Terminal"){
329           $display= $this->show_header(_("Add printer extension"),
330               _("This terminal has printer extension disabled. You can enable it by clicking below."));
331         }  
332         return ($display);
333       }
334     }
336     /* Base select dialog */
337     $once = true;
338     foreach($_POST as $name => $value){
339       if(preg_match("/^chooseBase/",$name) && $once && chkacl($this->acl,"base")){
340         $once = false;
341         $this->dialog = new baseSelectDialog($this->config);
342         $this->dialog->setCurrentBase($this->base);
343         $this->baseSelection = true;
344       }
345     }
347     /* Dialog handling for base select dialog
348      * Check if base was selected, dialog aborted etc */
349     if(is_object($this->dialog)){
351       $this->dialog->save_object();
352       if($this->baseSelection){
353         if($this->dialog->isClosed()){
354           $this->dialog = false;
355           $this->baseSelection = false;
356         }elseif($this->dialog->isSelected()){
357           $this->base = $this->dialog->isSelected();
358           $this->dialog= false;
359           $this->baseSelection = false;
360         }else{
361           return($this->dialog->execute());
362         }
363       }
364     }
366     /* Fill templating stuff */
367     $smarty->assign("bases", $this->config->idepartments);
368     $smarty->assign("base_select", $this->base);
370     /* Assign attributes */
371     foreach ($this->attributes as $attr){
372       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
373       $smarty->assign("$attr", $this->$attr);
374     }
376     $smarty->assign("baseACL", chkacl($this->acl,"base"));
378     if(isset($_POST['AddUser']) && chkacl($this->acl,"gotoUserPrinter") == ""){
379       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
380     }
381     if(isset($_POST['AddGroup']) && chkacl($this->acl,"gotoGroupPrinter") == ""){
382       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
383     }
384     if(isset($_POST['AddAdminUser']) && chkacl($this->acl,"gotoUserAdminPrinter") == ""){
385       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
386     }
387     if(isset($_POST['AddAdminGroup']) && chkacl($this->acl,"gotoGroupAdminPrinter") == ""){
388       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
389     }
391     /* Display ppd configure/select dialog      */
392     if(isset($_POST['EditDriver']) && chkacl($this->acl,"gotoPrinterPPD")==""){
393       if($this->PPDdialogToSave){
394         $this->dialog = $this->PPDdialogToSave;
395       }else{
396         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
397         $this->dialog->cn= $this->cn;
398       }
399     }
401     /* remove ppd */
402     if(isset($_POST['RemoveDriver']) && chkacl($this->acl,"gotoPrinterPPD")==""){
403       $this->gotoPrinterPPD = array();
404       $this->PPDdialogToSave = NULL;
405     }
407     /* Close ppd dialog */
408     if(isset($_POST['ClosePPD'])){
409       unset($this->dialog);
410       $this->dialog=NULL;
411     }
413     /* Save selected ppd */
414     if(isset($_POST['SavePPD'])){
415       $this->dialog->save_object();
416       if(count($this->dialog->check())){
417         foreach($this->dialog->check() as $msg){
418           print_red($msg);
419         }
420       }else{
421         $this->gotoPrinterPPD = array();
422         $this->gotoPrinterPPD = $this->dialog->save();
423         $this->PPDdialogToSave = $this->dialog;
424         unset($this->dialog);
425         $this->dialog=NULL;
426       }
427     }
429     /* Member management, delete user / group / admin ..*/
430     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember'])) && chkacl($this->acl,"gotoUserPrinter")==""){
431       foreach($_POST['UserMember'] as $mem){
432         $this->DelMember('AddUser',$mem);
433       }
434     }
436     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember'])) && chkacl($this->acl,"gotoGroupPrinter")==""){
437       foreach($_POST['UserMember'] as $mem){
438         $this->DelMember('AddGroup',$mem);
439       }
440     }
442     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember'])) && chkacl($this->acl,"gotoUserAdminPrinter")==""){
443       foreach($_POST['AdminMember'] as $mem){
444         $this->DelMember('AddAdminUser',$mem);
445       }
446     }
448     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember'])) && chkacl($this->acl,"gotoGroupAdminPrinter")==""){
449       foreach($_POST['AdminMember'] as $mem){
450         $this->DelMember('AddAdminGroup',$mem);
451       }
452     }
454     /* Abort user / group adding dialog */
455     if(isset($_POST['PrinterCancel'])){
456       unset($this->dialog);
457       $this->dialog= NULL;
458     }
460     /* Save selected users / groups */
461     if(isset($_POST['PrinterSave'])){
462       $this->dialog->save_object();
463       if(count($this->dialog->check())){
464         foreach($this->dialog->check() as $msg){
465           print_red($msg);
466         }
467       }else{
468         $data= $new = $this->dialog->save();
469         unset($data['type']);
470         foreach($data as $mem){  
471           $this->AddMember($new['type'], $mem['dn']);    
472         }
473         unset($this->dialog);
474         $this->dialog=NULL; 
475       }
476     }
478     /* Display dialog, if there is currently one open*/
479     if($this->dialog != NULL){
480       $this->dialog->save_object();
481       $display = $this->dialog->execute();
482       return $display;
483     }
485     /* Parse selected ppd file */
486     require_once ("class_ppdManager.inc");
487     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
489       $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
490       if(!preg_match("/\/$/",$path)){
491         $path = $path."/";
492       }
494       $ppdManager= new ppdManager($path);
495       if(!empty($this->gotoPrinterPPD)){
496         if((!file_exists($path.$this->gotoPrinterPPD))){
497           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD))."</b>";
498         }else{
499           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
500         }
501       }else{
502         $smarty->assign("driverInfo", _("Not defined"));
503       }
504     }else{
505       $smarty->assign("driverInfo",_("Can't get ppd informations."));
506     }
508     /* Create user & admin user list */
509     $list=$this->generateList();
510     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
511     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
513     asort($userlist);
514     asort($adminlist);
516     if($this->BelongsTo != "Printer"){
517       if($this->BelongsTo == "Terminal"){
518         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
519       }else{
520         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
521       }
522       $smarty->assign("cnACL"    ," disabled ");
523     }else{
524       $smarty->assign("desc"    ,"");
525     }
526     $smarty->assign("UserMember"    ,$this->UserMember);
527     $smarty->assign("UserMembers"   ,$userlist);
528     $smarty->assign("UserMemberKeys",array_flip($userlist));
530     $smarty->assign("AdminMember"    ,$this->AdminMember);
531     $smarty->assign("AdminMembers"   ,$adminlist);
532     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
533     if($this->BelongsTo == "Printer"){
534       $this->netConfigDNS->cn= $this->cn;
535       $smarty->assign("netconfig", $this->netConfigDNS->execute());
537       /* Display sub dialog from network settings */
538       if($this->netConfigDNS->dialog){
539         $this->dialog = TRUE;
540         return($this->netConfigDNS->execute());
541       }else{ 
542         $this->dialog = FALSE;
543       }
544     } else {
545       $smarty->assign("netconfig", "");
546     }
549     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
550   }
552   function remove_from_parent()
553   {
554     $this->netConfigDNS->acl = $this->acl;
556     /* Only remove if there was initially an account */
557     if($this->initially_was_account){
559       /* Update dn, to ensure storing as printer instead of WS / terminal
560        */
561       if($this->BelongsTo == "Terminal"){
562         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
563       }
565       if($this->BelongsTo == "Workstation"){
566         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
567       }
569       /* Check if this dn points to a printer, to avoid deleting something else */
570       $ldap= $this->config->get_ldap_link();
571       $ldap->cat($this->dn, array('dn',"objectClass"));
572       if(!$ldap->count()){
573         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
574         return;
575       }
577       /* Check if obejct is a printer */
578       $CheckPrinter = $ldap->fetch();
579       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
580         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
581         return;
582       }
584       /* Remove account & dns extension */ 
585       $this->netConfigDNS->remove_from_parent();
586       $ldap->rmdir($this->dn);
587       show_ldap_error($ldap->get_error(), _("Removing printer failed"));
588       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
590       /* Delete references to object groups */
591       $ldap->cd ($this->config->current['BASE']);
592       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
593       while ($ldap->fetch()){
594         $og= new ogroup($this->config, $ldap->getDN());
595         unset($og->member[$this->dn]);
596         $og->save ();
597       }
599       /* Remove previously selected ppd file.*/
600       if(!empty($this->initial_PPD)){
601         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
602         $tmp->removeModifiedPPD();
603       }
604     }
605   }
608   /* Save data to object */
609   function save_object()
610   {
611     plugin::save_object();
612     $this->netConfigDNS->save_object();
613     /* Save base, since this is no LDAP attribute */
614     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
615       $this->base= $_POST['base'];
616     }
617   }
619   /* Check supplied data */
620   function check()
621   {
622     /* Call common method to give check the hook */
623     $message= plugin::check();
624     if ($this->BelongsTo == 'printer'){
625       $message= array_merge($message, $this->netConfigDNS->check());
626     }
628     /* Don't display check messages if this is a template object */
629     if(isset($this->parent->by_object['workgeneric'])){
630       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
631         return $message;
632       }
633     }elseif(isset($this->parent->by_object['termgeneric'])){
634       if($this->parent->by_object['termgeneric']->cn == "default"){
635         return $message;
636       }
637     }
639     $dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
641     /* must: cn */
642     if(($this->BelongsTo == "Printer") && (empty($this->cn))){
643       $message[]= "The required field 'Printer name' is not set.";
644     }
646     if($this->BelongsTo == "Printer"){
647       $ui= get_userinfo();
648       $acl= get_permissions ($dn, $ui->subtreeACL);
649       $acl= get_module_permission($acl, "printgeneric", $dn);
650       if (chkacl($acl, "create") != ""){
651         $message[]= _("You have no permissions to create a printer on this 'Base'.");
652       }
653     }
655     
656     /* must: labeledURI */
657     if(empty($this->labeledURI)){
658       $message[]= "The required field 'Printer URL' is not set.";
659     }
660     
661     /* Check if there is already an entry with this cn*/
662     if (($this->orig_dn != $dn)&&($this->BelongsTo == "Printer")){
663       $ldap= $this->config->get_ldap_link();
664       $ldap->cd ($this->base);
665       $ldap->ls("(cn=".$this->cn.")","ou=printers,ou=systems,".$this->base, array("cn"));
666       if ($ldap->count() != 0){
667         while ($attrs= $ldap->fetch()){
668           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
669             continue;
670           }
671           if ($attrs['dn'] != $this->orig_dn){
672             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
673             break;
674           }
675         }
676       }
677     }
679     return ($message);
680   }
683   /* Save to LDAP */
684   function save()
685   {
686     $this->netConfigDNS->acl = $this->acl;
688     /* Update dn, to ensure storing as printer instead of WS / terminal
689      */
690     if($this->BelongsTo == "Terminal"){
691       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
692     }
694     if($this->BelongsTo == "Workstation"){
695       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
696       $this->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
697     }
698     
699     if(!$this->is_account) return;
700     if(isset($this->parent->by_object['workgeneric'])){
701       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
702         return;
703       }
704     }elseif(isset($this->parent->by_object['termgeneric'])){
705       if($this->parent->by_object['termgeneric']->cn == "default"){
706         return;
707       }
708     }
710     /* If type is still unknown, the initialisation of this printer failed, abort. */
711     if($this->BelongsTo == "unknown"){
712       return;
713     }
715     /* save ppd configuration */
716     if($this->PPDdialogToSave){
717       $this->PPDdialogToSave->save_ppd();
718     }
719     if($this->orig_dn != $this->dn){
720       $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
721       $this->PPDdialogToSave->cn = $this->cn;
722       $this->PPDdialogToSave->generateProperties();
723       $this->PPDdialogToSave->update_ppd_url();
724     }
726     /* Remove previously selected ppd file.*/
727     if($this->initial_PPD != $this->gotoPrinterPPD){
728       if(!empty($this->initial_PPD)){
729         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
730         $tmp->removeModifiedPPD();
731       }
732     }
734     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
735       $method="https://";
736     }else{
737       $method="http://";
738     }
740     /* If no ppd is selected, remove this attribute */
741     if(!empty($this->gotoPrinterPPD)) {
742       $this->gotoPrinterPPD = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
743     }else{
744       $this->gotoPrinterPPD = array();
745     }
747     $dn= $this->dn;
748     plugin::save();
749     $ldap= $this->config->get_ldap_link();
751     /* reduce objectClasses to minimun */
752     $this->attrs['objectClass']= $this->objectclasses;
754     /* Remove all empty values */
755     if ($this->orig_dn == 'new'){
756       $attrs= array();
757       foreach ($this->attrs as $key => $val){
758         if (is_array($val) && count($val) == 0){
759           continue;
760         }
761         $attrs[$key]= $val;
762       }
763       $this->attrs= $attrs;
764     }
766     /* Append printer user 
767      */
768     $this->attrs['gotoUserPrinter']=array();
769     foreach($this->member['AddUser'] as $mem){
770       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
771     }
773     /* Append printer group 
774      */
775     $this->attrs['gotoGroupPrinter'] = array();
776     foreach($this->member['AddGroup'] as $mem){
777       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
778     }
780     /* Append printer admin user 
781      */
782     $this->attrs['gotoUserAdminPrinter'] =array();
783     foreach($this->member['AddAdminUser'] as $mem){
784       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
785     }
787     /* Append printer admin group 
788      */
789     $this->attrs['gotoGroupAdminPrinter'] = array();
790     foreach($this->member['AddAdminGroup'] as $mem){
791       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
792     }
794     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
795       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
796       $this->attrs['gosaUnitTag'] = $this->gosaUnitTag;
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     }
807     /* Ensure to create a new object */
808     if(preg_match("/ou=incoming,/",$this->orig_dn)){
809       $this->orig_dn = "new";
810     }
812     /* Write back to ldap */
813     $ldap= $this->config->get_ldap_link();
814     if ($this->orig_dn == 'new'){
815       $ldap->cd($this->config->current['BASE']);
816       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
817       $ldap->cd($this->dn);
818      
819       /* Remove empty values */ 
820       foreach($this->attrs as $name => $value){
821         if(empty($value)){
822           unset($this->attrs[$name]);
823         }
824       }
825       
826       $ldap->add($this->attrs);
827       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
828     } else {
829       if ($this->orig_dn != $this->dn){
830         $this->move($this->orig_dn, $this->dn);
831       }
833       $ldap->cd($this->dn);
834       $this->cleanup();
835       $ldap->modify ($this->attrs); 
837       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
838     }
839     show_ldap_error($ldap->get_error(), _("Saving printer failed"));
841     /* Only save dns stuff if we are a standalone printer */
842     if($this->BelongsTo == "Printer"){
843       $this->netConfigDNS->cn = $this->cn;
844       $this->netConfigDNS->dn = $this->dn;
845       $this->netConfigDNS->save($this->dn);
846     }
848     /* This is a multi object. Handle tagging here... */
849     $this->handle_object_tagging();
850   }
852   function generateList(){
853     $a_return=array();
854     foreach($this->member as $type => $values){
855       $a_return[$type]=array();
856       foreach($values as $value){
857         if((preg_match("/Group/i",$type))){
858           if(!isset($value['description'])){
859             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
860           }else{
861             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
862           }
863         }else{
864           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
865         }
866       }
867     }
868     return($a_return);
869   }
871   /* Delete member */
872   function DelMember($type,$id)
873   {
874     if(isset($this->member[$type][$id])){
875       unset($this->member[$type][$id]);
876     }
877   }
879   /* Add given obejct to members */
880   function AddMember($type,$dn)
881   {
882     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
883     if(!in_array_ics($type, $types)){
884       print_red(sprintf(_("Illegal printer type while adding '%s' to the list of '%s' printers,"),$dn,$type));
885       return;
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("cn","uid"));
898     if($ldap->count()){
900       $attrs = $ldap->fetch();
901       $name = $attrs[$var][0];
903       /* Check if this uid/cn is already assigned to any permission */
904       foreach($types as $ctype){
905         if(isset(  $this->member[$ctype][$name])){
906           print_red(sprintf(_("Can't add '%s' to the list of members, it is already used."),$attrs[$var][0]));
907           return;
908         }
909       }
911       /* Everything is fine. So add the given object to members */
912       $this->member[$type][$attrs[$var][0]] = $attrs ;
913     }else{
914       print_red(sprintf(_("Can't add '%s' to list of members, it is not reachable."),$dn));
915     }
916   }
919 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
920 ?>