1 <?php
3 class ppdManager
4 {
5 var $path= "";
6 var $cachedList= array();
7 var $timestamps = array();
9 function ppdManager($path)
10 {
11 if(is_dir($path)){
12 $this->path= $path;
13 }elseif(is_link($path) && (is_dir(readlink($path)))) {
14 $this->path= $path;
15 } else {
16 msg_dialog::display(_("PPD manager error"), sprintf(_("The specified path '%s' does not exist."),$path), ERROR_DIALOG);
17 return (false);
18 }
19 }
22 function findPPD($path)
23 {
24 $list= array();
25 $currentDir= getcwd();
27 $dh = opendir($path);
28 while(false !== ($file = readdir($dh))){
30 /* Skip well known files */
31 if( $file == '.' || $file == '..'){
32 continue;
33 }
35 /* Recurse through all "common" directories */
36 $check_path= $path.'/'.$file;
37 if(is_dir($check_path) || (is_link($check_path) && (is_dir(readlink($check_path))))){
38 $list= array_merge($list, $this->findPPD($check_path));
39 continue;
40 }
42 /* Check for PPD extension */
43 if (preg_match('/\.ppd(.gz)?$/i', $file)){
44 $list[]= $path.'/'.$file;
45 }
46 }
48 closedir($dh);
49 chdir ($currentDir);
50 return ($list);
51 }
54 function updateAttribute($file, $section, $attribute, $value)
55 {
56 $fsection= false;
57 $fattribute= false;
58 $section= preg_replace('/^\*/', '', $section);
59 $attribute= preg_replace('/^\*/', '', $attribute);
61 $rp= @gzopen($file, "r");
62 $wp= @gzopen("$file.tmp", "w");
66 while (!gzeof($rp)){
67 $lines[]= gzgets($rp, 1024);
68 }
70 $ret = "";
71 $done =false;
72 foreach($lines as $nr => $line){
74 if (preg_match("/\*OpenGroup:*\s+\**$section\/*/", $line)){
75 $fsection= true;
76 $ret .=$line;
77 continue;
78 }
80 /* Change model name .. */
81 if ((preg_match("/^\*".$attribute.":*\s+/",$line)) && ($attribute == "ModelName")){
82 $line= "*$attribute: \"$value\"\n";
83 $done =true;
84 }
86 if (($fsection) && ($section != "NO_SECTION")){
87 if (preg_match("/^\*CloseGroup:*\s+\**$section\/*/", $line)){
88 $fsection= false;
89 $ret .=$line;
90 continue;
91 }
94 if (preg_match("/^\*OpenUI:*\s+\**$attribute\/*/", $line)){
95 $fattribute= true;
96 $ret .= $line;
97 continue;
98 }
100 if ($fattribute){
101 if (preg_match("/^\*CloseUI:*\s+\**$attribute\/*/", $line)){
102 $fattribute= false;
103 $ret .= $line;
104 continue;
105 }
107 if (preg_match("/^\*Default$attribute:*\s+/", $line)){
108 $line= "*Default$attribute: $value\n";
109 $done =true;
110 }
111 }
112 }else{
113 if (preg_match("/^\*OpenUI:*\s+\**$attribute\/*/", $line)){
114 $fattribute= true;
115 $ret .= $line;
116 continue;
117 }
119 if ($fattribute){
120 if (preg_match("/^\*CloseUI:*\s+\**$attribute\/*/", $line)){
121 $fattribute= false;
122 $ret .= $line;
123 continue;
124 }
126 if (preg_match("/^\*Default$attribute:*\s+/", $line)){
127 $line= "*Default$attribute: $value\n";
128 $done =true;
129 }
130 }
131 }
132 $ret .=$line;
133 }
135 gzwrite($wp,$ret);
137 gzclose($wp);
138 gzclose($rp);
140 copy("$file.tmp", "$file");
141 unlink("$file.tmp");
142 }
145 function saveProperties($ppdFile, $properties)
146 {
147 if(!is_readable($ppdFile)){
148 msg_dialog::display(_("PPD manager error"), sprintf(_("Specified PPD file '%s' cannot be opened for reading."),$ppdFile), ERROR_DIALOG);
149 }elseif(!is_writeable(preg_replace("#(^.*/).*$#","\\1",$ppdFile.".tmp"))){
150 msg_dialog::display(_("PPD manager error"), sprintf(_("The temporary file '%s' cannot be opened for writing."),$ppdFile.".tmp"), ERROR_DIALOG);
151 }else{
152 if(is_array($properties)){
153 foreach ($properties as $name => $section){
154 foreach ($section as $attribute => $value){
155 if (is_array($value)){
156 $this->updateAttribute($ppdFile, $name, $attribute, $value['_default']);
157 }
158 }
159 }
160 }
161 }
162 }
164 function loadProperties($ppdFile)
165 {
166 $group= "";
167 $option= "";
168 $properties= array();
170 $fh= gzopen ($ppdFile, 'r');
171 while (!gzeof($fh)) {
173 /* Read line */
174 $line= gzgets($fh, 256);
175 if (strlen($line) >= 256){
176 trigger_error(_('Parsing PPD file %s failed - line too long. Trailing characters have been ignored'), E_USER_WARNING);
177 }
179 /* Trigger for option groups */
180 if (preg_match('/^\*OpenGroup:/i', $line)){
182 /* Sanity checks */
183 if ($group != ""){
184 trigger_error(_('Nested groups are not supported!'), E_USER_WARNING);
185 continue;
186 }
187 if (in_array($group, $properties)){
188 trigger_error(_('Group name not unique!'), E_USER_WARNING);
189 continue;
190 }
192 // TODO: Symbol values are not supported yet!
193 if (preg_match('/\^/', $line)){
194 trigger_error(_('Symbol values are not supported yet!'), E_USER_WARNING);
195 }
196 $complete= preg_replace('@^\*OpenGroup:\s+(.*)$@i', '\1', $line);
197 $complete= trim($complete, '"');
198 if (preg_match('@/@', $complete)){
199 $group= trim(preg_replace('@^\*OpenGroup:\s+"?([^/]+)/.*$@i', '\1', $line));
200 $name = preg_replace('@^\*OpenGroup:\s+"?[^/]+/([^/]+).*$@i', '\1', $line);
201 } else {
202 $group= $complete;
203 $name = $complete;
204 }
205 $properties[$group]= array('_name' => $name);
206 continue;
207 }
208 if (preg_match("/^\*CloseGroup:\s+\"?$group\"?/i", $line)){
209 $group= "";
210 continue;
211 }
213 /* Trigger for options */
214 if (preg_match('/^\*OpenUI\s+/i', $line)){
216 /* Sanity check */
217 if ($option != ""){
218 trigger_error(_('Nested options are not supported!'), E_USER_WARNING);
219 continue;
220 }
222 // TODO: Symbol values are not supported yet!
223 if (preg_match('/\^/', $line)){
224 trigger_error(_('Symbol values are not supported yet!'), E_USER_WARNING);
225 }
226 $complete= preg_replace('@^\*OpenUI\s+(.*)$@i', '\1', $line);
227 $complete= trim($complete, '"');
228 if (preg_match('@/@', $complete)){
229 $option= trim(preg_replace('@^\*OpenUI\s+([^/]+)/.*$@i', '\1', $line));
230 $name = trim(preg_replace('@^\*OpenUI\s+[^/]+/([^/]+).*$@i', '\1', $line));
231 } else {
232 $option= trim($complete);
233 $name = trim($complete);
234 }
236 /* Extract option type */
237 $type= trim(preg_replace('/^[^:]+:\s+/', '', $line));
238 $name= preg_replace('/:.*$/', '', $name);
239 $option= preg_replace('/:.*$/', '', $option);
241 // TODO: PickMany is not supported yet!
242 if (preg_match('/PickMany/i', $type)){
243 trigger_error(_('PickMany is not supported yet!'), E_USER_WARNING);
244 }
245 if(empty($group)){
246 $properties["NO_SECTION"][$option]= array('_name' => $name, '_type' => $type);
247 }else{
248 $properties[$group][$option]= array('_name' => $name, '_type' => $type);
249 }
250 continue;
251 }
252 /* Show interest for option parsing */
253 if ($option != ""){
255 $eoption= preg_replace('@\*@', '', $option);
257 /* Close section? */
258 if (preg_match("@^\*CloseUI:\s+\*$eoption@i", $line)){
259 $option= "";
260 continue;
261 }
263 /* Default value? */
264 if (preg_match("@^\*Default$eoption:@", $line)){
265 $c= preg_replace("@^\*Default$eoption:\s+@", "", $line);
266 if(empty($group)){
267 $properties["NO_SECTION"][$option]['_default']= trim(trim($c, '"'));
268 }else{
269 $properties[$group][$option]['_default']= trim(trim($c, '"'));
270 }
271 continue;
272 }
274 /* Possible value? */
275 if (preg_match("@^\*$eoption\s+@", $line)){
276 #*PageSize Letter/US Letter: "<>setpagedevice"
277 $c= preg_replace("@^\*$eoption\s+([^/]+).*$@", "$1", $line);
278 $d= preg_replace("@^\*$eoption\s+[^/]+/([^:]+).*$@", "$1", $line);
279 if(empty($group)){
280 $properties["NO_SECTION"][$option][trim($c)]= trim($d);
281 }else{
282 $properties[$group][$option][trim($c)]= trim($d);
283 }
284 continue;
285 }
286 }
287 }
288 gzclose ($fh);
289 return ($properties);
290 }
292 function loadDescription($ppdFile)
293 {
294 $ppdDesc = array();
296 /* Only parse complete PPD file again, if it was changed */
297 $modified = filemtime($ppdFile);
298 if(isset($this->cachedList[$ppdFile]) && isset($this->timestamps[$ppdFile]) && $modified == $this->timestamps[$ppdFile]){
299 return($this->cachedList[$ppdFile]);
300 }
302 /* Remember modified timestamp, to speed up next request */
303 $this->timestamps[$ppdFile] = filemtime($ppdFile);
305 $fh= gzopen ($ppdFile, 'r');
306 while ((!gzeof($fh))&&($fh)) {
308 /* Read line */
309 $line= gzgets($fh, 256);
310 if (strlen($line) >= 256){
311 trigger_error(_('Parsing PPD file %s failed - line too long. Trailing characters have been ignored'), E_USER_WARNING);
312 }
314 /* Extract interesting informations */
315 if (preg_match('/^\*Manufacturer:/i', $line)){
316 $ppdDesc['manufacturer'] = trim(preg_replace('/^\*Manufacturer:\s+"?([^"]+)"?.*$/i', '\1', $line));
317 }
318 if (preg_match('/^\*ModelName:/i', $line)){
319 $ppdDesc['model'] = trim(preg_replace('/^\*ModelName:\s+"?([^"]+)"?.*$/i', '\1', $line));
320 }
322 /* Got everything we need? Skip rest for speed reasons... */
323 if (isset($ppdDesc['manufacturer']) && isset($ppdDesc['model'])){
324 break;
325 }
326 }
327 gzclose ($fh);
329 /* If model contains manufacturer strip it */
330 $ppdDesc['model'] = str_replace($ppdDesc['manufacturer']." ", '', $ppdDesc['model']);
332 /* Write out a notice that the PPD file seems to be broken if we can't
333 extract any usefull informations */
334 if ($ppdDesc['manufacturer'] == '' || $ppdDesc['model'] == ''){
335 trigger_error(sprintf(_('Parsing PPD file %s failed - no information found.'), $ppdFile), E_USER_WARNING);
336 }
338 $ppdDesc['name'] = $ppdDesc['manufacturer'] . " - " . $ppdDesc['model'];
340 return $ppdDesc;
341 }
344 function getPrinterList($reload= false)
345 {
346 /* Load list of PPD files */
347 if (count($this->cachedList) == 0 || $reload){
348 $list= $this->findPPD($this->path);
350 /* Load descriptive informations to build final printer list */
351 $new = array();
352 foreach ($list as $ppdFile){
353 $new[$ppdFile] = $this->loadDescription($ppdFile);
354 }
355 $this->cachedList= $new ;
357 }
359 return ($this->cachedList);
360 }
362 }
363 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
364 ?>