1 <?php
3 class ppdManager
4 {
5 var $path= "";
6 var $cachedList= array();
8 function ppdManager($path)
9 {
10 $this->path= $path;
11 echo "Path set to $path<br>";
12 }
15 function findPPD($path)
16 {
17 $list= array();
18 $currentDir= getcwd();
20 $dh = opendir($path);
21 while(false !== ($file = readdir($dh))){
23 /* Skip well known files */
24 if( $file == '.' || $file == '..'){
25 continue;
26 }
28 /* Recurse through all "common" directories */
29 if(is_dir($path.'/'.$file)){
30 $list= array_merge($list, $this->findPPD($path.'/'.$file));
31 continue;
32 }
34 /* Check for PPD extension */
35 if (preg_match('/\.ppd$/i', $file)){
36 $list[]= $path.'/'.$file;
37 }
38 }
40 closedir($dh);
41 chdir ($currentDir);
42 return ($list);
43 }
46 function loadDescription($ppdFile)
47 {
48 $model= "";
49 $manufacturer= "";
51 $fh= fopen ($ppdFile, 'r');
52 while (!feof($fh)) {
54 /* Read line */
55 $line= fgets($fh, 4096);
56 if (strlen($line) >= 4095){
57 trigger_error(_('Parsing PPD file %s faild - line too long. Trailing characters have been ignored'), E_USER_WARNING);
58 }
60 /* Extract interesting informations */
61 if (preg_match('/^\*Manufacturer:/i', $line)){
62 $manufacturer= preg_replace('/^\*Manufacturer:\s+"([^"]+)".*$/i', '\1', $line);
63 }
64 if (preg_match('/^\*ModelName:/i', $line)){
65 $model= preg_replace('/^\*ModelName:\s+"([^"]+)".*$/i', '\1', $line);
66 }
68 /* Got everything we need? Skip rest for speed reasons... */
69 if ($model != '' && $manufacturer != ''){
70 break;
71 }
72 }
73 fclose ($fh);
75 /* Write out a notice that the PPD file seems to be broken if we can't
76 extract any usefull informations */
77 if ($model == '' || $manufacturer == ''){
78 trigger_error(sprintf(_('Parsing PPD file %s failed - no information found.'), $ppdFile), E_USER_WARNING);
79 }
81 return ($manufacturer.' - '.$model);
82 }
85 function getPrinterList($reload= false)
86 {
87 /* Load list of PPD files */
88 if (count($this->cachedList) == 0 || $reload){
89 $list= $this->findPPD($this->path);
91 /* Load descriptive informations to build final printer list */
92 $this->cachedList= array();
93 foreach ($list as $ppdFile){
94 $this->cachedList[$ppdFile]= $this->loadDescription($ppdFile);
95 }
97 }
99 return ($this->cachedList);
100 }
102 }
104 ?>