Code

Added support for master-key encrypted passwords in gosa.conf
[gosa.git] / include / class_ppdManager.inc
index 1b09debc06fbfa04d3ca3f92f7cac2c85413d415..a9dfd1a7f995e5bb292b7985864ebac8984039c3 100644 (file)
@@ -4,6 +4,7 @@ class ppdManager
 {
        var $path= "";
        var $cachedList= array();
+       var $timestamps = array();
 
        function ppdManager($path)
        {
@@ -46,7 +47,7 @@ class ppdManager
                return ($list);
        }
 
-       
+
        function updateAttribute($file, $section, $attribute, $value)
        {
                $fsection= false;
@@ -54,46 +55,82 @@ class ppdManager
                $section= preg_replace('/^\*/', '', $section);
                $attribute= preg_replace('/^\*/', '', $attribute);
 
-               $rp= fopen($file, "r");
-               $wp= fopen("$file.tmp", "w");
+               $rp= @fopen($file, "r");
+               $wp= @fopen("$file.tmp", "w");
+
+               
 
                while (!feof($rp)){
-                       $line= fgets($rp, 1024);
+                       $lines[]= fgets($rp, 1024);
+               }
 
+               $ret = "";
+               $done =false;
+               foreach($lines as $nr => $line){
+       
                        if (preg_match("/\*OpenGroup:*\s+\**$section\/*/", $line)){
-                               $fsection= true;
-                               fwrite ($wp, $line);
-                               continue;
+                $fsection= true;
+                               $ret .=$line; 
+                continue;
+            }
+
+                       /* Change model name .. */
+                       if ((preg_match("/^\*".$attribute.":*\s+/",$line)) && ($attribute == "ModelName")){
+                               $line= "*$attribute: \"$value\"\n";
+                               $done =true;
                        }
 
-                       if ($fsection){
+                       if (($fsection) && ($section != "NO_SECTION")){
                                if (preg_match("/^\*CloseGroup:*\s+\**$section\/*/", $line)){
                                        $fsection= false;
-                                       fwrite ($wp, $line);
+                                       $ret .=$line; 
+                                       continue;
+                               }
+
+
+                               if (preg_match("/^\*OpenUI:*\s+\**$attribute\/*/", $line)){
+                                       $fattribute= true;
+                                       $ret .= $line; 
                                        continue;
                                }
 
+                               if ($fattribute){
+                                       if (preg_match("/^\*CloseUI:*\s+\**$attribute\/*/", $line)){
+                                               $fattribute= false;
+                                               $ret .= $line; 
+                                               continue;
+                                       }
+
+                                       if (preg_match("/^\*Default$attribute:*\s+/", $line)){
+                                               $line= "*Default$attribute: $value\n";
+                                               $done =true;
+                                       }
+                               }
+                       }else{
                                if (preg_match("/^\*OpenUI:*\s+\**$attribute\/*/", $line)){
                                        $fattribute= true;
-                                       fwrite ($wp, $line);
+                                       $ret .= $line; 
                                        continue;
                                }
 
                                if ($fattribute){
                                        if (preg_match("/^\*CloseUI:*\s+\**$attribute\/*/", $line)){
                                                $fattribute= false;
-                                               fwrite ($wp, $line);
+                                               $ret .= $line; 
                                                continue;
                                        }
 
                                        if (preg_match("/^\*Default$attribute:*\s+/", $line)){
                                                $line= "*Default$attribute: $value\n";
+                                               $done =true;
                                        }
                                }
                        }
-                       fwrite ($wp, $line);
+                       $ret .=$line; 
                }
 
+               fwrite($wp,$ret);
+               
                fclose($wp);
                fclose($rp);
 
@@ -104,22 +141,27 @@ class ppdManager
 
        function saveProperties($ppdFile, $properties)
        {
-               foreach ($properties as $name => $section){
-                       foreach ($section as $attribute => $value){
-                               if (is_array($value)){
-                                       $this->updateAttribute($ppdFile, $name, $attribute, $value['_default']);
+               if(!is_readable($ppdFile)){
+                       print_red(sprintf(_("Specified ppd file '%s' can't be opened for reading."),$ppdFile));
+               }elseif(!is_writeable(preg_replace("#(^.*/).*$#","\\1",$ppdFile.".tmp"))){
+                       print_red(sprintf(_("The required tmp file file '%s' can't be opened for writing."),$ppdFile.".tmp"));
+               }else{
+                       foreach ($properties as $name => $section){
+                               foreach ($section as $attribute => $value){
+                                       if (is_array($value)){
+                                               $this->updateAttribute($ppdFile, $name, $attribute, $value['_default']);
+                                       }
                                }
                        }
                }
        }
-       
-       
+
        function loadProperties($ppdFile)
        {
                $group= "";
                $option= "";
                $properties= array();
-               
+
                $fh= fopen ($ppdFile, 'r');
                while (!feof($fh)) {
 
@@ -141,7 +183,7 @@ class ppdManager
                                        trigger_error(_('Group name not unique!'), E_USER_WARNING);
                                        continue;
                                }
-                               
+
                                // TODO: Symbol values are not supported yet!
                                if (preg_match('/\^/', $line)){
                                        trigger_error(_('Symbol values are not supported yet!'), E_USER_WARNING);
@@ -171,7 +213,7 @@ class ppdManager
                                        trigger_error(_('Nested options are not supported!'), E_USER_WARNING);
                                        continue;
                                }
-                               
+
                                // TODO: Symbol values are not supported yet!
                                if (preg_match('/\^/', $line)){
                                        trigger_error(_('Symbol values are not supported yet!'), E_USER_WARNING);
@@ -190,13 +232,16 @@ class ppdManager
                                $type= trim(preg_replace('/^[^:]+:\s+/', '', $line));
                                $name= preg_replace('/:.*$/', '', $name);
                                $option= preg_replace('/:.*$/', '', $option);
-                               
+
                                // TODO: PickMany is not supported yet!
                                if (preg_match('/PickMany/i', $type)){
                                        trigger_error(_('PickMany is not supported yet!'), E_USER_WARNING);
                                }
-                               
-                               $properties[$group][$option]= array('_name' => $name, '_type' => $type);
+                               if(empty($group)){
+                                       $properties["NO_SECTION"][$option]= array('_name' => $name, '_type' => $type);
+                               }else{
+                                       $properties[$group][$option]= array('_name' => $name, '_type' => $type);
+                               }
                                continue;
                        }
                        /* Show interest for option parsing */
@@ -213,31 +258,47 @@ class ppdManager
                                /* Default value? */
                                if (preg_match("@^\*Default$eoption:@", $line)){
                                        $c= preg_replace("@^\*Default$eoption:\s+@", "", $line);
-                                       $properties[$group][$option]['_default']= trim(trim($c, '"'));
+                                       if(empty($group)){
+                                               $properties["NO_SECTION"][$option]['_default']= trim(trim($c, '"'));
+                                       }else{
+                                               $properties[$group][$option]['_default']= trim(trim($c, '"'));
+                                       }
                                        continue;
                                }
 
                                /* Possible value? */
                                if (preg_match("@^\*$eoption\s+@", $line)){
-                               #*PageSize Letter/US Letter: "<>setpagedevice"
+#*PageSize Letter/US Letter: "<>setpagedevice"
                                        $c= preg_replace("@^\*$eoption\s+([^/]+).*$@", "$1", $line);
                                        $d= preg_replace("@^\*$eoption\s+[^/]+/([^:]+).*$@", "$1", $line);
-                                       $properties[$group][$option][trim($c)]= trim($d);
+                                       if(empty($group)){
+                                               $properties["NO_SECTION"][$option][trim($c)]= trim($d);
+                                       }else{
+                                               $properties[$group][$option][trim($c)]= trim($d);
+                                       }
                                        continue;
                                }
                        }
-
                }
                fclose ($fh);
-
                return ($properties);
        }
-       
+
        function loadDescription($ppdFile)
        {
                $model= "";
                $manufacturer= "";
+
                
+               /* Only parse complete PPD file again, if it was changed */
+               $modified = filemtime($ppdFile);
+               if(isset($this->cachedList[$ppdFile]) && isset($this->timestamps[$ppdFile]) && $modified == $this->timestamps[$ppdFile]){
+                       return($this->cachedList[$ppdFile]);
+               }
+
+               /* Remember modified timestamp, to speed up next request */
+               $this->timestamps[$ppdFile] = filemtime($ppdFile);
+
                $fh= fopen ($ppdFile, 'r');
                while ((!feof($fh))&&($fh)) {
 
@@ -246,7 +307,7 @@ class ppdManager
                        if (strlen($line) >= 256){
                                trigger_error(_('Parsing PPD file %s failed - line too long. Trailing characters have been ignored'), E_USER_WARNING);
                        }
-                       
+
                        /* Extract interesting informations */
                        if (preg_match('/^\*Manufacturer:/i', $line)){
                                $manufacturer= preg_replace('/^\*Manufacturer:\s+"?([^"]+)"?.*$/i', '\1', $line);
@@ -279,16 +340,17 @@ class ppdManager
                        $list= $this->findPPD($this->path);
 
                        /* Load descriptive informations to build final printer list */
-                       $this->cachedList= array();
+                       $new = array();
                        foreach ($list as $ppdFile){
-                               $this->cachedList[$ppdFile]= $this->loadDescription($ppdFile);
+                               $new[$ppdFile] = $this->loadDescription($ppdFile); 
                        }
+                       $this->cachedList= $new ;
 
                }
 
                return ($this->cachedList);
        }
-       
+
 }
 
 ?>