86da20b32d298e590b0435905e6f0f47a7f1c75c
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 if(!file_exists($ppdfile)){
53 print_red(sprintf(_("Can't open '%s', ppd settings reseted."),$ppdfile));
54 }else{
55 $tmp2= split("\n", $this->ppdManager->loadDescription($ppdfile));
56 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]);
57 $tmp3['link'] =$ppdfile;
58 $tmp3['ppd'] =$this->ppdManager->loadDescription($ppdfile);
59 $this->selectedPPD = $tmp3;
60 }
61 }
62 }
64 function getPrinterReload()
65 {
66 $tmp = $this->ppdManager->getPrinterList(true);
68 /* Sort all available files, and create header (Vendor index) */
69 foreach($tmp as $file=>$ppd){
70 $tmp2 = split("\n",$ppd);
71 if(!isset($this->ppdListHeader[$tmp2[0]])){
72 $this->ppdListHeader[$tmp2[0]]=0;
73 }
74 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
75 $tmp3['link'] =$file;
76 $tmp3['ppd'] =$ppd;
77 $this->ppdListHeader[$tmp2[0]]++;
78 $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
79 }
81 }
83 function execute()
84 {
85 /* Call parent execute */
86 plugin::execute();
87 /* Fill templating stuff */
88 $smarty= get_smarty();
89 $display= "";
91 /* Is there a new PPD file uploaded ? */
92 if((isset($_FILES['NewPPDFile']))&&(isset($_POST['SubmitNewPPDFile']))){
93 $file = ($_FILES['NewPPDFile']);
94 if($file['size'] != 0 ){
95 $res = @$this->ppdManager->loadDescription($file['tmp_name']);
96 $name = (trim(preg_replace("/-/","",$res)));
97 if(!empty($name)){
98 $vendor = trim(preg_replace("/-.*$/i","",$res));
99 $model = trim(preg_replace("/".$vendor."/","",$name));
100 if(!is_dir('/var/spool/ppd/'.$vendor)){
101 if(!(@mkdir('/var/spool/ppd/'.$vendor))){
102 print_red(sprintf(_("Cab't create folder '%s' for the uploaded ppd file."),$vendor));
103 }
104 }
106 if(!isset($this->ppdList[$vendor])){
107 $this->ppdList[$vendor] = array();
108 }
110 if(is_dir('/var/spool/ppd/'.$vendor)){
111 $found = false;
112 foreach($this->ppdList[$vendor] as $key => $val){
113 if(preg_match("/".$model.".*/i",$key)){
114 $found = true;
115 print_red(sprintf(_("There is already a ppd file for this kind of printer.")));
116 }
117 }// Foreach
118 if(!$found){
119 $filename = '/var/spool/ppd/'.$vendor."/".$vendor."-".preg_replace("/^[^ ]/","",str_replace("-","",$model)).".ppd";
120 $fp = @fopen($filename,"w+");
121 if(!$fp){
122 print_red(sprintf(_("Can't save file '%s'"),$filename));
123 }else{
124 $str = file_get_contents($file['tmp_name']);
125 fputs($fp,$str);
126 @fclose($fp);
128 $tmp2= split("\n", $res);
129 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]);
130 $tmp3['link'] =$filename;
131 $tmp3['ppd'] =$res;
132 $this->selectedPPD = $tmp3;
133 $this->getPrinterReload();
134 }
135 }
136 }// If dir
137 }else{
138 print_red(_("Please specify a valid ppd file."));
139 }
140 }else{
141 print_red(_("Please specify a valid ppd file."));
142 }
144 }
147 /* Open a dialog that allow us to select different PPDs
148 */
149 if(isset($_POST['SelectPPD'])){
150 $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
151 }
153 /* The selection dialog fpr PPDs is canceled
154 */
155 if(isset($_POST['ClosePPDSelection'])){
156 unset($this->dialog);
157 $this->dialog=NULL;
158 }
160 /* A new PPDs was selected in the PPDs selection Dialog
161 * Perform a Check. If everything is fine, use the new PPD.
162 */
163 if(isset($_POST['SavePPDSelection'])){
164 if(!isset($_POST['PPDselection'])){
165 print_red(_("Please select a valid ppd."));
166 }else{
167 $this->selectedPPD['link'] = $_POST['PPDselection'];
168 $this->ppdConfig = false;
169 unset($this->dialog);
170 $this->dialog=NULL;
171 }
172 }
174 /* if a dialog is open, print the dialog instead of this class
175 */
176 if($this->dialog!=NULL){
177 $display = $this->dialog->execute();
178 return($display);
179 }
181 /* Give smarty the information it needs */
182 $smarty->assign("ppdString" ,$this->getPPDInformation());
183 $tmp= $this->generateProperties();
184 if ($tmp == ""){
185 $smarty->assign("showOptions", 0);
186 } else {
187 $smarty->assign("showOptions", 1);
188 $smarty->assign("properties",$this->generateProperties());
189 }
191 /* Print out template */
192 $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
193 return($display);
194 }
196 function check(){
197 /* Check the given data
198 */
199 $message=array();
200 return $message;
201 }
203 function save_object()
204 {
205 if(isset($_POST['PPDDisSubmitted'])){
206 if(is_array($this->ppdConfig)){
207 foreach($this->ppdConfig as $cat => $obj){
208 foreach($obj as $attr => $attributes){
209 if(isset($_POST[base64_encode($attributes['_name'])])){
210 $this->ppdConfig[$cat][$attr]['_default'] = $_POST[base64_encode($attributes['_name'])];
211 }
212 }
213 }
214 }
215 }
217 }
219 function save_ppd(){
220 $this->ppdManager->saveProperties($this->selectedPPD['link'],$this->ppdConfig);
221 }
223 /* Save to LDAP */
224 function save()
225 {
226 /* return the selected PPD, and in future the selected options too */
227 return($this->selectedPPD['link']);
228 }
230 function getPPDInformation()
231 {
232 /* Get Information for a single PPD entry
233 * This will be shown on top of template
234 */
235 $str = "none";
236 if(!empty($this->selectedPPD)){
237 $str = $this->selectedPPD['link'];
238 $str = $this->ppdManager->loadDescription($this->selectedPPD['link']);
239 }
240 return($str) ;
241 }
243 function generateProperties()
244 {
245 /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file.
246 * This function will generate a userfriendly post based form with this informations
247 */
249 /* Set Headline */
250 $str = "";
251 $feed= "";
253 /* If ppd exists and is readable */
254 if((!empty($this->selectedPPD['link']))&&(file_exists($this->selectedPPD['link']))){
256 /* If there is no initial Configuration, load it */
257 if($this->ppdConfig == false){
258 $this->ppdConfig = $this->ppdManager->loadProperties($this->selectedPPD['link']);
259 }
261 /* Create a table */
262 $str .= "<div style='padding-left:30px;'><table>";
264 /* Input all data to the table */
265 foreach($this->ppdConfig as $cat => $obj){
267 /* Add new category */
268 $str .= "<tr><td colspan='2'>$feed";
269 if ($feed == ""){
270 $feed= "<br>";
271 }
272 $str .= "<b>"._("Section")." '".$cat."' </b><br>";
273 $str .= "</tr></td>";
275 /* Add attributes of the current category */
276 foreach($obj as $attr => $settings){
279 /* Skip all entries beginning with _ */
280 if($attr[0] == "_") continue;
282 /* Prepare data */
283 $values = array();
284 $name = $settings['_name'];
285 $default = $settings['_default'];
286 $type = $settings['_type'];
288 /* Add name to table */
289 $str .= "<tr><td style='padding-left:40px;'>\n";
290 $str .= $name."<br>\n";
291 $str .= "</td><td>\n";
293 /* Get all values */
294 foreach( $settings as $vname => $value){
295 if($vname[0] != "_"){
296 $values[$vname]= $value;
297 }
298 }
300 /* preparing Html output
301 * Supported types are PickOne/Boolean
302 */
304 /* If type is PickOne, create a select box */
305 if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
307 $str .= "<select name='".base64_encode($name)."'>\n";
308 foreach($values as $value){
309 $selected = "";
310 if($value == $default){
311 $selected = " selected ";
312 }
313 $str .= "<option value='".$value."' ".$selected.">".$value."</option>\n";
314 }
315 $str .= "</select>\n";
317 }elseif($type == "Boolean"){
319 /* If type is Boolean & no values are given */
320 $str .= "<select name='".base64_encode($name)."'>\n";
321 if($default == "False"){
322 $str .= "<option value='True' >"._("True")."</option>\n";
323 $str .= "<option value='False' selected>"._("False")."</option>\n";
324 }else{
325 $str .= "<option value='True' selected>"._("True")."</option>\n";
326 $str .= "<option value='False' >"._("False")."</option>\n";
327 }
328 $str .= "</select>\n";
330 }else{
331 print_red(sprintf(_("Unsupported ppd type '%s' used for '%s' "),$type,$name));
332 }
333 $str .= "</td></tr>\n";
334 }
335 }
336 $str .= "</table></div>\n";
338 }
339 return($str);
340 }
341 }
342 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
343 ?>