Code

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