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