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 if(!(filesize($ppdFile))) {
167 trigger_error(_('Parsing PPD file failed - File has 0 bytes'));
168 }
170 $group= "";
171 $option= "";
172 $properties= array();
174 $fh= gzopen ($ppdFile, 'r');
175 while (!gzeof($fh)) {
177 /* Read line */
178 $line= gzgets($fh, 256);
179 if (strlen($line) >= 256){
180 trigger_error(_('Parsing PPD file %s failed - line too long. Trailing characters have been ignored'), E_USER_WARNING);
181 }
183 /* Trigger for option groups */
184 if (preg_match('/^\*OpenGroup:/i', $line)){
186 /* Sanity checks */
187 if ($group != ""){
188 trigger_error(_('Nested groups are not supported!'), E_USER_WARNING);
189 continue;
190 }
191 if (in_array($group, $properties)){
192 trigger_error(_('Group name not unique!'), E_USER_WARNING);
193 continue;
194 }
196 // TODO: Symbol values are not supported yet!
197 if (preg_match('/\^/', $line)){
198 trigger_error(_('Symbol values are not supported yet!'), E_USER_WARNING);
199 }
200 $complete= preg_replace('@^\*OpenGroup:\s+(.*)$@i', '\1', $line);
201 $complete= trim($complete, '"');
202 if (preg_match('@/@', $complete)){
203 $group= trim(preg_replace('@^\*OpenGroup:\s+"?([^/]+)/.*$@i', '\1', $line));
204 $name = preg_replace('@^\*OpenGroup:\s+"?[^/]+/([^/]+).*$@i', '\1', $line);
205 } else {
206 $group= $complete;
207 $name = $complete;
208 }
209 $properties[$group]= array('_name' => $name);
210 continue;
211 }
212 if (preg_match("/^\*CloseGroup:\s+\"?$group\"?/i", $line)){
213 $group= "";
214 continue;
215 }
217 /* Trigger for options */
218 if (preg_match('/^\*OpenUI\s+/i', $line)){
220 /* Sanity check */
221 if ($option != ""){
222 trigger_error(_('Nested options are not supported!'), E_USER_WARNING);
223 continue;
224 }
226 // TODO: Symbol values are not supported yet!
227 if (preg_match('/\^/', $line)){
228 trigger_error(_('Symbol values are not supported yet!'), E_USER_WARNING);
229 }
230 $complete= preg_replace('@^\*OpenUI\s+(.*)$@i', '\1', $line);
231 $complete= trim($complete, '"');
232 if (preg_match('@/@', $complete)){
233 $option= trim(preg_replace('@^\*OpenUI\s+([^/]+)/.*$@i', '\1', $line));
234 $name = trim(preg_replace('@^\*OpenUI\s+[^/]+/([^/]+).*$@i', '\1', $line));
235 } else {
236 $option= trim($complete);
237 $name = trim($complete);
238 }
240 /* Extract option type */
241 $type= trim(preg_replace('/^[^:]+:\s+/', '', $line));
242 $name= preg_replace('/:.*$/', '', $name);
243 $option= preg_replace('/:.*$/', '', $option);
245 // TODO: PickMany is not supported yet!
246 if (preg_match('/PickMany/i', $type)){
247 trigger_error(_('PickMany is not supported yet!'), E_USER_WARNING);
248 }
249 if(empty($group)){
250 $properties["NO_SECTION"][$option]= array('_name' => $name, '_type' => $type);
251 }else{
252 $properties[$group][$option]= array('_name' => $name, '_type' => $type);
253 }
254 continue;
255 }
256 /* Show interest for option parsing */
257 if ($option != ""){
259 $eoption= preg_replace('@\*@', '', $option);
261 /* Close section? */
262 if (preg_match("@^\*CloseUI:\s+\*$eoption@i", $line)){
263 $option= "";
264 continue;
265 }
267 /* Default value? */
268 if (preg_match("@^\*Default$eoption:@", $line)){
269 $c= preg_replace("@^\*Default$eoption:\s+@", "", $line);
270 if(empty($group)){
271 $properties["NO_SECTION"][$option]['_default']= trim(trim($c, '"'));
272 }else{
273 $properties[$group][$option]['_default']= trim(trim($c, '"'));
274 }
275 continue;
276 }
278 /* Possible value? */
279 if (preg_match("@^\*$eoption\s+@", $line)){
280 #*PageSize Letter/US Letter: "<>setpagedevice"
281 $c= preg_replace("@^\*$eoption\s+([^/]+).*$@", "$1", $line);
282 $d= preg_replace("@^\*$eoption\s+[^/]+/([^:]+).*$@", "$1", $line);
283 if(empty($group)){
284 $properties["NO_SECTION"][$option][trim($c)]= trim($d);
285 }else{
286 $properties[$group][$option][trim($c)]= trim($d);
287 }
288 continue;
289 }
290 }
291 }
292 gzclose ($fh);
293 return ($properties);
294 }
296 function loadDescription($ppdFile)
297 {
298 if(!(filesize($ppdFile))) {
299 trigger_error(_('Parsing PPD file failed - File has 0 bytes'));
300 }
302 $ppdDesc = array();
304 /* Only parse complete PPD file again, if it was changed */
305 $modified = filemtime($ppdFile);
306 if(isset($this->cachedList[$ppdFile]) && isset($this->timestamps[$ppdFile]) && $modified == $this->timestamps[$ppdFile]){
307 return($this->cachedList[$ppdFile]);
308 }
310 /* Remember modified timestamp, to speed up next request */
311 $this->timestamps[$ppdFile] = filemtime($ppdFile);
313 $fh= gzopen ($ppdFile, 'r');
314 while ((!gzeof($fh))&&($fh)) {
316 /* Read line */
317 $line= gzgets($fh, 256);
318 if (strlen($line) >= 256){
319 trigger_error(_('Parsing PPD file %s failed - line too long. Trailing characters have been ignored'), E_USER_WARNING);
320 }
322 /* Extract interesting informations */
323 if (preg_match('/^\*Manufacturer:/i', $line)){
324 $ppdDesc['manufacturer'] = trim(preg_replace('/^\*Manufacturer:\s+"?([^"]+)"?.*$/i', '\1', $line));
325 }
326 if (preg_match('/^\*ModelName:/i', $line)){
327 $ppdDesc['model'] = trim(preg_replace('/^\*ModelName:\s+"?([^"]+)"?.*$/i', '\1', $line));
328 }
330 /* Got everything we need? Skip rest for speed reasons... */
331 if (isset($ppdDesc['manufacturer']) && isset($ppdDesc['model'])){
332 break;
333 }
334 }
335 gzclose ($fh);
337 /* If model contains manufacturer strip it */
338 $ppdDesc['model'] = str_replace($ppdDesc['manufacturer']." ", '', $ppdDesc['model']);
340 /* Write out a notice that the PPD file seems to be broken if we can't
341 extract any usefull informations */
342 if ($ppdDesc['manufacturer'] == '' || $ppdDesc['model'] == ''){
343 trigger_error(sprintf(_('Parsing PPD file %s failed - no information found.'), $ppdFile), E_USER_WARNING);
344 }
346 $ppdDesc['name'] = $ppdDesc['manufacturer'] . " - " . $ppdDesc['model'];
348 return $ppdDesc;
349 }
352 function getPrinterList($reload= false)
353 {
354 /* Load list of PPD files */
355 if (count($this->cachedList) == 0 || $reload){
356 $list= $this->findPPD($this->path);
358 /* Load descriptive informations to build final printer list */
359 $new = array();
360 foreach ($list as $ppdFile){
361 $new[$ppdFile] = $this->loadDescription($ppdFile);
362 }
363 $this->cachedList= $new ;
365 }
367 return ($this->cachedList);
368 }
370 }
371 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
372 ?>