8515a1b49b89f990fb3d044991af793485b4689f
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 $filename = $this->pathToPPD.$vendor."/".$vendor."-".preg_replace("/^[^ ]/","",str_replace("-","",$model)).".ppd";
140 $fp = @fopen($filename,"w+");
141 if(!$fp){
142 print_red(sprintf(_("Can't save file '%s'."),$filename));
143 }else{
144 $str = file_get_contents($file['tmp_name']);
145 fputs($fp,$str);
146 @fclose($fp);
148 $tmp2= split("\n", $res);
149 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]);
150 $tmp3['link'] =$filename;
151 $tmp3['ppd'] =$res;
152 $this->selectedPPD = $tmp3;
153 $this->getPrinterReload();
154 }
155 }
156 }// If dir
157 }else{
158 print_red(_("Please specify a valid ppd file."));
159 }
160 }else{
161 print_red(_("Please specify a valid ppd file."));
162 }
164 }
167 /* Open a dialog that allow us to select different PPDs
168 */
169 if(isset($_POST['SelectPPD'])){
170 $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
171 }
173 /* The selection dialog fpr PPDs is canceled
174 */
175 if(isset($_POST['ClosePPDSelection'])){
176 unset($this->dialog);
177 $this->dialog=NULL;
178 }
180 /* A new PPDs was selected in the PPDs selection Dialog
181 * Perform a Check. If everything is fine, use the new PPD.
182 */
183 if(isset($_POST['SavePPDSelection'])){
184 if(!isset($_POST['PPDselection'])){
185 print_red(_("Please select a valid ppd."));
186 }else{
187 $this->selectedPPD['link'] = $_POST['PPDselection'];
188 $this->ppdConfig = false;
189 unset($this->dialog);
190 $this->dialog=NULL;
191 }
192 }
194 /* if a dialog is open, print the dialog instead of this class
195 */
196 if($this->dialog!=NULL){
197 $display = $this->dialog->execute();
198 return($display);
199 }
201 /* Give smarty the information it needs */
202 $smarty->assign("ppdString" ,$this->getPPDInformation());
203 $tmp= $this->generateProperties();
204 if ($tmp == ""){
205 $smarty->assign("showOptions", 0);
206 } else {
207 $smarty->assign("showOptions", 1);
208 $smarty->assign("properties",$this->generateProperties());
209 }
210 }
211 /* Print out template */
212 $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
213 return($display);
214 }
216 function check(){
217 /* Check the given data
218 */
219 $message=array();
220 return $message;
221 }
223 function save_object()
224 {
225 if(isset($_POST['PPDDisSubmitted'])){
226 if(is_array($this->ppdConfig)){
227 foreach($this->ppdConfig as $cat => $obj){
228 foreach($obj as $attr => $attributes){
229 if(isset($_POST[base64_encode($attributes['_name'])])){
230 $this->ppdConfig[$cat][$attr]['_default'] = $_POST[base64_encode($attributes['_name'])];
231 }
232 }
233 }
234 }
235 }
237 }
239 function save_ppd(){
240 $this->ppdManager->saveProperties($this->pathToPPD.$this->selectedPPD['link'],$this->ppdConfig);
241 }
243 /* Save to LDAP */
244 function save()
245 {
246 /* return the selected PPD, and in future the selected options too */
247 return($this->selectedPPD['link']);
248 }
250 function getPPDInformation()
251 {
252 /* Get Information for a single PPD entry
253 * This will be shown on top of template
254 */
255 $str = "none";
256 if(!empty($this->selectedPPD)){
257 $str = $this->ppdManager->loadDescription($this->pathToPPD.$this->selectedPPD['link']);
258 }
259 return($str) ;
260 }
262 function generateProperties()
263 {
264 /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file.
265 * This function will generate a userfriendly post based form with this informations
266 */
268 /* Set Headline */
269 $str = "";
270 $feed= "";
272 $s_ppd = $this->pathToPPD.$this->selectedPPD['link'];
274 /* If ppd exists and is readable */
275 if((!empty($this->selectedPPD['link']))&&(file_exists($s_ppd))){
277 /* If there is no initial Configuration, load it */
278 if($this->ppdConfig == false){
279 $this->ppdConfig = $this->ppdManager->loadProperties($s_ppd);
280 }
282 /* Create a table */
283 $str .= "<div style='padding-left:30px;'><table>";
285 /* Input all data to the table */
286 foreach($this->ppdConfig as $cat => $obj){
288 /* Add new category */
289 $str .= "<tr><td colspan='2'>$feed";
290 if ($feed == ""){
291 $feed= "<br>";
292 }
293 $str .= "<b>"._("Section")." '".$cat."' </b><br>";
294 $str .= "</tr></td>";
296 /* Add attributes of the current category */
297 foreach($obj as $attr => $settings){
300 /* Skip all entries beginning with _ */
301 if($attr[0] == "_") continue;
303 /* Prepare data */
304 $values = array();
305 $name = $settings['_name'];
306 $default = $settings['_default'];
307 $type = $settings['_type'];
309 /* Add name to table */
310 $str .= "<tr><td style='padding-left:40px;'>\n";
311 $str .= $name."<br>\n";
312 $str .= "</td><td>\n";
314 /* Get all values */
315 foreach( $settings as $vname => $value){
316 if($vname[0] != "_"){
317 $values[$vname]= $value;
318 }
319 }
321 /* preparing Html output
322 * Supported types are PickOne/Boolean
323 */
325 /* If type is PickOne, create a select box */
326 if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
328 $str .= "<select name='".base64_encode($name)."'>\n";
329 foreach($values as $value){
330 $selected = "";
331 if($value == $default){
332 $selected = " selected ";
333 }
334 $str .= "<option value='".$value."' ".$selected.">".$value."</option>\n";
335 }
336 $str .= "</select>\n";
338 }elseif($type == "Boolean"){
340 /* If type is Boolean & no values are given */
341 $str .= "<select name='".base64_encode($name)."'>\n";
342 if($default == "False"){
343 $str .= "<option value='True' >"._("True")."</option>\n";
344 $str .= "<option value='False' selected>"._("False")."</option>\n";
345 }else{
346 $str .= "<option value='True' selected>"._("True")."</option>\n";
347 $str .= "<option value='False' >"._("False")."</option>\n";
348 }
349 $str .= "</select>\n";
351 }else{
352 print_red(sprintf(_("Unsupported ppd type '%s' used for '%s' "),$type,$name));
353 }
354 $str .= "</td></tr>\n";
355 }
356 }
357 $str .= "</table></div>\n";
359 }
360 return($str);
361 }
362 }
363 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
364 ?>