Code

Remove the tabs::tabs call from tabs_printers.inc because
[gosa.git] / trunk / gosa-plugins / goto / admin / systems / goto / 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          = "";
18   var $orig_cn          = "";
19   var $orig_base        = "";
21   var $UserMember       ="";
22   var $UserMembers      =array();
23   var $UserMemberKeys   =array();
25   var $AdminMember      ="";
26   var $AdminMembers     =array();
27   var $AdminMemberKeys  =array();
29   var $PPDdialogToSave  = NULL;
30   var $BelongsTo        = "unknown"; //  Specifies if this is a standalone printer, or belongs to a terminal / WS
32   var $member           =array();
33   var $strings          = "";
34   var $netConfigDNS;
35   var $baseSelection    = false;
36   var $macAddress       = "";
38   var $gotoUserAdminPrinter;
39   var $gotoGroupAdminPrinter ;
40   var $gotoGroupPrinter;
41   var $gotoUserPrinter ;
43   /* attribute list for save action */
44   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress", 
45                               "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter","gosaUnitTag");
46   var $objectclasses  = array("top", "gotoPrinter");
47   var $view_logged    = FALSE;
48   var $parent;
50   function printgeneric (&$config, $dn,$parent_init,$parent)
51   {
52     $this->config = &$config;
53     $this->dn = $dn; 
54  
55     /* If parent was posted(the tabs object) we can detect the printer type. */
56     if($parent){
57       $this->parent = $parent;
58       $this->getTypeOfPrinter();
59     }else{
60       $this->BelongsTo = "unknown";
61       return;
62     }
64     /* Update dn, to ensure storing as printer instead of WS / terminal */
65     if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
66       $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
67     }
69     if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
70       $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
71     }
73     $this->orig_dn = $this->dn;
75     /* Get printer settings, possibly dn has changed */
76     plugin::plugin ($config, $this->dn);
78     /* Get is_account initially_was_account status */
79     $this->getTypeOfPrinter(true);
81     /* set orig dn to new if object is new */
82     $ldap= $this->config->get_ldap_link();
83     $ldap->cat($this->dn, array('dn'));
84     if(!$ldap->count()){
85       $this->orig_dn = "new";
86     }
87     
88     /* create dns object */
89     $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
91     /* Set base */
92     if ($this->dn == "new"){
93       $ui= get_userinfo();
94       $this->base= dn2base($ui->dn);
95       $this->cn= "";
96     } else {
97     
98       /* Set base and check if the extracted base exists */
99       if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/",$this->dn)){
100         $this->base= preg_replace("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/","",dn2base($this->dn));
101       }else{
102         $this->base= preg_replace("/".preg_quote(get_ou('printerRDN'), '/')."/","",dn2base($this->dn));
103       }
105       if(!isset($this->config->idepartments[$this->base])){
106         msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
107         $this->base  = session::get('CurrentMainBase');
108       }
109     }
111     /* Extract selected ppd */
112     if(isset($this->gotoPrinterPPD)){
113       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
114     }
116     $this->initial_PPD = $this->gotoPrinterPPD;
118     /* Prepare different member types */ 
119     foreach(array("AddUser"       =>"gotoUserPrinter",
120           "AddGroup"      =>"gotoGroupPrinter",
121           "AddAdminUser"  =>"gotoUserAdminPrinter",
122           "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
124       /* $this->members contains all members */
125       $this->member[$type]=array();
127       if(isset($this->attrs[$attr])){
128         $ldap->cd($this->config->current['BASE']) ;
129         for($i = 0 ;  $i < $this->attrs[$attr]['count']; $i++){
130         
131           $mem = $this->attrs[$attr][$i];
132           if(preg_match("/Group/",$type)){
133             $ldap->search("(&(|(objectClass=posixGroup)(objectClass=gosaGroupOfNames))(cn=".$mem."))",array("cn","description"));
134             if($ldap->count()){
135               $entry = $ldap->fetch();
136               $this->member[$type][$entry['cn'][0]]=$entry;
137             }
138           }else{
139             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
140             if($ldap->count()){
141               $entry = $ldap->fetch();
142               $this->member[$type][$entry['uid'][0]]=$entry;
143             }
144           }
145         }
146       }
147     }
148     $this->orig_cn    = $this->cn;
149     $this->orig_base  = $this->base;
150   }
152   function set_acl_base($base)
153   {
154     plugin::set_acl_base($base);
155     if(is_object($this->netConfigDNS)){
156       $this->netConfigDNS->set_acl_base($base);
157     }
158   }
160   function set_acl_category($cat)
161   {
162     plugin::set_acl_category($cat);
163     if(is_object($this->netConfigDNS)){
164       $this->netConfigDNS->set_acl_category($cat);
165     }
166   }
168   /* Detect type of printer.
169    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
170    * We can detect the type printer type when comparing the tabs objects
171    */
172   function getTypeOfPrinter($UpdateAccountStatus = false)
173   {
174     /* Disable account as default
175      */  
176     $this->is_account = $this->initially_was_account = false;
178     /* Detect type of printer via parent tabs.
179      */
181     $class = get_class($this->parent);
182     if(isset($this->parent->by_object['workgeneric'])){
184       /* Exclude templates 
185        */
186       $this->cn = $this->parent->by_object['workgeneric']->cn;
187       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
188         $this->BelongsTo = "WorkstationTemplate";
189       }else{
190         $this->BelongsTo = "Workstation";
191       }
192     }elseif(isset($this->parent->by_object['termgeneric'])){
194       /* Exclude templates 
195        */
196       $this->cn = $this->parent->by_object['termgeneric']->cn;
197       if($this->parent->by_object['termgeneric']->cn == "default"){
198         $this->BelongsTo = "TerminalTemplate";  
199       }else{
200         $this->BelongsTo = "Terminal";
201       }
202     }elseif(isset($this->parent->by_name['printgeneric'])){
203       $this->BelongsTo  = "Printer";
204     }
206     if($UpdateAccountStatus){
208       /* Set is_account / was account 
209        */
210       if($this->dn == "new"){
211         $this->initially_was_account = false;
212       }
214       /* If is printer it must be a true account.
215        */
216       if(preg_match("/printer/i",$this->BelongsTo)){
217         $this->is_account = true;
218       }
220       /* Update dn, to ensure storing as printer instead of WS / terminal
221        */
222       if(preg_match("/terminal/i",$this->BelongsTo)){
223         $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
224       }
226       if(preg_match("/workstation/i",$this->BelongsTo)){
227         $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
228       }
230       /* Detect if this is a valid printer account;
231        */
232       $ldap = $this->config->get_ldap_link();
233       $ldap->cat($this->dn, array('objectClass'));
235       if($ldap->count()){
236         $attrs = $ldap->fetch();
237         if(in_array("gotoPrinter",$attrs['objectClass'])){
238           $this->initially_was_account = true;
239           $this->is_account             = true;
240         }else{
241           $this->is_account = false;
242         }
243       }
244     }
245   }
247   function execute()
248   {
249     /* Call parent execute */
250     plugin::execute();
252     if($this->is_account && !$this->view_logged){
253       $this->view_logged = TRUE;
254       new log("view","printer/".get_class($this),$this->dn);
255     }
258     /* If type of printer couldn't be detected (because of missing parent object in construction) 
259      * hide this tab.
260      */
261     if(preg_match("/unknown/i",$this->BelongsTo)){
262       $display= $this->show_enable_header(_("Add printer extension"),
263           _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
264       return($display);
265     }
267     /* Templates can't have printer extensions 
268      */
269     if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
270       $display= $this->show_enable_header(_("Add printer extension"),
271           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
272       return($display);
273     }
274     if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
275       $display= $this->show_enable_header(_("Add printer extension"),
276           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
277       return($display);
278     }
280     /* Get cn from base object */
281     if(preg_match("/^Workstation$/i",$this->BelongsTo)){
282       $this->cn = $this->parent->by_object['workgeneric']->cn;
283     }
284     if(preg_match("/^Terminal$/i",$this->BelongsTo)){
285       $this->cn = $this->parent->by_object['termgeneric']->cn;
286     }
288     $smarty= get_smarty();
290     /* Assign acls */
291     $tmp = $this->plInfo();
292     foreach($tmp['plProvidedAcls'] as $name => $translation){
293       $smarty->assign($name."ACL", $this->getacl($name));
294     }
296     $display="";
298     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
299     if(preg_match("/^Printer$/i",$this->BelongsTo)){    
300       $smarty->assign("StandAlone",true);
301     }else{
302       $smarty->assign("StandAlone",false);
303     }
305     /* Do we need to flip is_account state? */
306     if(isset($_POST['modify_state'])){
307       if($this->is_account && $this->acl_is_removeable()){
308         $this->is_account= FALSE;
309       }elseif(!$this->is_account && $this->acl_is_createable()){
310         $this->is_account= TRUE;
311       }
312     }
314     /* Do we represent a valid printer? */
315     if (!$this->is_account && $this->parent === NULL){
316       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
317         msgPool::noValidExtension(_("printer"))."</b>";
318       return($display);
319     }
321     /* If this is a WS / Terminal depending printer, display account state button */
322     if(!preg_match("/^Printer$/i",$this->BelongsTo)){
323       if($this->cn == "" && ($this->dn != "new")){
324         $display= $this->show_enable_header(_("Add printer extension"),
325             msgPool::featuresDisabled(_("printer"))._("You can't enable it while 'cn' is not present in entry. Possibly you are currently creating a new terminal template."),TRUE,TRUE);
326         $this->is_account= false;
327         return $display;
328       }
330       if (($this->is_account)){
331         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
332           $display= $this->show_disable_header(_("Remove printer extension"),
333               msgPool::featuresEnabled(_("printer")));
334         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
335           $display= $this->show_disable_header(_("Remove printer extension"),
336               msgPool::featuresDisabled(_("printer")));
337         }
338       }else{
339         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
340           $display= $this->show_enable_header(_("Add printer extension"),
341               msgPool::featuresEnabled(_("printer")));
342         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
343           $display= $this->show_enable_header(_("Add printer extension"),
344               msgPool::featuresDisabled(_("printer")));
345         }  
346         return ($display);
347       }
348     }
350     /* Base select dialog */
351     $once = true;
352     foreach($_POST as $name => $value){
353       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
354         $once = false;
355         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
356         $this->dialog->setCurrentBase($this->base);
357         $this->baseSelection = true;
358       }
359     }
361     /* Dialog handling for base select dialog
362      * Check if base was selected, dialog aborted etc */
363     if(is_object($this->dialog)){
365       $this->dialog->save_object();
366       if($this->baseSelection){
367         if($this->dialog->isClosed()){
368           $this->dialog = false;
369           $this->baseSelection = false;
370         }elseif($this->dialog->isSelected()){
372           /* A new base was selected, check if it is a valid one */
373           $tmp = $this->get_allowed_bases();
374           if(isset($tmp[$this->dialog->isSelected()])){
375             $this->base = $this->dialog->isSelected();
376           }
378           $this->dialog= false;
379           $this->baseSelection = false;
380         }else{
381           return($this->dialog->execute());
382         }
383       }
384     }
386     /* Fill templating stuff */
387     $smarty->assign("bases", $this->get_allowed_bases());
388     $smarty->assign("base_select", $this->base);
390     /* Assign attributes */
391     foreach ($this->attributes as $attr){
392       $smarty->assign("$attr", $this->$attr);
393     }
395     if(isset($_POST['AddUser'])){
396       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser",$this->member);
397     }
398     if(isset($_POST['AddGroup'])){
399       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup",$this->member);
400     }
401     if(isset($_POST['AddAdminUser'])){
402       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser",$this->member);
403     }
404     if(isset($_POST['AddAdminGroup'])){
405       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup",$this->member);
406     }
408     /* Display ppd configure/select dialog      */
409     if(isset($_POST['EditDriver'])){
410       if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
411         $this->dialog = $this->PPDdialogToSave;
412       }else{
413         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
414         $this->dialog->cn= $this->cn;
415       }
416     }
418     /* remove ppd */
419     if(isset($_POST['RemoveDriver'])){
420       $this->gotoPrinterPPD = array();
421       $this->PPDdialogToSave = NULL;
422     }
424     /* Close ppd dialog */
425     if(isset($_POST['ClosePPD'])){
426       unset($this->dialog);
427       $this->dialog=FALSE;
428     }
430     /* Save selected ppd */
431     if(isset($_POST['SavePPD'])){
432       $this->dialog->save_object();
433       if(count($this->dialog->check())){
434         foreach($this->dialog->check() as $msg){
435           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
436         }
437       }else{
438         $this->gotoPrinterPPD = array();
439         $this->gotoPrinterPPD = $this->dialog->save();
440         $this->PPDdialogToSave = $this->dialog;
441         unset($this->dialog);
442         $this->dialog=FALSE;
443       }
444     }
446      /* Member management, delete user / group / admin ..*/
447     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
448       foreach($_POST['UserMember'] as $mem){
449         $this->DelMember('AddUser',$mem);
450       }
451     }
453     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
454       foreach($_POST['UserMember'] as $mem){
455         $this->DelMember('AddGroup',$mem);
456       }
457     }
459     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
460       foreach($_POST['AdminMember'] as $mem){
461         $this->DelMember('AddAdminUser',$mem);
462       }
463     }
465     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
466       foreach($_POST['AdminMember'] as $mem){
467         $this->DelMember('AddAdminGroup',$mem);
468       }
469     }
471     /* Abort user / group adding dialog */
472     if(isset($_POST['PrinterCancel'])){
473       unset($this->dialog);
474       $this->dialog= FALSE;
475     }
477     /* Save selected users / groups */
478     if(isset($_POST['PrinterSave'])){
479       $this->dialog->save_object();
480       if(count($this->dialog->check())){
481         foreach($this->dialog->check() as $msg){
482           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
483         }
484       }else{
485         $data= $new = $this->dialog->save();
486         unset($data['type']);
487         foreach($data as $mem){
488           $this->AddMember($new['type'], $mem['dn']);
489         }
490         unset($this->dialog);
491         $this->dialog=FALSE;
492       }
493     }
495     /* Display dialog, if there is currently one open*/
496     if(is_object($this->dialog)){
497       $this->dialog->save_object();
498       $display = $this->dialog->execute();
499       return $display;
500     }
502     /* Parse selected ppd file */
503     $config = session::get('config');
504     if ($config->get_cfg_value("ppdPath") != ""){
505       $path = $config->get_cfg_value("ppdPath");
506       if(!preg_match("/\/$/",$path)){
507         $path = $path."/";
508       }
510       $ppdManager= new ppdManager($path);
511       if(!empty($this->gotoPrinterPPD)){
512         if((!file_exists($path.$this->gotoPrinterPPD))){
513           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
514         }else{
515           $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD);
516           $smarty->assign("driverInfo", $ppdDesc['name']);
517         }
518       }else{
519         $smarty->assign("driverInfo", _("Not defined"));
520       }
521     }else{
522       $smarty->assign("driverInfo",_("Can't get ppd informations."));
523     }
525     /* Create user & admin user list */
526     $list=$this->generateList();
527     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
528     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
530     asort($userlist);
531     asort($adminlist);
533     if(!preg_match("/Printer/i",$this->BelongsTo)){
534       if(preg_match("/Terminal/i",$this->BelongsTo)){
535         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
536       }else{
537         $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
538       }
539       $smarty->assign("cnACL"    , $this->getacl("cn",true));
540     }else{
541       $smarty->assign("desc"    ,"");
542     }
543     $smarty->assign("UserMember"    ,$this->UserMember);
544     $smarty->assign("UserMembers"   ,$userlist);
545     $smarty->assign("UserMemberKeys",array_flip($userlist));
547     $smarty->assign("AdminMember"    ,$this->AdminMember);
548     $smarty->assign("AdminMembers"   ,$adminlist);
549     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
550     if(preg_match("/Printer/i",$this->BelongsTo)){
552       /* Show main page */
553       $str = $this->netConfigDNS->execute();
554       if(is_object($this->netConfigDNS->dialog)){
555         return($str);
556       }
557       $smarty->assign("netconfig", $str);
558     } else {
559       $smarty->assign("netconfig", "");
560     }
562     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
563   }
565   function remove_from_parent()
566   {
567     /* Only remove if there was initially an account */
568     if($this->initially_was_account){
570       /* Update dn, to ensure storing as printer instead of WS / terminal
571        */
572       if(preg_match("/terminal/i",$this->BelongsTo)){
573         $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/').",/",get_ou('printerRDN'),$this->dn);
574       }
576       if(preg_match("/workstation/i",$this->BelongsTo)){
577         $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
578       }
580       /* Check if this dn points to a printer, to avoid deleting something else */
581       $ldap= $this->config->get_ldap_link();
582       $ldap->cat($this->dn, array('dn',"objectClass"));
583       if(!$ldap->count()){
584         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
585         return;
586       }
588       /* Check if obejct is a printer */
589       $CheckPrinter = $ldap->fetch();
590       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
591         msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
592         return;
593       }
595       /* Remove account & dns extension */ 
596       $this->netConfigDNS->remove_from_parent();
597       $ldap->rmdir($this->dn);
599       new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
600   
601       if (!$ldap->success()){
602         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
603       }
604       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
606       /* Delete references to object groups */
607       $ldap->cd ($this->config->current['BASE']);
608       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
609       while ($ldap->fetch()){
610         $og= new ogroup($this->config, $ldap->getDN());
611         unset($og->member[$this->dn]);
612         $og->save ();
613       }
615       /* Remove previously selected ppd file.*/
616       if(!empty($this->initial_PPD)){
617         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
618         $tmp->removeModifiedPPD();
619       }
620     }
621   }
624   /* Save data to object */
625   function save_object()
626   {
627     /* Create a base backup and reset the
628        base directly after calling plugin::save_object();
629        Base will be set seperatly a few lines below */
630     $base_tmp = $this->base;
631     plugin::save_object();
632     $this->base = $base_tmp;
634     if(is_object($this->netConfigDNS)){
635       $this->netConfigDNS->save_object();
636     }
637     
638     /* Set new base if allowed */
639     $tmp = $this->get_allowed_bases();
640     if(isset($_POST['base'])){
641       if(isset($tmp[$_POST['base']])){
642         $this->base= $_POST['base'];
643       }
644     }
645   }
647   /* Check supplied data */
648   function check()
649   {
650     /* Call common method to give check the hook */
651     $message= plugin::check();
652     if (preg_match("/printer/i",$this->BelongsTo)){
653       $message= array_merge($message, $this->netConfigDNS->check());
654     }
656     /* Don't display check messages if this is a template object */
657     if(isset($this->parent->by_object['workgeneric'])){
658       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
659         return $message;
660       }
661     }elseif(isset($this->parent->by_object['termgeneric'])){
662       if($this->parent->by_object['termgeneric']->cn == "default"){
663         return $message;
664       }
665     }
667     $dn= "cn=".$this->cn.get_ou('printerRDN').",".$this->base;
669     /* must: cn */
670     if(($this->BelongsTo == "Printer") && $this->cn == ""){
671       $message[]= msgPool::required(_("Name"));
672     }
674     /* must: cn */
675     if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
676       $message[]= msgPool::invalid(_("Name"));
677     }
679     /* must: labeledURI */
680     if(empty($this->labeledURI)){
681       $message[]= msgPool::required(_("Printer URL"));
682     }
683     
684     /* Check if there is already an entry with this cn*/
685     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
686       $ldap= $this->config->get_ldap_link();
687       $ldap->cd ($this->base);
688       $ldap->ls("(cn=".$this->cn.")",get_ou('printerRDN').$this->base, array("cn"));
689       if ($ldap->count() != 0){
690         while ($attrs= $ldap->fetch()){
691           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
692             continue;
693           }
694           if ($attrs['dn'] != $this->orig_dn){
695             $message[]= msgPool::duplicated(_("Name"));
696             break;
697           }
698         }
699       }
700     }
702     /* Check if we are allowed to create or move this object
703      */
704     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
705       $message[] = msgPool::permCreate();
706     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
707       $message[] = msgPool::permMove();
708     }
710     return ($message);
711   }
714   /* Save to LDAP */
715   function save()
716   {
717     /* Update dn, to ensure storing as printer instead of WS / terminal
718      */
719     if(preg_match("/terminal/i",$this->BelongsTo)){
720       $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
721     }
723     if(preg_match("/workstation/i",$this->BelongsTo)){
724       $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
725     }
726     
727     if(!$this->is_account) return;
728     if(isset($this->parent->by_object['workgeneric'])){
729       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
730         return;
731       }
733       /* Adapt IP & mac from parent object */
734       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
735       $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
737     }elseif(isset($this->parent->by_object['termgeneric'])){
738       if($this->parent->by_object['termgeneric']->cn == "default"){
739         return;
740       }
741     
742       /* Adapt IP & mac from parent object */
743       $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
744       $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
745     }
747     /* If type is still unknown, the initialisation of this printer failed, abort. */
748     if(preg_match("/unknown/i",$this->BelongsTo)){
749       return;
750     }
752     /* save ppd configuration */
753     if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
754       $this->PPDdialogToSave->save_ppd();
755     }
756     if($this->orig_dn != $this->dn){
757       if(!empty($this->gotoPrinterPPD)) {
758         $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
759         $this->PPDdialogToSave->cn = $this->cn;
760         $this->PPDdialogToSave->generateProperties();
761         $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
762       }
763     }
765     /* Remove previously selected ppd file.*/
766     if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
767       if(!empty($this->initial_PPD)){
768         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
769         $tmp->removeModifiedPPD();
770       }
771     }
773     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
774       $method="https://";
775     }else{
776       $method="http://";
777     }
778    
779     /* Get servername */
780     $server = $_SERVER['SERVER_NAME'];
781     if(tests::is_ip($server)){  
782       $server_name = gethostbyaddr($server);
783     }else{
784       $server_name = gethostbyaddr(gethostbyname($server));
785     }
787     /* If no ppd is selected, remove this attribute */
788     if(!empty($this->gotoPrinterPPD)) {
789       $this->gotoPrinterPPD = $method.str_replace("//","/",$server_name."/ppd/".$this->gotoPrinterPPD);
790     }else{
791       $this->gotoPrinterPPD = array();
792     }
794     $dn= $this->dn;
796     /* reduce objectClasses to minimun */
797     $this->attrs['objectClass']= $this->objectclasses;
799     plugin::save();
800     $ldap= $this->config->get_ldap_link();
802     /* Remove all empty values */
803     if ($this->orig_dn == 'new'){
804       $attrs= array();
805       foreach ($this->attrs as $key => $val){
806         if (is_array($val) && count($val) == 0){
807           continue;
808         }
809         $attrs[$key]= $val;
810       }
811       $this->attrs= $attrs;
812     }
814     /* Append printer user 
815      */
816     $this->attrs['gotoUserPrinter']=array();
817     foreach($this->member['AddUser'] as $mem){
818       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
819     }
821     /* Append printer group 
822      */
823     $this->attrs['gotoGroupPrinter'] = array();
824     foreach($this->member['AddGroup'] as $mem){
825       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
826     }
828     /* Append printer admin user 
829      */
830     $this->attrs['gotoUserAdminPrinter'] = array();
831     foreach($this->member['AddAdminUser'] as $mem){
832       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
833     }
835     /* Append printer admin group 
836      */
837     $this->attrs['gotoGroupAdminPrinter']= array();
838     foreach($this->member['AddAdminGroup'] as $mem){
839       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
840     }
842     if($this->orig_dn == 'new'){
843       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
844         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
845           unset($this->attrs[$checkVar]);
846         }
847       }
848     }
850     /* Ensure to create a new object */
851     if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/",$this->orig_dn)){
852       $this->orig_dn = "new";
853     }
855     /* Move object in necessary*/
856     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
857       $this->move($this->orig_dn, $this->dn);
858     }
860     /* Write back to ldap */
861     $ldap= $this->config->get_ldap_link();
862     $ldap->cat($this->dn);
863     if(!$ldap->count()){
864       $ldap->cd($this->config->current['BASE']);
865       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
866       $ldap->cd($this->dn);
868       /* Remove empty values */ 
869       foreach($this->attrs as $name => $value){
870         if(empty($value)){
871           unset($this->attrs[$name]);
872         }
873       }
875       $ldap->add($this->attrs);
876       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
877       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
878     } else {
879       $ldap->cd($this->dn);
880       $this->cleanup();
881       $ldap->modify ($this->attrs); 
882       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
883       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
884     }
885     if (!$ldap->success()){
886       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
887     }
889     #if(preg_match("/printer/i",$this->BelongsTo)){
890       $this->netConfigDNS->cn = $this->cn;
891       $this->netConfigDNS->dn = $this->dn;
892       $this->netConfigDNS->save();
893     #}
894   }
896   function generateList(){
897     $a_return=array();
899     foreach($this->member as $type => $values){
900       $a_return[$type]=array();
901       foreach($values as $value){
902         if((preg_match("/Group/i",$type))){
903           if(!isset($value['description'])){
904             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
905           }else{
906             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
907           }
908         }else{
909           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
910         }
911       }
912     }
913     return($a_return);
914   }
916   /* Return plugin informations for acl handling
917       #FIXME FAIscript seams to ununsed within this class... */
918   static function plInfo()
919   {
920     return (array(
921           "plShortName"   => _("Generic"),
922           "plDescription" => _("Print generic"),
923           "plSelfModify"  => FALSE,
924           "plDepends"     => array(),
925           "plPriority"    => 4,
926           "plSection"     => array("administration"),
927           "plCategory"    => array("printer" => array("description"  => _("Printer"),
928                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
929           "plProvidedAcls"=> array(
930             "cn"                => _("Name"),
931             "base"                => _("Base") ,         
932             "description"       => _("Description"), 
933             "l"                 => _("Location"), 
934             "labeledURI"        => _("LabeledURL"), 
935             "gotoPrinterPPD"    => _("Printer PPD"),
936             "gotoUserPrinter"   => _("Permissions")) 
937           ));
938   }
941   /* Delete member */
942   function DelMember($type,$id)
943   {
944     /* Check if there was a printer "dn" given, or the "cn" */
945     foreach($this->member[$type] as $key => $printer){
946       if($printer['dn'] == $id) {
947         $id = $key;
948       }
949     }
950   
951     if(!$this->acl_is_writeable("gotoUserPrinter")){
952       msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
953       return(FALSE);
954     }
955  
956     if(isset($this->member[$type][$id])){
957       unset($this->member[$type][$id]);
958       return(TRUE);
959     }
960     return(FALSE);
961   }
964   /* Add given obejct to members */
965   function AddMember($type,$dn)
966   {
967     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
968     if(!in_array_ics($type, $types)){
969       msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
970       return(FALSE);
971     }
973     if(!$this->acl_is_writeable("gotoUserPrinter")){
974       msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
975       return(FALSE);
976     }
978     /* Get name of index attributes */
979     if(preg_match("/user/i",$type)){
980       $var = "uid";
981     }else{
982       $var = "cn";
983     }
985     $ldap = $this->config->get_ldap_link();
986     $ldap->cd($dn);
987     $ldap->cat($dn,array($var,"cn"));
988     if($ldap->count()){
990       $attrs = $ldap->fetch();
992       if(isset($attrs[$var][0])){
993         $name = $attrs[$var][0];
995         /* Check if this uid/cn is already assigned to any permission */
996         foreach($types as $ctype){
998           /* If we want to add a user, only check user/userAdmin members */
999           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
1000             continue;
1001           }
1003           /* If we want to add a group, only check groups/adminGroups .. */
1004           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1005             continue;
1006           }
1008           if(isset(  $this->member[$ctype][$name])){
1009             msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1010             return(FALSE);
1011           }
1012         }
1014         /* Everything is fine. So add the given object to members */
1015         $this->member[$type][$attrs[$var][0]] = $attrs ;
1016       }else{
1017         print_a($attrs);
1018       }
1019     }else{
1020       msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1021       return(FALSE);
1022     }
1023     return(TRUE);
1024   }
1027    /* Display generic part for server copy & paste */
1028   function getCopyDialog()
1029   {
1030     $vars = array("cn");
1031     $smarty = get_smarty();
1032     $smarty->assign("cn" ,$this->cn);
1033     $smarty->assign("object","printer");
1034     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1035     $ret = array();
1036     $ret['string'] = $str;
1037     $ret['status'] = "";
1038     return($ret);
1039   }
1042   function saveCopyDialog()
1043   {
1044     if(isset($_POST['cn'])){
1045       $this->cn = $_POST['cn'];
1046     }
1047   }
1049   function PrepareForCopyPaste($source)
1050   {
1051     plugin::PrepareForCopyPaste($source);
1052     if(isset($source['macAddress'][0])){
1053       $this->netConfigDNS->macAddress = $source['macAddress'][0];
1054     }
1055     if(isset($source['ipHostNumber'][0])){
1056       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1057     }
1059     $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1060     foreach($this->attributes as $attr){
1061       $this->$attr = $source_o->$attr;
1062     }
1063     $this->member = $source_o -> member;
1065     $this->gotoPrinterPPD = "";
1066   }
1069 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1070 ?>