1 <?php
3 class ppdManager
4 {
5 var $path= "";
6 var $cachedList= array();
8 function ppdManager($path)
9 {
10 $this->path= $path;
11 }
14 function findPPD($path)
15 {
16 $list= array();
17 $currentDir= getcwd();
19 $dh = opendir($path);
20 while(false !== ($file = readdir($dh))){
22 /* Skip well known files */
23 if( $file == '.' || $file == '..'){
24 continue;
25 }
27 /* Recurse through all "common" directories */
28 if(is_dir($path.'/'.$file)){
29 $list= array_merge($list, $this->findPPD($path.'/'.$file));
30 continue;
31 }
33 /* Check for PPD extension */
34 if (preg_match('/\.ppd$/i', $file)){
35 $list[]= $path.'/'.$file;
36 }
37 }
39 closedir($dh);
40 chdir ($currentDir);
41 return ($list);
42 }
45 function updateAttribute($file, $section, $attribute, $value)
46 {
47 $fsection= false;
48 $fattribute= false;
49 $section= preg_replace('/^\*/', '', $section);
50 $attribute= preg_replace('/^\*/', '', $attribute);
52 $rp= fopen($file, "r");
53 $wp= fopen("$file.tmp", "w");
55 while (!feof($rp)){
56 $line= fgets($rp, 1024);
58 if (preg_match("/\*OpenGroup:*\s+\**$section\/*/", $line)){
59 $fsection= true;
60 continue;
61 }
63 if ($fsection){
64 if (preg_match("/^\*CloseGroup:*\s+\**$section\/*/", $line)){
65 $fsection= false;
66 fwrite ($wp, $line);
67 continue;
68 }
70 if (preg_match("/^\*OpenUI:*\s+\**$attribute\/*/", $line)){
71 $fattribute= true;
72 fwrite ($wp, $line);
73 continue;
74 }
76 if ($fattribute){
77 if (preg_match("/^\*CloseUI:*\s+\**$attribute\/*/", $line)){
78 $fattribute= false;
79 fwrite ($wp, $line);
80 continue;
81 }
83 if (preg_match("/^\*Default$attribute:*\s+/", $line)){
84 $line= "*Default$attribute: $value\n";
85 }
86 }
87 }
88 fwrite ($wp, $line);
89 }
91 fclose($wp);
92 fclose($rp);
94 echo "Move $file.tmp to $file<br>";
95 }
98 function saveProperties($ppdFile, $properties)
99 {
100 foreach ($properties as $name => $section){
101 foreach ($section as $attribute => $value){
102 if (is_array($value)){
103 $this->updateAttribute($ppdFile, $name, $attribute, $value['_default']);
104 }
105 }
106 }
107 }
110 function loadProperties($ppdFile)
111 {
112 $group= "";
113 $option= "";
114 $properties= array();
116 $fh= fopen ($ppdFile, 'r');
117 while (!feof($fh)) {
119 /* Read line */
120 $line= fgets($fh, 256);
121 if (strlen($line) >= 256){
122 trigger_error(_('Parsing PPD file %s failed - line too long. Trailing characters have been ignored'), E_USER_WARNING);
123 }
125 /* Trigger for option groups */
126 if (preg_match('/^\*OpenGroup:/i', $line)){
128 /* Sanity checks */
129 if ($group != ""){
130 trigger_error(_('Nested groups are not supported!'), E_USER_WARNING);
131 continue;
132 }
133 if (in_array($group, $properties)){
134 trigger_error(_('Group name not unique!'), E_USER_WARNING);
135 continue;
136 }
138 // TODO: Symbol values are not supported yet!
139 if (preg_match('/\^/', $line)){
140 trigger_error(_('Symbol values are not supported yet!'), E_USER_WARNING);
141 }
142 $complete= preg_replace('@^\*OpenGroup:\s+(.*)$@i', '\1', $line);
143 $complete= trim($complete, '"');
144 if (preg_match('@/@', $complete)){
145 $group= trim(preg_replace('@^\*OpenGroup:\s+"?([^/]+)/.*$@i', '\1', $line));
146 $name = preg_replace('@^\*OpenGroup:\s+"?[^/]+/([^/]+).*$@i', '\1', $line);
147 } else {
148 $group= $complete;
149 $name = $complete;
150 }
151 $properties[$group]= array('_name' => $name);
152 continue;
153 }
154 if (preg_match("/^\*CloseGroup:\s+\"?$group\"?/i", $line)){
155 $group= "";
156 continue;
157 }
159 /* Trigger for options */
160 if (preg_match('/^\*OpenUI\s+/i', $line)){
162 /* Sanity check */
163 if ($option != ""){
164 trigger_error(_('Nested options are not supported!'), E_USER_WARNING);
165 continue;
166 }
168 // TODO: Symbol values are not supported yet!
169 if (preg_match('/\^/', $line)){
170 trigger_error(_('Symbol values are not supported yet!'), E_USER_WARNING);
171 }
172 $complete= preg_replace('@^\*OpenUI\s+(.*)$@i', '\1', $line);
173 $complete= trim($complete, '"');
174 if (preg_match('@/@', $complete)){
175 $option= trim(preg_replace('@^\*OpenUI\s+([^/]+)/.*$@i', '\1', $line));
176 $name = trim(preg_replace('@^\*OpenUI\s+[^/]+/([^/]+).*$@i', '\1', $line));
177 } else {
178 $option= trim($complete);
179 $name = trim($complete);
180 }
182 /* Extract option type */
183 $type= trim(preg_replace('/^[^:]+:\s+/', '', $line));
184 $name= preg_replace('/:.*$/', '', $name);
185 $option= preg_replace('/:.*$/', '', $option);
187 // TODO: PickMany is not supported yet!
188 if (preg_match('/PickMany/i', $type)){
189 trigger_error(_('PickMany is not supported yet!'), E_USER_WARNING);
190 }
192 $properties[$group][$option]= array('_name' => $name, '_type' => $type);
193 continue;
194 }
195 /* Show interest for option parsing */
196 if ($option != ""){
198 $eoption= preg_replace('@\*@', '', $option);
200 /* Close section? */
201 if (preg_match("@^\*CloseUI:\s+\*$eoption@i", $line)){
202 $option= "";
203 continue;
204 }
206 /* Default value? */
207 if (preg_match("@^\*Default$eoption:@", $line)){
208 $c= preg_replace("@^\*Default$eoption:\s+@", "", $line);
209 $properties[$group][$option]['_default']= trim(trim($c, '"'));
210 continue;
211 }
213 /* Possible value? */
214 if (preg_match("@^\*$eoption\s+@", $line)){
215 #*PageSize Letter/US Letter: "<>setpagedevice"
216 $c= preg_replace("@^\*$eoption\s+([^/]+).*$@", "$1", $line);
217 $d= preg_replace("@^\*$eoption\s+[^/]+/([^:]+).*$@", "$1", $line);
218 $properties[$group][$option][trim($c)]= trim($d);
219 continue;
220 }
221 }
223 }
224 fclose ($fh);
226 return ($properties);
227 }
229 function loadDescription($ppdFile)
230 {
231 $model= "";
232 $manufacturer= "";
234 $fh= fopen ($ppdFile, 'r');
235 while (!feof($fh)) {
237 /* Read line */
238 $line= fgets($fh, 256);
239 if (strlen($line) >= 256){
240 trigger_error(_('Parsing PPD file %s failed - line too long. Trailing characters have been ignored'), E_USER_WARNING);
241 }
243 /* Extract interesting informations */
244 if (preg_match('/^\*Manufacturer:/i', $line)){
245 $manufacturer= preg_replace('/^\*Manufacturer:\s+"?([^"]+)"?.*$/i', '\1', $line);
246 }
247 if (preg_match('/^\*ModelName:/i', $line)){
248 $model= preg_replace('/^\*ModelName:\s+"?([^"]+)"?.*$/i', '\1', $line);
249 }
251 /* Got everything we need? Skip rest for speed reasons... */
252 if ($model != '' && $manufacturer != ''){
253 break;
254 }
255 }
256 fclose ($fh);
258 /* Write out a notice that the PPD file seems to be broken if we can't
259 extract any usefull informations */
260 if ($model == '' || $manufacturer == ''){
261 trigger_error(sprintf(_('Parsing PPD file %s failed - no information found.'), $ppdFile), E_USER_WARNING);
262 }
264 return ($manufacturer.' - '.$model);
265 }
268 function getPrinterList($reload= false)
269 {
270 /* Load list of PPD files */
271 if (count($this->cachedList) == 0 || $reload){
272 $list= $this->findPPD($this->path);
274 /* Load descriptive informations to build final printer list */
275 $this->cachedList= array();
276 foreach ($list as $ppdFile){
277 $this->cachedList[$ppdFile]= $this->loadDescription($ppdFile);
278 }
280 }
282 return ($this->cachedList);
283 }
285 }
287 ?>