Code

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