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