Code

Added support for general check hooks
[gosa.git] / plugins / admin / systems / class_printGeneric.inc
index 642f60f39605ab4ddd4112a53af9985a3acba96d..df9f7f12879878165062bdcb7a533e930683fa9c 100644 (file)
@@ -14,8 +14,6 @@ class printgeneric extends plugin
   /* Needed values and lists */
   var $base             = "";
   var $cn               = "";
-  var $macAddress       = "";
-  var $ipHostNumber     = "";
   var $l                = "";
   var $description      = "";
   var $labeledURI       = "";
@@ -30,20 +28,33 @@ class printgeneric extends plugin
   var $AdminMember      ="";
   var $AdminMembers     =array();
   var $AdminMemberKeys  =array();
-  
+
+  var $is_terminalBased = false; 
+  var $gotoUserPrinter ;
+
+  var $PPDdialogToSave  = NULL;
   var $member           =array();
   var $strings          = "";
   var $type             = "";
   var $dialog           =NULL;
 
+  var $netConfigDNS;
+  var $baseSelection    = false;
+
+  var $macAddress       = "";
+
   /* attribute list for save action */
-  var $attributes     = array("cn", "description", "l", "labeledURI", "macAddress", "ipHostNumber","gotoPrinterPPD");
+  var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress");
   var $objectclasses  = array("top", "gotoPrinter");
 
   function printgeneric ($config, $dn= NULL)
   {
     plugin::plugin ($config, $dn);
     $ldap= $this->config->get_ldap_link();
+    
+    $this->netConfigDNS = new termDNS($this->config, $this->dn,$this->objectclasses);
+
 
     /* Set base */
     if ($this->dn == "new"){
@@ -55,19 +66,35 @@ class printgeneric extends plugin
       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
     }
 
+    if(isset($this->gotoPrinterPPD)){
+      $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
+    }
 
     /* In case of gotoWorkstation this tab is calles from workstation plugin
      * in case of gotoTerminal it is called from a terminal tab
      * else it is a standalone printer
      */
     if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
-      $this->is_terminal = "true";   
-      $this->dn   = preg_replace("/ou=workstation/","ou=printer",$this->dn);
-      $this->type = "station";
+      /* Fix for new Workstations */
+      if(preg_match("/ou=incoming/",$this->dn)){    
+        $this->is_terminal = "true";   
+        $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
+        $this->type = "station";
+      }else{
+        $this->is_terminal = "true";   
+        $this->dn   = preg_replace("/ou=workstations/","ou=printers",$this->dn);
+        $this->type = "station";
+      }
     }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
-      $this->type = "terminal";
-      $this->is_terminal = "true";
-      $this->dn   = preg_replace("/ou=terminal/","ou=printer",$this->dn);
+      if(preg_match("/ou=incoming/",$this->dn)){    
+        $this->is_terminal = "true";   
+        $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
+        $this->type = "terminal";
+      }else{
+        $this->type = "terminal";
+        $this->is_terminal = "true";
+        $this->dn   = preg_replace("/ou=terminals/","ou=printers",$this->dn);
+      }
     }else{
       /* Save dn for later references */
       $this->orig_dn= $this->dn;
@@ -90,7 +117,6 @@ class printgeneric extends plugin
       }
     }
 
-   
     /* Prepare different member types 
      */ 
     foreach(array("AddUser"       =>"gotoUserPrinter",
@@ -101,7 +127,9 @@ class printgeneric extends plugin
       /* $this->members contains all members */
       $this->member[$type]=array();
 
-      unset($this->attrs[$attr]['count']);
+      if (isset($this->attrs[$attr]['count'])) {
+        unset($this->attrs[$attr]['count']);
+      }
 
       if(isset($this->attrs[$attr])){
         foreach($this->attrs[$attr] as $mem){
@@ -121,10 +149,49 @@ class printgeneric extends plugin
         }
       }
     }
+  
+    $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
+    if($attrs = $ldap->fetch()){
+      if(in_array("gotoTerminal",$attrs['objectClass'])){
+        $this->is_terminalBased = "Terminal";
+      }else{
+        $this->is_terminalBased = "Workstation";
+      }
+    }
+
+    /* A printer was only saved if the printer belongs to a workstation. 
+     * If there was a new single printer created, it was never saved.
+     * -This allows new printers to be saved.
+     */
+    if(($this->dn == "new")&&(!$this->is_terminal)){
+      $this->is_account = true;
+    }
   }
 
   function execute()
   {
+       /* Call parent execute */
+       plugin::execute();
+
+    if(isset($this->parent->by_name['workgeneric'])){
+//      echo "workstation";
+      if($this->parent->by_object['workgeneric']->cn == "wdefault"){
+        $display= $this->show_header(_("Add printer extension"),
+            _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
+        $this->is_account= false;
+        return $display;
+      }
+    }elseif(isset($this->parent->by_name['termgeneric'])){
+      if($this->parent->by_object['termgeneric']->cn == "default"){
+        $display= $this->show_header(_("Add printer extension"),
+            _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
+        $this->is_account= false;
+        return $display;
+      }
+    }elseif(isset($this->parent->by_name['printgeneric'])){
+//      echo "printer";
+    }
+
 
     $smarty= get_smarty();
     $display="";
@@ -149,32 +216,68 @@ class printgeneric extends plugin
     }
 
     if($this->is_terminal){
-      if(empty($this->cn))
-        $display= $this->show_header(_("Add printer extension"),
+      if(empty($this->cn)){
+        if(!in_array("gotoPrinter",$this->objectclasses)){
+          $display= $this->show_header(_("Add printer extension"),
               _("This workstation has printer extension disabled. You can't enable it while 'cn' is not present in entry. Possibly you are currently creating a new terminal template"),TRUE,TRUE);
-        $this->is_account= false;
-        return $display;
-
-       if ($this->is_account){
-        if($this->type=="station"){
-          $display= $this->show_header(_("Remove printer extension"),
-              _("This workstation has printer extension enabled.You can disable it by clicking below."));
+          $this->is_account= false;
+          return $display;
         }else{
-          $display= $this->show_header(_("Remove printer extension"),
-              _("This terminal has printer extension enabled. You can disable it by clicking below."));
+          $smarty->assign("is_terminal","false");
+          $skip = true;
         }
-      }else{
-        if($this->type=="station"){
-          $display= $this->show_header(_("Add printer extension"),
-              _("This workstation has printer extension disabled. You can enable it by clicking below."));
+      }
+
+      if(!isset($skip)){
+        if (($this->is_account)){
+          if($this->type=="station"){
+            $display= $this->show_header(_("Remove printer extension"),
+                _("This workstation has printer extension enabled.You can disable it by clicking below."));
+          }else{
+            $display= $this->show_header(_("Remove printer extension"),
+                _("This terminal has printer extension enabled. You can disable it by clicking below."));
+          }
         }else{
-          $display= $this->show_header(_("Add printer extension"),
-              _("This terminal has printer extension disabled. You can enable it by clicking below."));
-        }  
-        return ($display);
+          if($this->type=="station"){
+            $display= $this->show_header(_("Add printer extension"),
+                _("This workstation has printer extension disabled. You can enable it by clicking below."));
+          }else{
+            $display= $this->show_header(_("Add printer extension"),
+                _("This terminal has printer extension disabled. You can enable it by clicking below."));
+          }  
+          return ($display);
+        }
       }
     }
 
+    /* Base select dialog */
+    $once = true;
+    foreach($_POST as $name => $value){
+      if(preg_match("/^chooseBase/",$name) && $once){
+        $once = false;
+        $this->dialog = new baseSelectDialog($this->config);
+        $this->dialog->setCurrentBase($this->base);
+        $this->baseSelection = true;
+      }
+    }
+
+    /* Dialog handling */
+    if(is_object($this->dialog)){
+      /* Must be called before save_object */
+      $this->dialog->save_object();
+      if($this->baseSelection){
+        if($this->dialog->isClosed()){
+          $this->dialog = false;
+          $this->baseSelection = false;
+        }elseif($this->dialog->isSelected()){
+          $this->base = $this->dialog->isSelected();
+          $this->dialog= false;
+          $this->baseSelection = false;
+        }else{
+          return($this->dialog->execute());
+        }
+      }
+    }
 
     /* Do we represent a valid printer? */
     if (!$this->is_account && $this->parent == NULL){
@@ -192,7 +295,7 @@ class printgeneric extends plugin
       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
       $smarty->assign("$attr", $this->$attr);
     }
-    
+
     if(isset($_POST['AddUser'])){
       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
     }
@@ -207,33 +310,47 @@ class printgeneric extends plugin
     }
 
     if(isset($_POST['EditDriver'])){
-      $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
+      if($this->PPDdialogToSave){
+        $this->dialog = $this->PPDdialogToSave;
+      }else{
+        $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
+      }
     }
 
     if(isset($_POST['PrinterCancel'])){
       unset($this->dialog);
       $this->dialog= NULL;
     }
+    
+    if(isset($_POST['RemoveDriver'])){
+      $this->gotoPrinterPPD = array();
+      $this->PPDdialogToSave = NULL;
+    }
 
     if(isset($_POST['SavePPD'])){
+      $this->dialog->save_object();
       if(count($this->dialog->check())){
         foreach($this->dialog->check() as $msg){
           print_red($msg);
         }
       }else{
         $this->gotoPrinterPPD = array();
+        
         $this->gotoPrinterPPD = $this->dialog->save();
+        $this->PPDdialogToSave = $this->dialog;
         unset($this->dialog);
         $this->dialog=NULL;
       }
 
     }
-    
+
     if(isset($_POST['ClosePPD'])){
       unset($this->dialog);
       $this->dialog=NULL;
     }
 
+  
+
     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
       if(isset($this->member['AddUser'][$_POST['UserMember']])){
         unset($this->member['AddUser'][$_POST['UserMember']]);
@@ -253,7 +370,7 @@ class printgeneric extends plugin
     }
 
     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
-      if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
+      if(isset($this->member['AddAdminGroup'][$_POST['AdminMember']])){
         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
       }
     }
@@ -284,29 +401,51 @@ class printgeneric extends plugin
     }
 
     if($this->dialog != NULL){
+      $this->dialog->save_object();
       $display = $this->dialog->execute();
       return $display;
     }
 
-    /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
-    $smarty->assign("staticAddress", "");
+    require_once ("class_ppdManager.inc");
 
+    if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
 
-    require_once ("class_ppdManager.inc");
-    $ppdManager= new ppdManager('/var/spool/ppd/');
-    if(!empty($this->gotoPrinterPPD)){
-      $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
+      $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
+      if(!preg_match("/\/$/",$path)){
+        $path = $path."/";
+      }
+
+      $ppdManager= new ppdManager($path);
+      if(!empty($this->gotoPrinterPPD)){
+        if((!file_exists($path.$this->gotoPrinterPPD))){
+          $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
+        }else{
+          $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
+        }
+      }else{
+        $smarty->assign("driverInfo", _("not defined"));
+      }
     }else{
-      $smarty->assign("driverInfo", _("Undefined"));
+      $smarty->assign("driverInfo",_("can't get ppd informations."));
     }
 
+
     $list=$this->generateList();
     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
 
     asort($userlist);
     asort($adminlist);
-
+    if($this->is_terminalBased){
+      if($this->is_terminalBased == "Terminal"){
+        $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
+      }else{
+        $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
+      }
+      $smarty->assign("cnACL"    ," disabled ");
+    }else{
+      $smarty->assign("desc"    ,"");
+    }
     $smarty->assign("UserMember"    ,$this->UserMember);
     $smarty->assign("UserMembers"   ,$userlist);
     $smarty->assign("UserMemberKeys",array_flip($userlist));
@@ -314,33 +453,39 @@ class printgeneric extends plugin
     $smarty->assign("AdminMember"    ,$this->AdminMember);
     $smarty->assign("AdminMembers"   ,$adminlist);
     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
-    $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
+    $smarty->assign("netconfig", $this->netConfigDNS->execute());
 
     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
   }
 
   function remove_from_parent()
   {
-    $ldap= $this->config->get_ldap_link();
-    $ldap->rmdir($this->dn);
-    show_ldap_error($ldap->get_error());
-    $this->handle_post_events("remove");
-
-    /* Delete references to object groups */
-    $ldap->cd ($this->config->current['BASE']);
-    $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
-    while ($ldap->fetch()){
-      $og= new ogroup($this->config, $ldap->getDN());
-      unset($og->member[$this->dn]);
-      $og->save ();
+    if($this->initially_was_account){
+      $ldap= $this->config->get_ldap_link();
+      $ldap->cat($this->dn);
+      if(count($ldap->fetch()) ){
+
+        $this->netConfigDNS->remove_from_parent();
+        $ldap->rmdir($this->dn);
+        show_ldap_error($ldap->get_error());
+        $this->handle_post_events("remove");
+
+        /* Delete references to object groups */
+        $ldap->cd ($this->config->current['BASE']);
+        $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
+        while ($ldap->fetch()){
+          $og= new ogroup($this->config, $ldap->getDN());
+          unset($og->member[$this->dn]);
+          $og->save ();
+        }
+      }
     }
   }
-
   /* Save data to object */
   function save_object()
   {
     plugin::save_object();
-
+    $this->netConfigDNS->save_object();
     /* Save base, since this is no LDAP attribute */
     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
       $this->base= $_POST['base'];
@@ -350,7 +495,20 @@ class printgeneric extends plugin
   /* Check supplied data */
   function check()
   {
-    $message= array();
+    /* Call common method to give check the hook */
+    $message= plugin::check();
+    $message= array_merge($message, $this->netConfigDNS->check());
+
+    if(isset($this->parent->by_name['workgeneric'])){
+      if($this->parent->by_object['workgeneric']->cn == "wdefault"){
+        return $message;
+      }
+    }elseif(isset($this->parent->by_name['termgeneric'])){
+      if($this->parent->by_object['termgeneric']->cn == "default"){
+        return $message;
+      }
+    }
+
     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
 
     /* must: cn */
@@ -386,16 +544,32 @@ class printgeneric extends plugin
   /* Save to LDAP */
   function save()
   {
+    if (!$this->is_account){
+      return;
+    }
+    if(isset($this->parent->by_name['workgeneric'])){
+      if($this->parent->by_object['workgeneric']->cn == "wdefault"){
+        return;
+      }
+    }elseif(isset($this->parent->by_name['termgeneric'])){
+      if($this->parent->by_object['termgeneric']->cn == "default"){
+        return;
+      }
+    }
+
+    if($this->PPDdialogToSave){
+      $this->PPDdialogToSave->save_ppd();
+    }
     $dn= $this->dn;
     plugin::save();
     $ldap= $this->config->get_ldap_link();
    
     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
-      $this->dn= preg_replace("/ou=terminal/","ou=printer",$this->dn);
+      $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
     }
     
     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
-      $this->dn= preg_replace("/ou=workstation/","ou=printer",$this->dn);
+      $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
     }
 
     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
@@ -426,9 +600,22 @@ class printgeneric extends plugin
       $this->attrs= $attrs;
     }
 
+    if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
+      $method="https://";
+    }else{
+      $method="http://";
+    }
+  
+    if(!is_array($this->attrs['gotoPrinterPPD'])) {
+      $this->attrs['gotoPrinterPPD'] = $this->attrs['gotoPrinterPPD'];
+      $this->attrs['gotoPrinterPPD'] = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->attrs['gotoPrinterPPD']);
+    }
 
     /* Append printer user 
      */
+    if(is_string( $this->attrs['gotoUserPrinter'])){
+       $this->attrs['gotoUserPrinter']=array();
+    }
     foreach($this->member['AddUser'] as $mem){
       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
     }
@@ -465,11 +652,16 @@ class printgeneric extends plugin
       }
 
       $ldap->cd($this->dn);
-      $ldap->modify($this->attrs);
+      $this->cleanup();
+$ldap->modify ($this->attrs); 
+
       $this->handle_post_events("modify");
     }
     show_ldap_error($ldap->get_error());
 
+    $this->netConfigDNS->cn = $this->cn;
+    $this->netConfigDNS->save($this->dn);
+
     /* Optionally execute a command after we're done */
     $this->postcreate();
   }