Code

Updated device item config
[gosa.git] / 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 $ppdServerPart   = "";
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 ;
44     var $baseSelector;
46     /* attribute list for save action */
47     var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress", 
48             "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter","gosaUnitTag");
49     var $objectclasses  = array("top", "gotoPrinter");
50     var $view_logged    = FALSE;
51     var $parent;
54     // Memeber dialog object
55     var $userSelect;
56     var $adminUserSelect;
58     function printgeneric (&$config, $dn,$parent_init,$parent)
59     {
60         $this->config = &$config;
61         $this->dn = $dn; 
63         /* If parent was posted(the tabs object) we can detect the printer type. */
64         if($parent){
65             $this->parent = $parent;
66             $this->getTypeOfPrinter();
67         }else{
68             $this->BelongsTo = "unknown";
69             return;
70         }
72         /* Update dn, to ensure storing as printer instead of WS / terminal */
73         if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
74             $this->dn= preg_replace("/".preg_quote(get_ou("termgeneric", "terminalRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
75         }
77         if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
78             $this->dn= preg_replace("/".preg_quote(get_ou("workgeneric", "workstationRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
79         }
81         $this->orig_dn = $this->dn;
83         /* Get printer settings, possibly dn has changed */
84         plugin::plugin ($config, $this->dn);
86         /* Get is_account initially_was_account status */
87         $this->getTypeOfPrinter(true);
89         /* set orig dn to new if object is new */
90         $ldap= $this->config->get_ldap_link();
91         $ldap->cat($this->dn, array('dn'));
92         if(!$ldap->count()){
93             $this->orig_dn = "new";
94         }
96         /* create dns object */
97         $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
99         /* Set base */
100         if ($this->dn == "new"){
101             $ui= get_userinfo();
102             $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
103             $this->cn= "";
104         } else {
106             /* Set base and check if the extracted base exists */
107             if(preg_match("/".preg_quote(get_ou("ArpNewDevice", "systemIncomingRDN"), '/')."/i",$this->dn)){
108                 $this->base= preg_replace("/".preg_quote(get_ou("ArpNewDevice", "systemIncomingRDN"), '/')."/i","",dn2base($this->dn));
109             }else{
110                 $this->base= preg_replace("/".preg_quote(get_ou("printgeneric", "printerRDN"), '/')."/i","",dn2base($this->dn));
111             }
113             if(!isset($this->config->idepartments[$this->base])){
114                 msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
115                 $this->base  = session::get('CurrentMainBase');
116             }
117         }
119         /* If no ppd is selected, remove this attribute */
120         if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
121             $this->ppdServerPart = preg_replace("/^(http.*ppd)\/.*$/i","\\1",$this->gotoPrinterPPD);
122         }else{
124             /* Detect PPD server part */
125             if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
126                 $method="https://";
127             }else{
128                 $method="http://";
129             }
131             /* Get servername */
132             $server = $_SERVER['SERVER_NAME'];
133             if(tests::is_ip($server)){  
134                 $server_name = @gethostbyaddr($server);
135             }else{
136                 $server_name = @gethostbyaddr(gethostbyname($server));
137             }
138             $this->ppdServerPart  = $method.str_replace("//","/",$server_name."/ppd");
139         }
141         /* Extract selected ppd */
142         if(isset($this->gotoPrinterPPD)){
143             $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
144         }
146         $this->initial_PPD = $this->gotoPrinterPPD;
148         /* Prepare different member types */ 
149         foreach(array("AddUser"       =>"gotoUserPrinter",
150                     "AddGroup"      =>"gotoGroupPrinter",
151                     "AddAdminUser"  =>"gotoUserAdminPrinter",
152                     "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
154             /* $this->members contains all members */
155             $this->member[$type]=array();
157             if(isset($this->attrs[$attr])){
158                 $ldap->cd($this->config->current['BASE']) ;
159                 for($i = 0 ;  $i < $this->attrs[$attr]['count']; $i++){
161                     $mem = $this->attrs[$attr][$i];
162                     if(preg_match("/Group/",$type)){
163                         $ldap->search("(&(|(objectClass=posixGroup)(objectClass=gosaGroupOfNames))(cn=".$mem."))",array("cn","description"));
164                         if($ldap->count()){
165                             $entry = $ldap->fetch();
166                             $this->member[$type][$entry['cn'][0]]=$entry;
167                         }
168                     }else{
169                         $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
170                         if($ldap->count()){
171                             $entry = $ldap->fetch();
172                             $this->member[$type][$entry['uid'][0]]=$entry;
173                         }
174                     }
175                 }
176             }
177         }
178         $this->orig_cn    = $this->cn;
179         $this->orig_base  = $this->base;
181         /* Instanciate base selector */
182         $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
183         $this->baseSelector->setSubmitButton(false);
184         $this->baseSelector->setHeight(300);
185         $this->baseSelector->update(true);
186     }
188     function set_acl_base($base)
189     {
190         plugin::set_acl_base($base);
191         if(is_object($this->netConfigDNS)){
192             $this->netConfigDNS->set_acl_base($base);
193         }
194     }
196     function set_acl_category($cat)
197     {
198         plugin::set_acl_category($cat);
199         if(is_object($this->netConfigDNS)){
200             $this->netConfigDNS->set_acl_category($cat);
201         }
202     }
204     /* Detect type of printer.
205      * Printer can be stand alone, belong to a workstation or belong to a terminal. 
206      * We can detect the type printer type when comparing the tabs objects
207      */
208     function getTypeOfPrinter($UpdateAccountStatus = false)
209     {
210         /* Disable account as default
211          */  
212         $this->is_account = $this->initially_was_account = false;
214         /* Detect type of printer via parent tabs.
215          */
217         $class = get_class($this->parent);
218         if(isset($this->parent->by_object['workgeneric'])){
220             /* Exclude templates 
221              */
222             $this->cn = $this->parent->by_object['workgeneric']->cn;
223             if($this->parent->by_object['workgeneric']->cn == "wdefault"){
224                 $this->BelongsTo = "WorkstationTemplate";
225             }else{
226                 $this->BelongsTo = "Workstation";
227             }
228         }elseif(isset($this->parent->by_object['termgeneric'])){
230             /* Exclude templates 
231              */
232             $this->cn = $this->parent->by_object['termgeneric']->cn;
233             if($this->parent->by_object['termgeneric']->cn == "default"){
234                 $this->BelongsTo = "TerminalTemplate";  
235             }else{
236                 $this->BelongsTo = "Terminal";
237             }
238         }elseif(isset($this->parent->by_name['printgeneric'])){
239             $this->BelongsTo  = "Printer";
240         }
242         if($UpdateAccountStatus){
244             /* Set is_account / was account 
245              */
246             if($this->dn == "new"){
247                 $this->initially_was_account = false;
248             }
250             /* If is printer it must be a true account.
251              */
252             if(preg_match("/printer/i",$this->BelongsTo)){
253                 $this->is_account = true;
254             }
256             /* Update dn, to ensure storing as printer instead of WS / terminal
257              */
258             if(preg_match("/terminal/i",$this->BelongsTo)){
259                 $this->dn= preg_replace("/".preg_quote(get_ou("termgeneric", "terminalRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
260             }
262             if(preg_match("/workstation/i",$this->BelongsTo)){
263                 $this->dn= preg_replace("/".preg_quote(get_ou("workgeneric", "workstationRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
264             }
266             /* Detect if this is a valid printer account;
267              */
268             $ldap = $this->config->get_ldap_link();
269             $ldap->cat($this->dn, array('objectClass'));
271             if($ldap->count()){
272                 $attrs = $ldap->fetch();
273                 if(in_array("gotoPrinter",$attrs['objectClass'])){
274                     $this->initially_was_account = true;
275                     $this->is_account             = true;
276                 }else{
277                     $this->is_account = false;
278                 }
279             }
280         }
281     }
283     function execute()
284     {
285         /* Call parent execute */
286         plugin::execute();
288         if($this->is_account && !$this->view_logged){
289             $this->view_logged = TRUE;
290             new log("view","printer/".get_class($this),$this->dn);
291         }
293         /* If type of printer couldn't be detected (because of missing parent object in construction) 
294          * hide this tab.
295          */
296         if(preg_match("/unknown/i",$this->BelongsTo)){
297             $display= $this->show_enable_header(_("Add printer extension"),
298                     _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
299             return($display);
300         }
302         /* Templates can't have printer extensions 
303          */
304         if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
305             $display= $this->show_enable_header(_("Add printer extension"),
306                     _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
307             return($display);
308         }
309         if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
310             $display= $this->show_enable_header(_("Add printer extension"),
311                     _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
312             return($display);
313         }
315         /* Get cn from base object */
316         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
317             $this->cn = $this->parent->by_object['workgeneric']->cn;
318         }
319         if(preg_match("/^Terminal$/i",$this->BelongsTo)){
320             $this->cn = $this->parent->by_object['termgeneric']->cn;
321         }
323         $smarty= get_smarty();
326         /* Assign acls */
327         $tmp = $this->plInfo();
328         foreach($tmp['plProvidedAcls'] as $name => $translation){
329             $smarty->assign($name."ACL", $this->getacl($name));
330         }
332         $display="";
334         /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
335         if(preg_match("/^Printer$/i",$this->BelongsTo)){    
336             $smarty->assign("StandAlone",true);
337         }else{
338             $smarty->assign("StandAlone",false);
339         }
341         /* Do we need to flip is_account state? */
342         if(isset($_POST['modify_state'])){
343             if($this->is_account && $this->acl_is_removeable()){
344                 $this->is_account= FALSE;
345             }elseif(!$this->is_account && $this->acl_is_createable()){
346                 $this->is_account= TRUE;
347             }
348         }
350         /* Do we represent a valid printer? */
351         if (!$this->is_account && $this->parent === NULL){
352             $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
353                 msgPool::noValidExtension(_("printer"))."</b>";
354             return($display);
355         }
357         /* If this is a WS / Terminal depending printer, display account state button */
358         if(!preg_match("/^Printer$/i",$this->BelongsTo)){
359             if($this->cn == "" && ($this->dn != "new")){
360                 $display= $this->show_enable_header(_("Add printer extension"),
361                         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);
362                 $this->is_account= false;
363                 return $display;
364             }
366             if (($this->is_account)){
367                 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
368                     $display= $this->show_disable_header(_("Remove printer extension"),
369                             msgPool::featuresEnabled(_("printer")));
370                 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
371                     $display= $this->show_disable_header(_("Remove printer extension"),
372                             msgPool::featuresDisabled(_("printer")));
373                 }
374             }else{
375                 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
376                     $display= $this->show_enable_header(_("Add printer extension"),
377                             msgPool::featuresEnabled(_("printer")));
378                 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
379                     $display= $this->show_enable_header(_("Add printer extension"),
380                             msgPool::featuresDisabled(_("printer")));
381                 }  
382                 return ($display);
383             }
384         }
386         /* Assign attributes */
387         foreach ($this->attributes as $attr){
388             $smarty->assign("$attr", set_post($this->$attr));
389         }
391         $smarty->assign("base", $this->baseSelector->render());
393         // Act on add user/grouo requests 
394         if(isset($_POST['AddUser'])){
395             $this->userSelect = new userGroupSelect($this->config, get_userinfo());
396             $this->dialog = TRUE;
397         }
398         if(isset($_POST['AddAdminUser'])){
399             $this->adminUserSelect = new userGroupSelect($this->config, get_userinfo());
400             $this->dialog = TRUE;
401         }
403         /* Display ppd configure/select dialog      */
404         if(isset($_POST['EditDriver'])){
405             if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
406                 $this->dialog = $this->PPDdialogToSave;
407             }else{
409                 if(is_array($this->gotoPrinterPPD)){
410                     $this->dialog = new printerPPDDialog($this->config, $this->dn,"");
411                 }else{
412                     $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
413                 }
414                 $this->dialog->cn= $this->cn;
415             }
417             // Detect edit acl base
418             $ldap = $this->config->get_ldap_link();
419             if($ldap->dn_exists($this->dn)){
420                 $acl_base = $this->dn;
421             }else{
422                 $acl_base = $this->base;
423             }
425             $this->dialog->set_acl_base($acl_base);
426         }
428         /* remove ppd */
429         if(isset($_POST['RemoveDriver'])){
430             /* Detect PPD server part */
431             if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
432                 $method="https://";
433             }else{
434                 $method="http://";
435             }
437             /* Get servername */
438             $server = $_SERVER['SERVER_NAME'];
439             if(tests::is_ip($server)){  
440                 $server_name = @gethostbyaddr($server);
441             }else{
442                 $server_name = @gethostbyaddr(gethostbyname($server));
443             }
444             $this->ppdServerPart  = $method.str_replace("//","/",$server_name."/ppd");
445             $this->gotoPrinterPPD = array();
446             $this->PPDdialogToSave = NULL;
447         }
449         /* Close ppd dialog */
450         if(isset($_POST['ClosePPD'])){
451             unset($this->dialog);
452             $this->dialog=FALSE;
453         }
455         /* Save selected ppd */
456         if(isset($_POST['SavePPD'])){
457             $this->dialog->save_object();
458             if(count($this->dialog->check())){
459                 foreach($this->dialog->check() as $msg){
460                     msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
461                 }
462             }else{
463                 $this->gotoPrinterPPD = array();
464                 $this->gotoPrinterPPD = $this->dialog->save();
465                 $this->PPDdialogToSave = $this->dialog;
466                 unset($this->dialog);
467                 $this->dialog=FALSE;
468             }
469         }
471         /* Member management, delete user / group / admin ..*/
472         if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
473             foreach($_POST['UserMember'] as $mem){
474                 $this->DelMember('AddUser',$mem);
475             }
476         }
478         if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
479             foreach($_POST['UserMember'] as $mem){
480                 $this->DelMember('AddGroup',$mem);
481             }
482         }
484         if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
485             foreach($_POST['AdminMember'] as $mem){
486                 $this->DelMember('AddAdminUser',$mem);
487             }
488         }
490         if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
491             foreach($_POST['AdminMember'] as $mem){
492                 $this->DelMember('AddAdminGroup',$mem);
493             }
494         }
496         // Abort user / group adding dialog 
497         if(isset($_POST['userGroupSelect_cancel'])){
498             $this->dialog=FALSE;
499             $this->userSelect = NULL;
500             $this->adminUserSelect = NULL;
501         }
503         // Save selected users / groups 
504         if(isset($_POST['userGroupSelect_save'])){
505             $users = array();
506             if($this->userSelect instanceOf userGroupSelect){
507                 $users = $this->userSelect->save();
508                 $add = '';
509             }elseif($this->adminUserSelect instanceOf userGroupSelect){
510                 $users = $this->adminUserSelect->save();
511                 $add = 'Admin';
512             }
513             foreach($users as $user){
514                 if(in_array('gosaAccount', $user['objectClass'])){
515                     $type = 'Add'.$add.'User';  
516                 }else{
517                     $type = 'Add'.$add.'Group';  
518                 }
519                 $this->AddMember($type, $user['dn']);
520             }
521             $this->dialog=FALSE;
522             $this->userSelect = NULL;
523             $this->adminUserSelect = NULL;
524         }
526         // Display add user/group dialogs 
527         if($this->userSelect instanceOf userGroupSelect || $this->adminUserSelect instanceOf userGroupSelect){
529             // Build up blocklist
530             $used = array();
531             foreach($this->member as $type => $members){
532                 foreach($members as $member){
533                     $used['dn'][] = $member['dn'];
534                 }
535             }
536             session::set('filterBlacklist', $used);
537             if($this->userSelect instanceOf userGroupSelect){
538                 return($this->userSelect->execute());
539             }elseif($this->adminUserSelect instanceOf userGroupSelect){
540                 return($this->adminUserSelect->execute());
541             }
542         }
545         /* Display dialog, if there is currently one open*/
546         if(is_object($this->dialog)){
547             $this->dialog->save_object();
548             $display = $this->dialog->execute();
549             return $display;
550         }
552         /* Parse selected ppd file */
553         $config = session::get('config');
554         if ($config->get_cfg_value("core","ppdPath") != ""){
555             $path = $config->get_cfg_value("core","ppdPath");
556             if(!preg_match("/\/$/",$path)){
557                 $path = $path."/";
558             }
560             $ppdManager= new ppdManager($path);
561             $smarty->assign("displayServerPath",true);
562             if(!empty($this->gotoPrinterPPD)){
563                 if((!file_exists($path.$this->gotoPrinterPPD))){
564                     $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
565                 }else{
566                     $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD);
567                     $smarty->assign("driverInfo", set_post($ppdDesc['name']));
568                 }
569             }else{
570                 $smarty->assign("driverInfo", _("Not defined"));
571                 $smarty->assign("displayServerPath",false);
572             }
573         }else{
574             $smarty->assign("driverInfo",_("Can't get PPD informations."));
575             $smarty->assign("displayServerPath",true);
576         }
577         $smarty->assign("ppdServerPart",$this->ppdServerPart);
580         /* Create user & admin user list */
581         $list=$this->generateList();
582         $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
583         $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
585         asort($userlist);
586         asort($adminlist);
588         if(!preg_match("/Printer/i",$this->BelongsTo)){
589             if(preg_match("/Terminal/i",$this->BelongsTo)){
590                 $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
591             }else{
592                 $smarty->assign("desc"    ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
593             }
594             $smarty->assign("cnACL"    , $this->getacl("cn",true));
595         }else{
596             $smarty->assign("desc"    ,"");
597         }
598         $smarty->assign("UserMember"    ,$this->UserMember);
599         $smarty->assign("UserMembers"   ,$userlist);
600         $smarty->assign("UserMemberKeys",array_flip($userlist));
602         $smarty->assign("AdminMember"    ,$this->AdminMember);
603         $smarty->assign("AdminMembers"   ,$adminlist);
604         $smarty->assign("AdminMemberKeys",array_flip($adminlist));
605         if(preg_match("/Printer/i",$this->BelongsTo)){
607             /* Show main page */
608             $str = $this->netConfigDNS->execute();
609             if(is_object($this->netConfigDNS->dialog)){
610                 return($str);
611             }
612             $smarty->assign("netconfig", $str);
613         } else {
614             $smarty->assign("netconfig", "");
615         }
617         return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
618     }
620     function remove_from_parent()
621     {
622         /* Only remove if there was initially an account */
623         if($this->initially_was_account){
625             /* Update dn, to ensure storing as printer instead of WS / terminal
626              */
627             if(preg_match("/terminal/i",$this->BelongsTo)){
628                 $this->dn= preg_replace("/".preg_quote(get_ou("termgeneric", "terminalRDN"), '/').",/i",get_ou("printgeneric", "printerRDN"),$this->dn);
629             }
631             if(preg_match("/workstation/i",$this->BelongsTo)){
632                 $this->dn= preg_replace("/".preg_quote(get_ou("workgeneric", "workstationRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
633             }
635             /* Check if this dn points to a printer, to avoid deleting something else */
636             $ldap= $this->config->get_ldap_link();
637             $ldap->cat($this->dn, array('dn',"objectClass"));
638             if(!$ldap->count()){
639                 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
640                 return;
641             }
643             /* Check if obejct is a printer */
644             $CheckPrinter = $ldap->fetch();
645             if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
646                 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
647                 return;
648             }
650             /* Remove account & dns extension */ 
651             $this->netConfigDNS->remove_from_parent();
652             $ldap->rmdir($this->dn);
654             new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
656             if (!$ldap->success()){
657                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
658             }
659             $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
661             /* Delete references to object groups */
662             $ldap->cd ($this->config->current['BASE']);
663             $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
664             while ($ldap->fetch()){
665                 $og= new ogroup($this->config, $ldap->getDN());
666                 unset($og->member[$this->dn]);
667                 $og->save ();
668             }
670             /* Remove previously selected ppd file.*/
671             if(!empty($this->initial_PPD)){
672                 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
673                 $tmp->removeModifiedPPD();
674             }
675         }
676     }
679     /* Save data to object */
680     function save_object()
681     {
682         /* Create a base backup and reset the
683            base directly after calling plugin::save_object();
684            Base will be set seperatly a few lines below */
685         $base_tmp = $this->base;
686         plugin::save_object();
687         $this->base = $base_tmp;
689         if(isset($_POST['ppdServerPart'])){
690             $this->ppdServerPart = get_post('ppdServerPart');
691         }
693         if(is_object($this->netConfigDNS)){
694             $this->netConfigDNS->save_object();
695         }
697         /* Refresh base */
698         if ($this->acl_is_moveable($this->base)){
699             if (!$this->baseSelector->update()) {
700                 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
701             }
702             if ($this->base != $this->baseSelector->getBase()) {
703                 $this->base= $this->baseSelector->getBase();
704                 $this->is_modified= TRUE;
705             }
706         }
708     }
710     /* Check supplied data */
711     function check()
712     {
713         /* Call common method to give check the hook */
714         $message= plugin::check();
715         if (preg_match("/printer/i",$this->BelongsTo)){
716             $message= array_merge($message, $this->netConfigDNS->check());
717         }
719         /* Don't display check messages if this is a template object */
720         if(isset($this->parent->by_object['workgeneric'])){
721             if($this->parent->by_object['workgeneric']->cn == "wdefault"){
722                 return $message;
723             }
724         }elseif(isset($this->parent->by_object['termgeneric'])){
725             if($this->parent->by_object['termgeneric']->cn == "default"){
726                 return $message;
727             }
728         }
730         $dn= "cn=".$this->cn.get_ou("printgeneric", "printerRDN").",".$this->base;
732         /* must: cn */
733         if(($this->BelongsTo == "Printer") && $this->cn == ""){
734             $message[]= msgPool::required(_("Name"));
735         }
737         // Check if a wrong base was supplied
738         if(!$this->baseSelector->checkLastBaseUpdate()){
739             $message[]= msgPool::check_base();
740         }
742         /* must: cn */
743         if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
744             $message[]= msgPool::invalid(_("Name"));
745         }
747         /* must: labeledURI */
748         if(empty($this->labeledURI)){
749             $message[]= msgPool::required(_("Printer URL"));
750         }
752         /* Check if there is already an entry with this cn*/
753         if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
754             $ldap= $this->config->get_ldap_link();
755             $ldap->cd ($this->base);
756             $ldap->ls("(cn=".$this->cn.")",get_ou("printgeneric", "printerRDN").$this->base, array("cn"));
757             if ($ldap->count() != 0){
758                 while ($attrs= $ldap->fetch()){
759                     if(preg_match("/cn=dhcp,/",$attrs['dn'])){
760                         continue;
761                     }
762                     if ($attrs['dn'] != $this->orig_dn){
763                         $message[]= msgPool::duplicated(_("Name"));
764                         break;
765                     }
766                 }
767             }
768         }
770         /* Check if we are allowed to create or move this object
771          */
772         if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
773             $message[] = msgPool::permCreate();
774         }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
775             $message[] = msgPool::permMove();
776         }
778         return ($message);
779     }
782     /* Save to LDAP */
783     function save()
784     {
785         /* Update dn, to ensure storing as printer instead of WS / terminal
786          */
787         if(preg_match("/terminal/i",$this->BelongsTo)){
788             $this->dn= preg_replace("/".preg_quote(get_ou("termgeneric", "terminalRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
789         }
791         if(preg_match("/workstation/i",$this->BelongsTo)){
792             $this->dn= preg_replace("/".preg_quote(get_ou("workgeneric", "workstationRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
793         }
795         if(!$this->is_account) return;
796         if(isset($this->parent->by_object['workgeneric'])){
797             if($this->parent->by_object['workgeneric']->cn == "wdefault"){
798                 return;
799             }
801             /* Adapt IP & mac from parent object */
802             $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
803             $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
805         }elseif(isset($this->parent->by_object['termgeneric'])){
806             if($this->parent->by_object['termgeneric']->cn == "default"){
807                 return;
808             }
810             /* Adapt IP & mac from parent object */
811             $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
812             $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
813         }
815         /* If type is still unknown, the initialisation of this printer failed, abort. */
816         if(preg_match("/unknown/i",$this->BelongsTo)){
817             return;
818         }
820         /* save ppd configuration */
821         if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
823             $this->PPDdialogToSave->save_ppd();
825             /* Rename the generated ppd to match the gzip ending '.gz', if necessary.
826              */
827             $path = $this->config->get_cfg_value("core","ppdPath");
828             if(!preg_match("/\/$/",$path)){
829                 $path = $path."/";
830             }
831             $ppdManager= new ppdManager($path);
832             if($ppdManager->useGzip && !preg_match('/\.gz$/',$this->gotoPrinterPPD)){
833                 if(rename($path.$this->gotoPrinterPPD,$path.$this->gotoPrinterPPD.'.gz')){
834                     $this->gotoPrinterPPD .= '.gz';
835                 }
836             }elseif(!$ppdManager->useGzip && preg_match('/\.gz$/',$this->gotoPrinterPPD)){
837                 $new_ppd = preg_replace('/\.gz$/','',$this->gotoPrinterPPD);
838                 if(rename($path.$this->gotoPrinterPPD,$path.$new_ppd)){
839                     $this->gotoPrinterPPD = $new_ppd; 
840                 }
841             }
842         }
843         if($this->orig_dn != $this->dn && $this->orig_dn != "new"){
844             if(!empty($this->gotoPrinterPPD)) {
845                 $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
846                 $this->PPDdialogToSave->cn = $this->cn;
847                 $this->PPDdialogToSave->generateProperties();
848                 $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
849             }
850         }
852         /* Remove previously selected ppd file.*/
853         if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
854             if(!empty($this->initial_PPD)){
855                 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
856                 $tmp->removeModifiedPPD();
857             }
858         }
860         /* If no ppd is selected, remove this attribute */
861         if(!empty($this->gotoPrinterPPD)) {
862             $this->gotoPrinterPPD = $this->ppdServerPart.'/'.$this->gotoPrinterPPD;
863         }else{
864             $this->gotoPrinterPPD = array();
865         }
867         $dn= $this->dn;
869         /* reduce objectClasses to minimun */
870         $this->attrs['objectClass']= $this->objectclasses;
872         plugin::save();
873         $ldap= $this->config->get_ldap_link();
875         /* Remove all empty values */
876         if ($this->orig_dn == 'new'){
877             $attrs= array();
878             foreach ($this->attrs as $key => $val){
879                 if (is_array($val) && count($val) == 0){
880                     continue;
881                 }
882                 $attrs[$key]= $val;
883             }
884             $this->attrs= $attrs;
885         }
887         /* Append printer user 
888          */
889         $this->attrs['gotoUserPrinter']=array();
890         foreach($this->member['AddUser'] as $mem){
891             $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
892         }
894         /* Append printer group 
895          */
896         $this->attrs['gotoGroupPrinter'] = array();
897         foreach($this->member['AddGroup'] as $mem){
898             $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
899         }
901         /* Append printer admin user 
902          */
903         $this->attrs['gotoUserAdminPrinter'] = array();
904         foreach($this->member['AddAdminUser'] as $mem){
905             $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
906         }
908         /* Append printer admin group 
909          */
910         $this->attrs['gotoGroupAdminPrinter']= array();
911         foreach($this->member['AddAdminGroup'] as $mem){
912             $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
913         }
915         if($this->orig_dn == 'new'){
916             foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
917                 if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
918                     unset($this->attrs[$checkVar]);
919                 }
920             }
921         }
923         /* Ensure to create a new object */
924         if(preg_match("/".preg_quote(get_ou("ArpNewDevice", "systemIncomingRDN"), '/')."/i",$this->orig_dn)){
925             $this->orig_dn = "new";
926         }
928         /* Move object in necessary*/
929         if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
930             $this->move($this->orig_dn, $this->dn);
931         }
933         /* Write back to ldap */
934         $ldap= $this->config->get_ldap_link();
935         $ldap->cat($this->dn);
936         if(!$ldap->count()){
937             $ldap->cd($this->config->current['BASE']);
938             $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
939             $ldap->cd($this->dn);
941             /* Remove empty values */ 
942             foreach($this->attrs as $name => $value){
943                 if(empty($value)){
944                     unset($this->attrs[$name]);
945                 }
946             }
948             $ldap->add($this->attrs);
949             $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
950             new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
951         } else {
952             $ldap->cd($this->dn);
953             $this->cleanup();
954             $ldap->modify ($this->attrs); 
955             $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
956             new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
957         }
958         if (!$ldap->success()){
959             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
960         }
962 #if(preg_match("/printer/i",$this->BelongsTo)){
963     $this->netConfigDNS->cn = $this->cn;
964     $this->netConfigDNS->dn = $this->dn;
965     $this->netConfigDNS->save();
966 #}
967     }
969     function generateList(){
970         $a_return=array();
972         foreach($this->member as $type => $values){
973             $a_return[$type]=array();
974             foreach($values as $value){
975                 if((preg_match("/Group/i",$type))){
976                     if(!isset($value['description'])){
977                         $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
978                     }else{
979                         $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
980                     }
981                 }else{
982                     $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
983                 }
984             }
985         }
986         return($a_return);
987     }
989     /* Return plugin informations for acl handling
990 #FIXME FAIscript seams to ununsed within this class... */
991     static function plInfo()
992     {
993         return (array(
994                     "plShortName"   => _("Generic"),
995                     "plDescription" => _("Print generic"),
996                     "plSelfModify"  => FALSE,
997                     "plDepends"     => array(),
998                     "plPriority"    => 4,
999                     "plSection"     => array("administration"),
1000                     "plRequirements"=> array(
1001                         'ldapSchema' => array('gotoPrinter' => '>=2.7'),
1002                         'onFailureDisablePlugin' => array(get_class())
1003                         ),
1004                     "plCategory"    => array("printer" => array("description"  => _("Printer"),
1005                             "objectClass"  => "gotoPrinter"),"workstation","terminal"),
1006                     "plProperties" =>
1007                     array(
1008                         array(
1009                             "name"          => "printerRDN",
1010                             "type"          => "rdn",
1011                             "default"       => "ou=printers,ou=systems,",
1012                             "description"   => _("The 'printerRDN' statement defines the location where new printers will be created. The default is 'ou=printers,ou=systems,'."),
1013                             "check"         => "gosaProperty::isRdn",
1014                             "migrate"       => "migrate_printerRDN",
1015                             "group"         => "plugin",
1016                             "mandatory"     => FALSE
1017                             )
1018                         ),
1020             "plProvidedAcls"=> array(
1021                     "cn"                => _("Name"),
1022                     "base"                => _("Base") ,         
1023                     "description"       => _("Description"), 
1024                     "l"                 => _("Location"), 
1025                     "labeledURI"        => _("Labeled URI"), 
1026                     "macAddress"        => _("Mac address"), 
1027                     "gotoPrinterPPD"    => _("Printer PPD"),
1028                     "gotoUserPrinter"   => _("Permissions")) 
1029                 ));
1030     }
1033     /* Delete member */
1034     function DelMember($type,$id)
1035     {
1036         /* Check if there was a printer "dn" given, or the "cn" */
1037         foreach($this->member[$type] as $key => $printer){
1038             if($printer['dn'] == $id) {
1039                 $id = $key;
1040             }
1041         }
1043         if(!$this->acl_is_writeable("gotoUserPrinter")){
1044             msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
1045             return(FALSE);
1046         }
1048         if(isset($this->member[$type][$id])){
1049             unset($this->member[$type][$id]);
1050             return(TRUE);
1051         }
1052         return(FALSE);
1053     }
1056     /* Add given obejct to members */
1057     function AddMember($type,$dn)
1058     {
1059         $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
1060         if(!in_array_ics($type, $types)){
1061             msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
1062             return(FALSE);
1063         }
1065         if(!$this->acl_is_writeable("gotoUserPrinter")){
1066             msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
1067             return(FALSE);
1068         }
1070         /* Get name of index attributes */
1071         if(preg_match("/user/i",$type)){
1072             $var = "uid";
1073         }else{
1074             $var = "cn";
1075         }
1077         $ldap = $this->config->get_ldap_link();
1078         $ldap->cd($dn);
1079         $ldap->cat($dn,array($var,"cn"));
1080         if($ldap->count()){
1082             $attrs = $ldap->fetch();
1084             if(isset($attrs[$var][0])){
1085                 $name = $attrs[$var][0];
1087                 /* Check if this uid/cn is already assigned to any permission */
1088                 foreach($types as $ctype){
1090                     /* If we want to add a user, only check user/userAdmin members */
1091                     if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
1092                         continue;
1093                     }
1095                     /* If we want to add a group, only check groups/adminGroups .. */
1096                     if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1097                         continue;
1098                     }
1100                     if(isset(  $this->member[$ctype][$name])){
1101                         msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1102                         return(FALSE);
1103                     }
1104                 }
1106                 /* Everything is fine. So add the given object to members */
1107                 $this->member[$type][$attrs[$var][0]] = $attrs ;
1108             }else{
1109                 print_a($attrs);
1110             }
1111         }else{
1112             msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1113             return(FALSE);
1114         }
1115         return(TRUE);
1116     }
1119     /* Display generic part for server copy & paste */
1120     function getCopyDialog()
1121     {
1122         $vars = array("cn");
1123         $smarty = get_smarty();
1124         $smarty->assign("cn" ,$this->cn);
1125         $smarty->assign("object","printer");
1126         $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1127         $ret = array();
1128         $ret['string'] = $str;
1129         $ret['status'] = "";
1130         return($ret);
1131     }
1134     function saveCopyDialog()
1135     {
1136         if(isset($_POST['cn'])){
1137             $this->cn = get_post('cn');
1138         }
1139     }
1141     function PrepareForCopyPaste($source)
1142     {
1143         plugin::PrepareForCopyPaste($source);
1144         if(isset($source['macAddress'][0])){
1145             $this->netConfigDNS->macAddress = $source['macAddress'][0];
1146         }
1147         if(isset($source['ipHostNumber'][0])){
1148             $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1149         }
1151         $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1152         foreach($this->attributes as $attr){
1153             $this->$attr = $source_o->$attr;
1154         }
1155         $this->member = $source_o -> member;
1157         $this->gotoPrinterPPD = "";
1158     }
1161     function is_modal_dialog()
1162     {
1163         return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
1164     }
1168 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1169 ?>