Code

Added logging to some classes
[gosa.git] / plugins / admin / systems / class_printGeneric.inc
index 44fc8330dfdd74825da5ed5cf95439e64bebbfd6..9000b008bebf3a644e6f6a9fff24d2bc915f51b3 100644 (file)
@@ -18,6 +18,7 @@ class printgeneric extends plugin
   var $description      = "";
   var $labeledURI       = "";
   var $gotoPrinterPPD   = "";
+  var $initial_PPD      = "";
   var $orig_dn          = "";
 
   var $UserMember       ="";
@@ -49,11 +50,13 @@ class printgeneric extends plugin
                               "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter");
   var $objectclasses  = array("top", "gotoPrinter");
 
-  function printgeneric ($config, $dn,$parent)
+  var $parent;
+
+  function printgeneric ($config, $dn,$parent_init,$parent)
   {
     $this->config = $config;
     $this->dn = $dn; 
-    
     /* If parent was posted(the tabs object) we can detect the printer type. */
     if($parent){
       $this->parent = $parent;
@@ -64,11 +67,11 @@ class printgeneric extends plugin
     }
 
     /* Update dn, to ensure storing as printer instead of WS / terminal */
-    if($this->BelongsTo == "Terminal"){
+    if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
     }
 
-    if($this->BelongsTo == "Workstation"){
+    if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
     }
 
@@ -115,6 +118,8 @@ class printgeneric extends plugin
       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
     }
 
+    $this->initial_PPD = $this->gotoPrinterPPD;
+
     /* Prepare different member types */ 
     foreach(array("AddUser"       =>"gotoUserPrinter",
           "AddGroup"      =>"gotoGroupPrinter",
@@ -146,8 +151,27 @@ class printgeneric extends plugin
         }
       }
     }
+    
+    if($this->is_account){
+      @log::log("view","printer/".get_class($this),$this->dn);
+    }
+  }
+
+  function set_acl_base($base)
+  {
+    plugin::set_acl_base($base);
+    if(is_object($this->netConfigDNS)){
+      $this->netConfigDNS->set_acl_base($base);
+    }
   }
 
+  function set_acl_category($cat)
+  {
+    plugin::set_acl_category($cat);
+    if(is_object($this->netConfigDNS)){
+      $this->netConfigDNS->set_acl_category($cat);
+    }
+  }
 
   /* Detect type of printer.
    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
@@ -161,6 +185,8 @@ class printgeneric extends plugin
 
     /* Detect type of printer via parent tabs.
      */
+
+    $class = get_class($this->parent);
     if(isset($this->parent->by_object['workgeneric'])){
 
       /* Exclude templates 
@@ -195,17 +221,17 @@ class printgeneric extends plugin
 
       /* If is printer it must be a true account.
        */
-      if($this->BelongsTo == "Printer"){
+      if(preg_match("/printer/i",$this->BelongsTo)){
         $this->is_account = true;
       }
 
       /* Update dn, to ensure storing as printer instead of WS / terminal
        */
-      if($this->BelongsTo == "Terminal"){
+      if(preg_match("/terminal/i",$this->BelongsTo)){
         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
       }
 
-      if($this->BelongsTo == "Workstation"){
+      if(preg_match("/workstation/i",$this->BelongsTo)){
         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
       }
 
@@ -234,7 +260,7 @@ class printgeneric extends plugin
     /* If type of printer couldn't be detected (because of missing parent object in construction) 
      * hide this tab.
      */
-    if($this->BelongsTo == "unknown"){
+    if(preg_match("/unknown/i",$this->BelongsTo)){
       $display= $this->show_enable_header(_("Add printer extension"),
           _("Could not intialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
       return($display);
@@ -242,22 +268,22 @@ class printgeneric extends plugin
 
     /* Templates can't have printer extensions 
      */
-    if($this->BelongsTo == "WorkstationTemplate"){
+    if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
       $display= $this->show_enable_header(_("Add printer extension"),
           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
       return($display);
     }
-    if($this->BelongsTo == "TerminalTemplate"){
+    if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
       $display= $this->show_enable_header(_("Add printer extension"),
           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
       return($display);
     }
 
     /* Get cn from base object */
-    if($this->BelongsTo == "Workstation"){
+    if(preg_match("/^Workstation$/i",$this->BelongsTo)){
       $this->cn = $this->parent->by_object['workgeneric']->cn;
     }
-    if($this->BelongsTo == "Terminal"){
+    if(preg_match("/^Terminal$/i",$this->BelongsTo)){
       $this->cn = $this->parent->by_object['termgeneric']->cn;
     }
 
@@ -269,18 +295,10 @@ class printgeneric extends plugin
       $smarty->assign($name."ACL", $this->getacl($name));
     }
 
-    /* Create base acls */
-    $baseACL = $this->getacl("base");
-    if(!$this->acl_is_moveable()) {
-      $baseACL = preg_replace("/w/","",$baseACL);
-    }
-    $smarty->assign("baseACL",          $baseACL);
-
-
     $display="";
 
     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
-    if($this->BelongsTo == "Printer"){    
+    if(preg_match("/^Printer$/i",$this->BelongsTo)){    
       $smarty->assign("StandAlone",true);
     }else{
       $smarty->assign("StandAlone",false);
@@ -303,7 +321,7 @@ class printgeneric extends plugin
     }
 
     /* If this is a WS / Terminal depending printer, display account state button */
-    if($this->BelongsTo != "Printer"){
+    if(!preg_match("/^Printer$/i",$this->BelongsTo)){
       if((empty($this->cn)) && ($this->dn != "new")){
         $display= $this->show_enable_header(_("Add printer extension"),
             _("This object 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);
@@ -312,18 +330,18 @@ class printgeneric extends plugin
       }
 
       if (($this->is_account)){
-        if($this->BelongsTo=="Workstation"){
+        if(preg_match("/^Workstation$/i",$this->BelongsTo)){
           $display= $this->show_disable_header(_("Remove printer extension"),
               _("This workstation has printer extension enabled.You can disable it by clicking below."));
-        }elseif($this->BelongsTo=="Terminal"){
+        }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
           $display= $this->show_disable_header(_("Remove printer extension"),
               _("This terminal has printer extension enabled. You can disable it by clicking below."));
         }
       }else{
-        if($this->BelongsTo=="Workstation"){
+        if(preg_match("/^Workstation$/i",$this->BelongsTo)){
           $display= $this->show_disable_header(_("Add printer extension"),
               _("This workstation has printer extension disabled. You can enable it by clicking below."));
-        }elseif($this->BelongsTo=="Terminal"){
+        }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
           $display= $this->show_enable_header(_("Add printer extension"),
               _("This terminal has printer extension disabled. You can enable it by clicking below."));
         }  
@@ -334,7 +352,7 @@ class printgeneric extends plugin
     /* Base select dialog */
     $once = true;
     foreach($_POST as $name => $value){
-      if(preg_match("/^chooseBase/",$name) && $once){
+      if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
         $once = false;
         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
         $this->dialog->setCurrentBase($this->base);
@@ -352,7 +370,13 @@ class printgeneric extends plugin
           $this->dialog = false;
           $this->baseSelection = false;
         }elseif($this->dialog->isSelected()){
-          $this->base = $this->dialog->isSelected();
+
+          /* A new base was selected, check if it is a valid one */
+          $tmp = $this->get_allowed_bases();
+          if(isset($tmp[$this->dialog->isSelected()])){
+            $this->base = $this->dialog->isSelected();
+          }
+
           $this->dialog= false;
           $this->baseSelection = false;
         }else{
@@ -389,6 +413,7 @@ class printgeneric extends plugin
         $this->dialog = $this->PPDdialogToSave;
       }else{
         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
+        $this->dialog->cn= $this->cn;
       }
     }
 
@@ -488,7 +513,7 @@ class printgeneric extends plugin
       $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."),$path.$this->gotoPrinterPPD))."</b>";
+          $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
         }else{
           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
         }
@@ -507,8 +532,8 @@ class printgeneric extends plugin
     asort($userlist);
     asort($adminlist);
 
-    if($this->BelongsTo != "Printer"){
-      if($this->BelongsTo == "Terminal"){
+    if(!preg_match("/Printer/i",$this->BelongsTo)){
+      if(preg_match("/Terminal/i",$this->BelongsTo)){
         $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>"));
@@ -524,7 +549,7 @@ class printgeneric extends plugin
     $smarty->assign("AdminMember"    ,$this->AdminMember);
     $smarty->assign("AdminMembers"   ,$adminlist);
     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
-    if($this->BelongsTo == "Printer"){
+    if(preg_match("/Printer/i",$this->BelongsTo)){
       $smarty->assign("netconfig", $this->netConfigDNS->execute());
     } else {
       $smarty->assign("netconfig", "");
@@ -540,11 +565,11 @@ class printgeneric extends plugin
 
       /* Update dn, to ensure storing as printer instead of WS / terminal
        */
-      if($this->BelongsTo == "Terminal"){
+      if(preg_match("/terminal/i",$this->BelongsTo)){
         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
       }
 
-      if($this->BelongsTo == "Workstation"){
+      if(preg_match("/workstation/i",$this->BelongsTo)){
         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
       }
 
@@ -566,6 +591,9 @@ class printgeneric extends plugin
       /* Remove account & dns extension */ 
       $this->netConfigDNS->remove_from_parent();
       $ldap->rmdir($this->dn);
+
+      @log::log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
+  
       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system print/generic with dn '%s' failed."),$this->dn));
       $this->handle_post_events("remove");
 
@@ -577,6 +605,12 @@ class printgeneric extends plugin
         unset($og->member[$this->dn]);
         $og->save ();
       }
+
+      /* Remove previously selected ppd file.*/
+      if(!empty($this->initial_PPD)){
+        $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
+        $tmp->removeModifiedPPD();
+      }
     }
   }
 
@@ -584,24 +618,22 @@ class printgeneric extends plugin
   /* Save data to object */
   function save_object()
   {
+    /* Create a base backup and reset the
+       base directly after calling plugin::save_object();
+       Base will be set seperatly a few lines below */
+    $base_tmp = $this->base;
     plugin::save_object();
-    $this->netConfigDNS->save_object();
-
-    /* Save base, since this is no LDAP attribute */
-    if((isset($_POST['base'])) && ($this->acl_is_moveable()) ) {
-      $this->set_acl_base('dummy,'.$_POST['base']);
-      if($this->acl_is_moveable()){
-
-        if(isset($this->config->idepartments[$_POST['base']])){
-          $this->base = $_POST['base'];
-          if ($_POST['base'] != $this->base){
-            $this->is_modified= TRUE;
-          }
-        }
-      }else{
+    $this->base = $base_tmp;
 
-        print_red(sprintf(_("You are not allowed to move this object to '%s'."),LDAP::fix($_POST['base'])));
-        $this->set_acl_base('dummy,'.$this->base);
+    if(is_object($this->netConfigDNS)){
+      $this->netConfigDNS->save_object();
+    }
+    
+    /* Set new base if allowed */
+    $tmp = $this->get_allowed_bases();
+    if(isset($_POST['base'])){
+      if(isset($tmp[$_POST['base']])){
+        $this->base= $_POST['base'];
       }
     }
   }
@@ -611,7 +643,7 @@ class printgeneric extends plugin
   {
     /* Call common method to give check the hook */
     $message= plugin::check();
-    if ($this->BelongsTo == 'printer'){
+    if (preg_match("/printer/i",$this->BelongsTo)){
       $message= array_merge($message, $this->netConfigDNS->check());
     }
 
@@ -629,23 +661,17 @@ class printgeneric extends plugin
     $dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
 
     /* must: cn */
-    if(($this->BelongsTo == "Printer") && (empty($this->cn))){
+    if(preg_match("/printer/i",$this->BelongsTo) && (empty($this->cn))){
       $message[]= "The required field 'Printer name' is not set.";
     }
 
-    if($this->BelongsTo == "Printer"){
-      if ($this->orig_dn == "new" && !$this->acl_is_createable()){
-        $message[]= _("You have no permissions to create a group on this 'Base'.");
-      }
-    }
-    
     /* must: labeledURI */
     if(empty($this->labeledURI)){
       $message[]= "The required field 'Printer URL' is not set.";
     }
     
     /* Check if there is already an entry with this cn*/
-    if (($this->orig_dn != $dn)&&($this->BelongsTo == "Printer")){
+    if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
       $ldap= $this->config->get_ldap_link();
       $ldap->cd ($this->base);
       $ldap->ls("(cn=".$this->cn.")","ou=printers,ou=systems,".$this->base, array("cn"));
@@ -668,11 +694,11 @@ class printgeneric extends plugin
   {
     /* Update dn, to ensure storing as printer instead of WS / terminal
      */
-    if($this->BelongsTo == "Terminal"){
+    if(preg_match("/terminal/i",$this->BelongsTo)){
       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
     }
 
-    if($this->BelongsTo == "Workstation"){
+    if(preg_match("/workstation/i",$this->BelongsTo)){
       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
     }
     
@@ -688,7 +714,7 @@ class printgeneric extends plugin
     }
 
     /* If type is still unknown, the initialisation of this printer failed, abort. */
-    if($this->BelongsTo == "unknown"){
+    if(preg_match("/unknown/i",$this->BelongsTo)){
       return;
     }
 
@@ -697,6 +723,27 @@ class printgeneric extends plugin
       $this->PPDdialogToSave->save_ppd();
     }
 
+    /* Remove previously selected ppd file.*/
+    if($this->initial_PPD != $this->gotoPrinterPPD){
+      if(!empty($this->initial_PPD)){
+        $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
+        $tmp->removeModifiedPPD();
+      }
+    }
+
+    if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
+      $method="https://";
+    }else{
+      $method="http://";
+    }
+
+    /* If no ppd is selected, remove this attribute */
+    if(!empty($this->gotoPrinterPPD)) {
+      $this->gotoPrinterPPD = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
+    }else{
+      $this->gotoPrinterPPD = array();
+    }
+
     $dn= $this->dn;
     plugin::save();
     $ldap= $this->config->get_ldap_link();
@@ -716,19 +763,6 @@ class printgeneric extends plugin
       $this->attrs= $attrs;
     }
 
-    if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
-      $method="https://";
-    }else{
-      $method="http://";
-    }
-
-    /* If no ppd is selected, remove this attribute */
-    if(!empty($this->gotoPrinterPPD)) {
-      $this->attrs['gotoPrinterPPD'] = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
-    }else{
-      $this->attrs['gotoPrinterPPD'] = array();
-    }
-
     /* Append printer user 
      */
     $this->attrs['gotoUserPrinter']=array();
@@ -757,16 +791,21 @@ class printgeneric extends plugin
       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
     }
 
-    if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
-      $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
-    }
-
     if($this->orig_dn == 'new'){
       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
           unset($this->attrs[$checkVar]);
         }
       }
+    }else{
+      if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
+        $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
+      }
+    }
+
+    /* Ensure to create a new object */
+    if(preg_match("/ou=incoming,/",$this->orig_dn)){
+      $this->orig_dn = "new";
     }
 
     /* Move object in necessary*/
@@ -781,17 +820,27 @@ class printgeneric extends plugin
       $ldap->cd($this->config->current['BASE']);
       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
       $ldap->cd($this->dn);
+
+      /* Remove empty values */ 
+      foreach($this->attrs as $name => $value){
+        if(empty($value)){
+          unset($this->attrs[$name]);
+        }
+      }
+
       $ldap->add($this->attrs);
       $this->handle_post_events("add");
+      @log::log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
     } else {
       $ldap->cd($this->dn);
       $this->cleanup();
       $ldap->modify ($this->attrs); 
       $this->handle_post_events("modify");
+      @log::log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
     }
     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system print/generic with dn '%s' failed."),$this->dn));
 
-    if($this->BelongsTo == "Printer"){
+    if(preg_match("/printer/i",$this->BelongsTo)){
       $this->netConfigDNS->cn = $this->cn;
       $this->netConfigDNS->dn = $this->dn;
       $this->netConfigDNS->save($this->dn);
@@ -806,6 +855,7 @@ class printgeneric extends plugin
 
   function generateList(){
     $a_return=array();
+
     foreach($this->member as $type => $values){
       $a_return[$type]=array();
       foreach($values as $value){
@@ -832,10 +882,10 @@ class printgeneric extends plugin
           "plDescription" => _("Print generic"),
           "plSelfModify"  => FALSE,
           "plDepends"     => array(),
-          "plPriority"    => 0,
+          "plPriority"    => 4,
           "plSection"     => array("administration"),
           "plCategory"    => array("printer" => array("description"  => _("Printer"),
-                                                    "objectClass"  => "gotoPrinter"),"workstation"),
+                                                    "objectClass"  => "gotoPrinter"),"workstation","terminal"),
           "plProvidedAcls"=> array(
             "cn"                => _("Name"),
             "base"                => _("Base") ,         
@@ -894,7 +944,7 @@ class printgeneric extends plugin
 
     $ldap = $this->config->get_ldap_link();
     $ldap->cd($dn);
-    $ldap->cat($dn,array($var));
+    $ldap->cat($dn,array($var,"cn"));
     if($ldap->count()){
 
       $attrs = $ldap->fetch();