Code

Removed path from ppd entry in ldap
[gosa.git] / plugins / admin / systems / class_printerPPDDialog.inc
1 <?php
3 class printerPPDDialog extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary          = "Manage server basic objects";
7   var $cli_description      = "Some longer text\nfor help";
8   var $cli_parameters       = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account       = TRUE;
12   var $attributes           = array();
13   var $objectclasses        = array("whatever");
15   var $ppdList              = array();  // Contains all Printer models
16   var $ppdListHeader        = array();  // Contains all printer vendors
18   var $dialog               = NULL;     
19   var $selectedPPD          = false;    // e.g. /var/spool/ppd/vendor/device.ppd
21   var $ppdManager           = false;    // new ppdManager;
22   var $ppdConfig            = false;    // $this->ppdManager->loadProperties($this->selectedPPD['link']);
24   var $pathToPPD            = "/var/spool/ppd/";
26   function printerPPDDialog ($config, $dn= NULL,$ppdfile=NULL )
27   {
28     plugin::plugin ($config, $dn);
29     $this->depselect = $this->config->current['BASE'];
31     /* Load all available PPD files and sort them into an array 
32      */
33     require_once ("class_ppdManager.inc");
34     $this->ppdManager= new ppdManager($this->pathToPPD);
35     $tmp = $this->ppdManager->getPrinterList();
37     /* Sort all available files, and create header (Vendor index) */
38     foreach($tmp as $file=>$ppd){
39       $tmp2 = split("\n",$ppd);
40       if(!isset($this->ppdListHeader[$tmp2[0]])){
41         $this->ppdListHeader[$tmp2[0]]=0;
42       }
43       $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
44       $tmp3['link']   =preg_replace("/".str_replace("/","\/",$this->pathToPPD)."/i","",$file);
45       $tmp3['ppd']    =$ppd;
46       $this->ppdListHeader[$tmp2[0]]++;
47       $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
48     }
50    /* The user has already a valid PPD assigned
51     * Get some informations about this PPD
52     */
53     if(($ppdfile!= NULL)&&(strlen($ppdfile)>0)){
54       if(!file_exists($this->pathToPPD.$ppdfile)){
55         print_red(sprintf(_("Can't open '%s', ppd settings resetted."),$ppdfile));
56       }else{
57         $tmp2= split("\n", $this->ppdManager->loadDescription($this->pathToPPD.$ppdfile));
58         $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]);
59         $tmp3['link']   =$ppdfile;
60         $tmp3['ppd']    =$this->ppdManager->loadDescription($this->pathToPPD.$ppdfile);
61         $this->selectedPPD = $tmp3;
62       }
63     }
64   }
66   function getPrinterReload()
67   {
68     $tmp = $this->ppdManager->getPrinterList(true);
70     /* Sort all available files, and create header (Vendor index) */
71     foreach($tmp as $file=>$ppd){
72       $tmp2 = split("\n",$ppd);
73       if(!isset($this->ppdListHeader[$tmp2[0]])){
74         $this->ppdListHeader[$tmp2[0]]=0;
75       }
76       $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
77       $tmp3['link']   =$file;
78       $tmp3['ppd']    =$ppd;
79       $this->ppdListHeader[$tmp2[0]]++;
80       $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
81     }
83   }
85   function execute()
86   {
87         /* Call parent execute */
88         plugin::execute();
90     /* Fill templating stuff */
91     $smarty= get_smarty();
92     $display= "";
94     /* Is there a new PPD file uploaded ? */
95     if((isset($_FILES['NewPPDFile']))&&(isset($_POST['SubmitNewPPDFile']))){
96       $file = ($_FILES['NewPPDFile']);
97       if($file['size'] != 0 ){
98         $res  = @$this->ppdManager->loadDescription($file['tmp_name']);
99         $name = (trim(preg_replace("/-/","",$res)));
100         if(!empty($name)){
101           $vendor = trim(preg_replace("/-.*$/i","",$res));
102           $model  = trim(preg_replace("/".$vendor."/","",$name));
103           if(!is_dir($this->pathToPPD.$vendor)){
104             if(!(@mkdir($this->pathToPPD.$vendor))){
105               print_red(sprintf(_("Can't create folder '%s' for the uploaded ppd file."),$vendor));
106             }
107           }
109           if(!isset($this->ppdList[$vendor])){
110             $this->ppdList[$vendor] = array();
111           }
112   
113           if(is_dir($this->pathToPPD.$vendor)){  
114             $found = false;
115             foreach($this->ppdList[$vendor] as $key => $val){
116               if(preg_match("/".$model.".*/i",$key)){
117                 $found = true;
118                 print_red(sprintf(_("There is already a ppd file for this kind of printer.")));
119               }
120             }// Foreach
121             if(!$found){
122               $filename = $this->pathToPPD.$vendor."/".$vendor."-".preg_replace("/^[^ ]/","",str_replace("-","",$model)).".ppd";
123               $fp = @fopen($filename,"w+");
124               if(!$fp){
125                 print_red(sprintf(_("Can't save file '%s'."),$filename));
126               }else{
127                 $str = file_get_contents($file['tmp_name']);
128                 fputs($fp,$str);
129                 @fclose($fp);
130                
131                 $tmp2= split("\n", $res);
132                 $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]);
133                 $tmp3['link']   =$filename;
134                 $tmp3['ppd']    =$res;
135                 $this->selectedPPD = $tmp3;
136                 $this->getPrinterReload(); 
137               }
138             }
139           }// If dir
140         }else{
141           print_red(_("Please specify a valid ppd file."));
142         }
143       }else{
144         print_red(_("Please specify a valid ppd file."));
145       }
146   
147     }
150     /* Open a dialog that allow us to select different PPDs
151      */
152     if(isset($_POST['SelectPPD'])){
153       $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
154     }
156     /* The selection dialog fpr PPDs is canceled
157      */
158     if(isset($_POST['ClosePPDSelection'])){
159       unset($this->dialog);
160       $this->dialog=NULL;
161     }
162   
163     /* A new PPDs was selected in the PPDs selection Dialog
164      * Perform a Check. If everything is fine, use the new PPD.
165      */
166     if(isset($_POST['SavePPDSelection'])){
167       if(!isset($_POST['PPDselection'])){
168         print_red(_("Please select a valid ppd."));
169       }else{
170         $this->selectedPPD['link'] = $_POST['PPDselection'];
171         $this->ppdConfig   = false;
172         unset($this->dialog);
173         $this->dialog=NULL;
174       }
175     }
176  
177     /* if a dialog is open, print the dialog instead of this class
178      */
179     if($this->dialog!=NULL){
180       $display = $this->dialog->execute();
181       return($display);
182     }
184     /* Give smarty the information it needs */
185     $smarty->assign("ppdString" ,$this->getPPDInformation());
186     $tmp= $this->generateProperties();
187     if ($tmp == ""){
188       $smarty->assign("showOptions", 0);
189     } else {
190       $smarty->assign("showOptions", 1);
191       $smarty->assign("properties",$this->generateProperties());
192     }
193   
194     /* Print out template */
195     $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
196     return($display);
197   }
199   function check(){
200     /* Check the given data
201      */
202     $message=array();
203     return $message;
204   }
206   function save_object()
207   {
208     if(isset($_POST['PPDDisSubmitted'])){
209       if(is_array($this->ppdConfig)){
210         foreach($this->ppdConfig as $cat => $obj){
211           foreach($obj as $attr => $attributes){
212             if(isset($_POST[base64_encode($attributes['_name'])])){
213               $this->ppdConfig[$cat][$attr]['_default'] = $_POST[base64_encode($attributes['_name'])];
214             }
215           }
216         }
217       }
218     }
220   }
221   
222   function save_ppd(){
223     $this->ppdManager->saveProperties($this->pathToPPD.$this->selectedPPD['link'],$this->ppdConfig);
224   }
226   /* Save to LDAP */
227   function save()
228   {
229     /* return the selected PPD, and in future the selected options too */
230     return($this->selectedPPD['link']);
231   }
233   function getPPDInformation()
234   {
235     /* Get Information for a single PPD entry 
236      * This will be shown on top of template
237      */
238     $str = "none";
239     if(!empty($this->selectedPPD)){
240       $str = $this->ppdManager->loadDescription($this->pathToPPD.$this->selectedPPD['link']);
241     }
242     return($str) ; 
243   }
245   function generateProperties()
246   { 
247     /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file. 
248      * This function will generate a userfriendly post based form with this informations
249      */
250   
251     /* Set Headline */
252     $str = "";
253     $feed= "";
255     $s_ppd = $this->pathToPPD.$this->selectedPPD['link'];
257     /* If ppd exists and is readable */
258     if((!empty($this->selectedPPD['link']))&&(file_exists($s_ppd))){
259     
260       /* If there is no initial Configuration, load it */
261       if($this->ppdConfig == false){
262         $this->ppdConfig = $this->ppdManager->loadProperties($s_ppd);
263       }
265       /* Create a table */
266       $str .= "<div style='padding-left:30px;'><table>";
268       /* Input all data to the table */
269       foreach($this->ppdConfig as $cat => $obj){
270     
271         /* Add new category */
272         $str .= "<tr><td colspan='2'>$feed";
273         if ($feed == ""){
274           $feed= "<br>";
275         }
276         $str .= "<b>"._("Section")." '".$cat."'&nbsp;</b><br>";
277         $str .= "</tr></td>";       
279         /* Add attributes of the current category */
280         foreach($obj as $attr => $settings){
281   
282       
283           /* Skip all entries beginning with _ */
284           if($attr[0] == "_") continue;  
285           
286           /* Prepare data */
287           $values   = array();
288           $name     = $settings['_name'];
289           $default  = $settings['_default'];
290           $type     = $settings['_type'];
291  
292           /* Add name to table */ 
293           $str .= "<tr><td style='padding-left:40px;'>\n";
294           $str .= $name."<br>\n";
295           $str .= "</td><td>\n";
296    
297           /* Get all values */ 
298           foreach( $settings as $vname => $value){
299             if($vname[0] != "_"){
300               $values[$vname]= $value;
301             }
302           }
304           /* preparing Html output
305            * Supported types are PickOne/Boolean
306            */
308           /* If type is PickOne, create a select box */
309           if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
311             $str  .=  "<select name='".base64_encode($name)."'>\n";
312             foreach($values as $value){
313               $selected = "";
314               if($value == $default){
315                 $selected = " selected ";
316               }
317               $str  .=  "<option value='".$value."' ".$selected.">".$value."</option>\n";
318             }
319             $str  .=  "</select>\n";
321           }elseif($type == "Boolean"){
323             /* If type is Boolean & no values are given */
324             $str  .=  "<select name='".base64_encode($name)."'>\n";
325             if($default == "False"){
326               $str  .=    "<option value='True' >"._("True")."</option>\n";
327               $str  .=    "<option value='False' selected>"._("False")."</option>\n";
328             }else{
329               $str  .=    "<option value='True' selected>"._("True")."</option>\n";
330               $str  .=    "<option value='False' >"._("False")."</option>\n";
331             }          
332             $str  .=  "</select>\n";
334           }else{
335             print_red(sprintf(_("Unsupported ppd type '%s' used for '%s' "),$type,$name));
336           }
337           $str .= "</td></tr>\n";
338         }
339       }
340       $str .= "</table></div>\n";
342     }
343     return($str);
344   }
346 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
347 ?>