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