Code

Updated workstation - Startup
[gosa.git] / plugins / admin / systems / class_printerPPDDialog.inc
index d35eccbc965777bc1d6c2249b46d5ac209d493e9..a93e91bb4e56ad2a40d99cafcc41e6827d0e57c3 100644 (file)
@@ -25,9 +25,15 @@ class printerPPDDialog extends plugin
       
   /* Object Info */
   var $cn                   = "" ;          // Used to tag the ppds modified by the printer object, 
-  var $dialog               = NULL;         // Contains sub dialogs 
 
-  function printerPPDDialog ($config, $dn= NULL,$ppdfile=NULL )
+  /* If there is already a ppd file for the same type of printer, 
+   *  remember the path to ppd file and display a dialog which allows 
+   *  to overwrite the current ppd file. 
+   */
+  var $add_ppd_later            = "";
+  var $add_later_msg_dialog     = NULL;
+
+  function printerPPDDialog (&$config, $dn= NULL, $ppdfile=NULL )
   {
     plugin::plugin ($config, $dn);
     $this->depselect = $this->config->current['BASE'];
@@ -47,29 +53,15 @@ class printerPPDDialog extends plugin
     if(!((!is_dir($this->pathToPPD))||(empty($this->pathToPPD)))){ 
 
       /* Load all available PPD files and sort them into an array */
-      require_once ("class_ppdManager.inc");
       $this->ppdManager= new ppdManager($this->pathToPPD);
-      $tmp = $this->ppdManager->getPrinterList();
-
-      /* Sort all available files, and create header (Vendor index) */
-      foreach($tmp as $file=>$ppd){
-        $tmp2 = split("\n",$ppd);
-        if(!isset($this->ppdListHeader[$tmp2[0]])){
-          $this->ppdListHeader[$tmp2[0]]=0;
-        }
-        $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
-        $tmp3['link']   =preg_replace("/".str_replace("/","\/",$this->pathToPPD)."/i","",$file);
-        $tmp3['ppd']    =$ppd;
-        $this->ppdListHeader[$tmp2[0]]++;
-        $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
-      }
+      $this->getPrinterReload ();
 
       /* The user has already a valid PPD assigned
        * Get some informations about this PPD
        * and set it as selected. 
        * The ppdpath ['link'] should be relative from .../ppd/modified/ 
        *     e.g. "/Compaq/Compaq-J1200.ppd" */
-      if(($ppdfile!= NULL)&&(strlen($ppdfile)>0)){
+      if(($ppdfile!== NULL)&&(strlen($ppdfile)>0)){
         $ppdfile = preg_replace("#".$this->pathToModified."#","",$ppdfile);
         if(!file_exists($this->pathToPPD.$this->pathToModified.$ppdfile)){
           print_red(sprintf(_("Can't open '%s', ppd settings resetted."),$ppdfile));
@@ -89,7 +81,6 @@ class printerPPDDialog extends plugin
   }
 
 
-
   function execute()
   {
     /* Call parent execute */
@@ -109,15 +100,16 @@ class printerPPDDialog extends plugin
     
       /* Check if path is write/readable*/
       $is_r = @is_readable($path);  
-      if((!is_dir($path))||(empty($path)) || (!$is_r)){
-        print_red(sprintf(_("The specified path '%s' which results from PPD_PATH in your gosa.conf is invalid,".
-                            " can't read/write any ppd informations."),$path));
+      if(((!is_dir($path))||(empty($path)) || (!$is_r)) && (!@mkdir($path))){
+        print_red(sprintf(_("The specified path '%s' which results from PPD_PATH in your gosa.conf is invalid, can't read/write any ppd informations."),$path));
         /* Print out template */
         $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
         return($display);
       }
     }
 
+    // PPD selection / upload / dialog handling 
+
     /* Is there a new PPD file uploaded ? */
     if((isset($_FILES['NewPPDFile']))&&(isset($_POST['SubmitNewPPDFile']))){
       $file = ($_FILES['NewPPDFile']);
@@ -130,48 +122,42 @@ class printerPPDDialog extends plugin
       }
     }
 
+    /* Overwrite existing PPD file and select it as currently used for this object */
+    if(is_object($this->add_later_msg_dialog) && ($this->add_later_msg_dialog->is_confirmed()) && $this->add_ppd_later != ""){
+      if($name = $this->AddPPD($this->add_ppd_later,TRUE)){
+        $this->SelectPPD($name);
+      }
+      $this->add_ppd_later = "";
+      $this->add_later_msg_dialog = NULL;
+    } 
 
-    /* Open a dialog that allow us to select different PPDs
-     */
+    /* Open a dialog that allow us to select different PPDs */
     if(isset($_POST['SelectPPD'])){
       $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
     }
 
-    /* The selection dialog fpr PPDs is canceled
-     */
+    /* The selection dialog fpr PPDs is canceled */
     if(isset($_POST['ClosePPDSelection'])){
       unset($this->dialog);
-      $this->dialog=NULL;
-    }
-
-    /* A new PPDs was selected in the PPDs selection Dialog
-     * Perform a Check. If everything is fine, use the new PPD.
-     */
-    if(isset($_POST['SavePPDSelection'])){
-      if(!isset($_POST['PPDselection'])){
-        print_red(_("Please select a valid ppd."));
-      }else{
-        $this->SelectPPD($_POST['PPDselection']);
-        unset($this->dialog);
-        $this->dialog=NULL;
-      }
+      $this->dialog=FALSE;
     }
 
     /* Div Selection */ 
     if((isset($_GET['act']))&&($_GET['act']=="use")){
       $this->SelectPPD(base64_decode($_GET['id']));
       unset($this->dialog);
-      $this->dialog=NULL;
+      $this->dialog=FALSE;
 
     }
 
-    /* if a dialog is open, print the dialog instead of this class
-     */
-    if($this->dialog!=NULL){
+    /* if a dialog is open, print the dialog instead of this class */
+    if(is_object($this->dialog)){
       $display = $this->dialog->execute();
       return($display);
     }
 
+    // ENDE  PPD selection / upload / dialog handling 
+
     /* Give smarty the information it needs */
     $smarty->assign("ppdString" ,$this->getPPDInformation());
     $tmp= $this->generateProperties();
@@ -222,7 +208,7 @@ class printerPPDDialog extends plugin
       $Model  = trim(preg_replace("/".$Vendor."/","",$Name));
     }    
 
-    $PrinterName  = $this->cn."-".$Name; 
+    $PrinterName  = $this->cn."-".preg_replace("/[^a-z0-9-_\.]/i","_",$Name);
     $PPDName      = $Vendor."/".$PrinterName.".ppd";
     
     /* Create the vendors path, if it doesn't exists already */
@@ -256,7 +242,7 @@ class printerPPDDialog extends plugin
   /* This function adds a new ppd file to the list of available ppds. 
      All required paths and files will be created 
       $_PathOnHdd e.g. = /tmp/PHP_tmpfile213452 */
-  function AddPPD($_PathOnHdd)
+  function AddPPD($_PathOnHdd,$overwrite = FALSE)
   {
     /* Check if file exists && is readable */
     if((!is_file($_PathOnHdd)) || (!is_readable($_PathOnHdd))){
@@ -278,7 +264,7 @@ class printerPPDDialog extends plugin
 
     /* Check if parse was successfull */
     if(empty($name) || empty($vendor)){
-      print_red(sprintf(_("The given ppd file '%s' seams to be invalid, can't get any modell or vendor informations."),$_PathOnHdd));
+      print_red(sprintf(_("The given ppd file '%s' seams to be invalid, can't get any model or vendor informations."),$_PathOnHdd));
       return(false);
     }
 
@@ -287,13 +273,28 @@ class printerPPDDialog extends plugin
       $this->ppdList[$vendor] = array();
     }
 
+    /* Create ppd file and fill in the source contents */
+    $ppdname      = $vendor."/".$name.".ppd";
+    $filename     = $this->pathToPPD.preg_replace("/[^a-z0-9-_\.\/]/i","_",$ppdname);
+    $filename     = $this->pathToPPD.$ppdname;
+    $contents     = file_get_contents($_PathOnHdd);
+
+
     /* Check if this ppd already exists */
     $found = false;
     foreach($this->ppdList[$vendor] as $key => $val){
       if(preg_match("/".$model.".*/i",$key)){
         $found = true;
-        print_red(sprintf(_("There is already a ppd file for this kind of printer.")));
-        return;
+        if(!$overwrite){
+          if(!copy($_PathOnHdd,$_PathOnHdd."_back")){
+            print_red(sprintf(_("Can't add new ppd file, the source file '%s' is not accessible."),$_PathOnHdd));         
+          }else{
+            $this->add_ppd_later = $_PathOnHdd."_back";
+            $this->add_later_msg_dialog = new msg_dialog(_("Overwrite existing PPD"),
+                _("There is already a ppd file for this kind of printer. Do you want to overwrite it?"),CONFIRM_DIALOG);
+          }
+          return;
+        }
       }
     }
 
@@ -305,11 +306,8 @@ class printerPPDDialog extends plugin
       }
     }
   
-    /* Create ppd file and fill in the source contents */
-    $ppdname      = $vendor."/".$name.".ppd";
-    $filename     = $this->pathToPPD.$ppdname;
+    /* Open file handle */
     $fp           = fopen($filename,"w+");
-    $contents     = file_get_contents($_PathOnHdd);
 
     /* Check file handle & contents */
     if(!$fp){
@@ -340,24 +338,26 @@ class printerPPDDialog extends plugin
    */
   function getPrinterReload()
   {
-    $tmp = $this->ppdManager->getPrinterList(true);
+    if(is_readable($this->pathToPPD)){
+      $tmp = @$this->ppdManager->getPrinterList(true);
 
-    $this->ppdListHeader = $this->ppdList = array();
+      $this->ppdListHeader = $this->ppdList = array();
 
-    /* Sort all available files, and create header (Vendor index) */
-    foreach($tmp as $file=>$ppd){
+      /* Sort all available files, and create header (Vendor index) */
+      foreach($tmp as $file=>$ppd){
 
-      if(preg_match("#".$this->pathToModified."#",$file)) continue;
+        if(preg_match("#".$this->pathToModified."#",$file)) continue;
 
-      $tmp2 = split("\n",$ppd);
-      if(!isset($this->ppdListHeader[$tmp2[0]])){
-        $this->ppdListHeader[$tmp2[0]]=0;
+        $tmp2 = split("\n",$ppd);
+        if(!isset($this->ppdListHeader[$tmp2[0]])){
+          $this->ppdListHeader[$tmp2[0]]=0;
+        }
+        $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
+        $tmp3['link']   =$file;
+        $tmp3['ppd']    =$ppd;
+        $this->ppdListHeader[$tmp2[0]]++;
+        $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
       }
-      $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
-      $tmp3['link']   =$file;
-      $tmp3['ppd']    =$ppd;
-      $this->ppdListHeader[$tmp2[0]]++;
-      $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
     }
   }
 
@@ -380,12 +380,14 @@ class printerPPDDialog extends plugin
 
 
   /* Save modified ppd */
-  function save_ppd(){
+  function save_ppd()
+  {
     if($this->ppdManager){
       $this->ppdManager->saveProperties($this->pathToPPD.$this->pathToModified.$this->selectedPPD['link'],$this->ppdConfig);
     }
   }
 
+
   /* Return selected ppd path, if none is selected then false */
   function save()
   {
@@ -393,6 +395,7 @@ class printerPPDDialog extends plugin
     return($this->pathToModified.$this->selectedPPD['link']);
   }
 
+
   /* Get Information for a single PPD entry 
    * This will be shown on top of template
    */
@@ -405,6 +408,7 @@ class printerPPDDialog extends plugin
     return($str) ; 
   }
 
+
   /* Display all options from the selected ppd file */
   function generateProperties()
   { 
@@ -439,14 +443,19 @@ class printerPPDDialog extends plugin
         /* Add attributes of the current category */
         foreach($obj as $attr => $settings){
 
-
           /* Skip all entries beginning with _ */
           if($attr[0] == "_") continue;  
 
           /* Prepare data */
           $values   = array();
           $name     = $settings['_name'];
-          $default  = $settings['_default'];
+
+          if (!isset($settings['_default'])){
+            $default  = "";
+          } else {
+            $default  = $settings['_default'];
+          }
+
           $type     = $settings['_type'];
 
           /* Add name to table */ 
@@ -501,8 +510,37 @@ class printerPPDDialog extends plugin
     }
     return($str);
   }
+
+  function removeModifiedPPD()
+  {
+    $path = $this->pathToPPD.$this->pathToModified.$this->selectedPPD['link'];
+
+    if(file_exists($path)){
+      if(is_writeable($path)){
+        if(!@unlink($path)){
+          print_red(sprintf(_("Removing old ppd file '%s' failed."),$path));
+        }
+      }else{
+        print_red(sprintf(_("Removing old ppd file '%s' failed. File is not accessible."),$path));
+      }
+    }else{
+      print_red(sprintf(_("Removing old ppd file '%s' failed. File does not exists or is not accessible."),$path));
+    }
+  }
+
+  function update_ppd_url()
+  {
+    $this->SelectPPD("modified/".$this->selectedPPD['link']);
+  }
   
-  function check(){  return(plugin::check()); }
+  function check()
+  {  
+    $message = plugin::check();
+    if(empty($this->selectedPPD['link'])){
+      $message[] = _("Please select a valid ppd file or use 'Cancel' to go back to printer configuration.");
+    }
+    return($message); 
+  }
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>