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 function printerPPDDialog ($config, $dn= NULL,$ppdfile=NULL )
25 {
26 plugin::plugin ($config, $dn);
27 $this->depselect = $this->config->current['BASE'];
29 /* Load all available PPD files and sort them into an array
30 */
31 require_once ("class_ppdManager.inc");
32 $this->ppdManager= new ppdManager('/var/spool/ppd/');
33 $tmp = $this->ppdManager->getPrinterList();
35 /* Sort all available files, and create header (Vendor index) */
36 foreach($tmp as $file=>$ppd){
37 $tmp2 = split("\n",$ppd);
38 if(!isset($this->ppdListHeader[$tmp2[0]])){
39 $this->ppdListHeader[$tmp2[0]]=0;
40 }
41 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
42 $tmp3['link'] =$file;
43 $tmp3['ppd'] =$ppd;
44 $this->ppdListHeader[$tmp2[0]]++;
45 $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
46 }
48 /* The user has already a valid PPD assigned
49 * Get some informations about this PPD
50 */
51 if(($ppdfile!= NULL)&&(strlen($ppdfile)>0)){
52 $tmp2= split("\n", $this->ppdManager->loadDescription($ppdfile));
53 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]);
54 $tmp3['link'] =$ppdfile;
55 $tmp3['ppd'] =$this->ppdManager->loadDescription($ppdfile);
56 $this->selectedPPD = $tmp3;
57 }
58 }
60 function getPrinterReload()
61 {
62 $tmp = $this->ppdManager->getPrinterList(true);
64 /* Sort all available files, and create header (Vendor index) */
65 foreach($tmp as $file=>$ppd){
66 $tmp2 = split("\n",$ppd);
67 if(!isset($this->ppdListHeader[$tmp2[0]])){
68 $this->ppdListHeader[$tmp2[0]]=0;
69 }
70 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
71 $tmp3['link'] =$file;
72 $tmp3['ppd'] =$ppd;
73 $this->ppdListHeader[$tmp2[0]]++;
74 $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
75 }
77 }
79 function execute()
80 {
81 /* Fill templating stuff */
82 $smarty= get_smarty();
83 $display= "";
85 /* Is there a new PPD file uploaded ? */
86 if((isset($_FILES['NewPPDFile']))&&(isset($_POST['SubmitNewPPDFile']))){
87 $file = ($_FILES['NewPPDFile']);
88 if($file['size'] != 0 ){
89 $res = @$this->ppdManager->loadDescription($file['tmp_name']);
90 $name = (trim(preg_replace("/-/","",$res)));
91 if(!empty($name)){
92 $vendor = trim(preg_replace("/-.*$/i","",$res));
93 $model = trim(preg_replace("/".$vendor."/","",$name));
94 if(!is_dir('/var/spool/ppd/'.$vendor)){
95 if(!(@mkdir('/var/spool/ppd/'.$vendor))){
96 print_red(sprintf(_("Cab't create folder '%s' for the uploaded ppd file."),$vendor));
97 }
98 }
100 if(!isset($this->ppdList[$vendor])){
101 $this->ppdList[$vendor] = array();
102 }
104 if(is_dir('/var/spool/ppd/'.$vendor)){
105 $found = false;
106 foreach($this->ppdList[$vendor] as $key => $val){
107 if(preg_match("/".$model.".*/i",$key)){
108 $found = true;
109 print_red(sprintf(_("There is already a ppd file for this kind of printer.")));
110 }
111 }// Foreach
112 if(!$found){
113 $filename = '/var/spool/ppd/'.$vendor."/".$vendor."-".preg_replace("/^[^ ]/","",str_replace("-","",$model)).".ppd";
114 $fp = @fopen($filename,"w+");
115 if(!$fp){
116 print_red(sprintf(_("Can't save file '%s'"),$filename));
117 }else{
118 $str = file_get_contents($file['tmp_name']);
119 fputs($fp,$str);
120 @fclose($fp);
122 $tmp2= split("\n", $res);
123 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]);
124 $tmp3['link'] =$filename;
125 $tmp3['ppd'] =$res;
126 $this->selectedPPD = $tmp3;
127 $this->getPrinterReload();
128 }
129 }
130 }// If dir
131 }else{
132 print_red(_("Please specify a valid ppd file."));
133 }
134 }else{
135 print_red(_("Please specify a valid ppd file."));
136 }
138 }
141 /* Open a dialog that allow us to select different PPDs
142 */
143 if(isset($_POST['SelectPPD'])){
144 $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
145 }
147 /* The selection dialog fpr PPDs is canceled
148 */
149 if(isset($_POST['ClosePPDSelection'])){
150 unset($this->dialog);
151 $this->dialog=NULL;
152 }
154 /* A new PPDs was selected in the PPDs selection Dialog
155 * Perform a Check. If everything is fine, use the new PPD.
156 */
157 if(isset($_POST['SavePPDSelection'])){
158 if(!isset($_POST['PPDselection'])){
159 print_red(_("Please select a valid PPD."));
160 }else{
161 $this->selectedPPD['link'] = $_POST['PPDselection'];
162 $this->ppdConfig = false;
163 unset($this->dialog);
164 $this->dialog=NULL;
165 }
166 }
168 /* if a dialog is open, print the dialog instead of this class
169 */
170 if($this->dialog!=NULL){
171 $display = $this->dialog->execute();
172 return($display);
173 }
175 /* Give smarty the information it needs */
176 $smarty->assign("ppdString" ,$this->getPPDInformation());
177 $smarty->assign("properties",$this->generateProperties());
179 /* Print out template */
180 $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
181 return($display);
182 }
184 function check(){
185 /* Check the given data
186 */
187 $message=array();
188 return $message;
189 }
191 function save_object()
192 {
193 if(isset($_POST['PPDDisSubmitted'])){
194 if(is_array($this->ppdConfig)){
195 foreach($this->ppdConfig as $cat => $obj){
196 foreach($obj as $attr => $attributes){
197 if(isset($_POST[base64_encode($attributes['_name'])])){
198 $this->ppdConfig[$cat][$attr]['_default'] = $_POST[base64_encode($attributes['_name'])];
199 }
200 }
201 }
202 }
203 }
205 }
207 function save_ppd(){
208 $this->ppdManager->saveProperties($this->selectedPPD['link'],$this->ppdConfig);
209 }
211 /* Save to LDAP */
212 function save()
213 {
214 /* return the selected PPD, and in future the selected options too */
215 return($this->selectedPPD['link']);
216 }
218 function getPPDInformation()
219 {
220 /* Get Information for a single PPD entry
221 * This will be shown on top of template
222 */
223 $str = "none";
224 if(!empty($this->selectedPPD)){
225 $str = $this->selectedPPD['link'];
226 $str = $this->ppdManager->loadDescription($this->selectedPPD['link']);
227 }
228 return($str) ;
229 }
231 function generateProperties()
232 {
233 /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file.
234 * This function will generate a userfriendly post based form with this informations
235 */
237 /* Set Headline */
238 $str = "";
240 /* If ppd exists and is readable */
241 if((!empty($this->selectedPPD['link']))&&(file_exists($this->selectedPPD['link']))){
243 /* If there is no initial Configuration, load it */
244 if($this->ppdConfig == false){
245 $this->ppdConfig = $this->ppdManager->loadProperties($this->selectedPPD['link']);
246 }
248 /* Create a table */
249 $str .= "<div style='padding-left:30px;'><table>";
251 /* Input all data to the table */
252 foreach($this->ppdConfig as $cat => $obj){
254 /* Add new category */
255 $str .= "<tr><td colspan='2'><br>";
256 $str .= "<b>".$cat." : </b><br>";
257 $str .= "</tr></td>";
259 /* Add attributes of the current category */
260 foreach($obj as $attr => $settings){
263 /* Skip all entries beginning with _ */
264 if($attr[0] == "_") continue;
266 /* Prepare data */
267 $values = array();
268 $name = $settings['_name'];
269 $default = $settings['_default'];
270 $type = $settings['_type'];
272 /* Add name to table */
273 $str .= "<tr><td style='padding-left:40px;'>\n";
274 $str .= $name."<br>\n";
275 $str .= "</td><td>\n";
277 /* Get all values */
278 foreach( $settings as $vname => $value){
279 if($vname[0] != "_"){
280 $values[$vname]= $value;
281 }
282 }
284 /* preparing Html output
285 * Supported types are PickOne/Boolean
286 */
288 /* If type is PickOne, create a select box */
289 if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
291 $str .= "<select name='".base64_encode($name)."'>\n";
292 foreach($values as $value){
293 $selected = "";
294 if($value == $default){
295 $selected = " selected ";
296 }
297 $str .= "<option value='".$value."' ".$selected.">".$value."</option>\n";
298 }
299 $str .= "</select>\n";
301 }elseif($type == "Boolean"){
303 /* If type is Boolean & no values are given */
304 $str .= "<select name='".base64_encode($name)."'>\n";
305 if($default == "False"){
306 $str .= "<option value='True' >"._("True")."</option>\n";
307 $str .= "<option value='False' selected>"._("False")."</option>\n";
308 }else{
309 $str .= "<option value='True' selected>"._("True")."</option>\n";
310 $str .= "<option value='False' >"._("False")."</option>\n";
311 }
312 $str .= "</select>\n";
314 }else{
315 print_red(sprintf(_("Unsupported PPD type '%s' used for '%s' "),$type,$name));
316 }
317 $str .= "</td></tr>\n";
318 }
319 }
320 $str .= "</table></div>\n";
322 }
323 return($str);
324 }
325 }
326 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
327 ?>