2ed5f19c9e1fb505637876dab96dd84c876a654a
1 <?php
3 class printgeneric extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage terminal base 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 /* Generic terminal attributes */
11 var $interfaces = array();
12 var $ignore_account = FALSE;
14 /* Needed values and lists */
15 var $base = "";
16 var $cn = "";
17 var $macAddress = "";
18 var $ipHostNumber = "";
19 var $l = "";
20 var $description = "";
21 var $labeledURI = "";
22 var $gotoPrinterPPD = "";
23 var $orig_dn = "";
24 var $is_terminal = false;
26 var $UserMember ="";
27 var $UserMembers =array();
28 var $UserMemberKeys =array();
30 var $AdminMember ="";
31 var $AdminMembers =array();
32 var $AdminMemberKeys =array();
34 var $is_terminalBased = false;
36 var $PPDdialogToSave = NULL;
38 var $member =array();
39 var $strings = "";
40 var $type = "";
41 var $dialog =NULL;
43 /* attribute list for save action */
44 var $attributes = array("cn", "description", "l", "labeledURI", "macAddress", "ipHostNumber","gotoPrinterPPD");
45 var $objectclasses = array("top", "gotoPrinter");
47 function printgeneric ($config, $dn= NULL)
48 {
49 plugin::plugin ($config, $dn);
50 $ldap= $this->config->get_ldap_link();
52 /* Set base */
53 if ($this->dn == "new"){
54 $ui= get_userinfo();
55 $this->base= dn2base($ui->dn);
56 $this->cn= "";
57 $this->is_terminal = true;
58 } else {
59 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
60 }
63 /* In case of gotoWorkstation this tab is calles from workstation plugin
64 * in case of gotoTerminal it is called from a terminal tab
65 * else it is a standalone printer
66 */
67 if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
68 /* Fix for new Workstations */
69 if(preg_match("/ou=incoming/",$this->dn)){
70 $this->is_terminal = "true";
71 $this->dn = preg_replace("/ou=incoming/","ou=printers",$this->dn);
72 $this->type = "station";
73 }else{
74 $this->is_terminal = "true";
75 $this->dn = preg_replace("/ou=workstations/","ou=printers",$this->dn);
76 $this->type = "station";
77 }
78 }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
79 if(preg_match("/ou=incoming/",$this->dn)){
80 $this->is_terminal = "true";
81 $this->dn = preg_replace("/ou=incoming/","ou=printers",$this->dn);
82 $this->type = "terminal";
83 }else{
84 $this->type = "terminal";
85 $this->is_terminal = "true";
86 $this->dn = preg_replace("/ou=terminal/","ou=printers",$this->dn);
87 }
88 }else{
89 /* Save dn for later references */
90 $this->orig_dn= $this->dn;
91 }
93 /* If it is no standalone printer
94 */
95 if($this->is_terminal){
96 // Reload plugin with new dn... (ou=printers instead of ou=terminals)
97 plugin::plugin ($this->config, $this->dn);
98 $ldap->cat($this->dn);
99 if(count($ldap->fetch())>0){
100 $this->orig_dn= $this->dn;
101 $this->is_account=true;
102 $this->initially_was_account = true;
103 }else{
104 $this->orig_dn = "new";
105 $this->is_account=false;
106 $this->initially_was_account = false;
107 }
108 }
110 /* Prepare different member types
111 */
112 foreach(array("AddUser" =>"gotoUserPrinter",
113 "AddGroup" =>"gotoGroupPrinter",
114 "AddAdminUser" =>"gotoUserAdminPrinter",
115 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
117 /* $this->members contains all members */
118 $this->member[$type]=array();
120 unset($this->attrs[$attr]['count']);
122 if(isset($this->attrs[$attr])){
123 foreach($this->attrs[$attr] as $mem){
124 if(preg_match("/Group/",$type)){
125 $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
126 $entry = $ldap->fetch();
127 if(isset($entry['description'])){
128 $this->member[$type][$entry['cn'][0]]=$entry;
129 }
130 }else{
131 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
132 $entry = $ldap->fetch();
133 if(isset($entry['uid'])){
134 $this->member[$type][$entry['uid'][0]]=$entry;
135 }
136 }
137 }
138 }
139 }
141 $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
142 if($attrs = $ldap->fetch()){
143 if(in_array("gotoTerminal",$attrs['objectClass'])){
144 $this->is_terminalBased = "Terminal";
145 }else{
146 $this->is_terminalBased = "Workstation";
147 }
148 }
150 /* A printer was only saved if the printer belongs to a workstation.
151 * If there was a new single printer created, it was never saved.
152 * -This allows new printers to be saved.
153 */
154 if($this->dn == "new"){
155 $this->is_account = true;
156 }
157 }
159 function execute()
160 {
161 /* Call parent execute */
162 plugin::execute();
164 $smarty= get_smarty();
165 $display="";
166 /* Template management.
167 * There are two ways to call this tab.
168 * 1. From a printer Dialog, here we will see the full template, without a toggle state button
169 * 2. As a terminal tab, here we hide the top (name,base,description) of the template.
170 * Toggle Account state will be shown in this case, to disable or enable this tab.
171 *
172 * $this->is_terminal indecates this two different types.
173 */
174 if($this->is_terminal){
175 $smarty->assign("is_terminal","true");
176 }else{
177 $smarty->assign("is_terminal","false");
178 }
180 /* Do we need to flip is_account state? */
181 if (isset($_POST['modify_state'])){
182 $this->is_modified = true;
183 $this->is_account= !$this->is_account;
184 }
186 if($this->is_terminal){
187 if(empty($this->cn)){
188 if(!in_array("gotoPrinter",$this->objectclasses)){
189 $display= $this->show_header(_("Add printer extension"),
190 _("This workstation has printer extension disabled. You can't enable it while 'cn' is not present in entry. Possibly you are currently creating a new terminal template"),TRUE,TRUE);
191 $this->is_account= false;
192 return $display;
193 }else{
194 $smarty->assign("is_terminal","false");
195 $skip = true;
196 }
197 }
199 if(!isset($skip)){
200 if (($this->is_account)){
201 if($this->type=="station"){
202 $display= $this->show_header(_("Remove printer extension"),
203 _("This workstation has printer extension enabled.You can disable it by clicking below."));
204 }else{
205 $display= $this->show_header(_("Remove printer extension"),
206 _("This terminal has printer extension enabled. You can disable it by clicking below."));
207 }
208 }else{
209 if($this->type=="station"){
210 $display= $this->show_header(_("Add printer extension"),
211 _("This workstation has printer extension disabled. You can enable it by clicking below."));
212 }else{
213 $display= $this->show_header(_("Add printer extension"),
214 _("This terminal has printer extension disabled. You can enable it by clicking below."));
215 }
216 return ($display);
217 }
218 }
219 }
222 /* Do we represent a valid printer? */
223 if (!$this->is_account && $this->parent == NULL){
224 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
225 _("This 'dn' has no printer features.")."</b>";
226 return($display);
227 }
229 /* Fill templating stuff */
230 $smarty->assign("bases", $this->config->idepartments);
231 $smarty->assign("base_select", $this->base);
233 /* Assign attributes */
234 foreach ($this->attributes as $attr){
235 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
236 $smarty->assign("$attr", $this->$attr);
237 }
239 if(isset($_POST['AddUser'])){
240 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
241 }
242 if(isset($_POST['AddGroup'])){
243 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
244 }
245 if(isset($_POST['AddAdminUser'])){
246 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
247 }
248 if(isset($_POST['AddAdminGroup'])){
249 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
250 }
252 if(isset($_POST['EditDriver'])){
253 if($this->PPDdialogToSave){
254 $this->dialog = $this->PPDdialogToSave;
255 }else{
256 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
257 }
258 }
260 if(isset($_POST['PrinterCancel'])){
261 unset($this->dialog);
262 $this->dialog= NULL;
263 }
265 if(isset($_POST['RemoveDriver'])){
266 $this->gotoPrinterPPD = array();
267 $this->PPDdialogToSave = NULL;
268 }
270 if(isset($_POST['SavePPD'])){
271 $this->dialog->save_object();
272 if(count($this->dialog->check())){
273 foreach($this->dialog->check() as $msg){
274 print_red($msg);
275 }
276 }else{
277 $this->gotoPrinterPPD = array();
279 $this->gotoPrinterPPD = $this->dialog->save();
280 $this->PPDdialogToSave = $this->dialog;
281 unset($this->dialog);
282 $this->dialog=NULL;
283 }
285 }
287 if(isset($_POST['ClosePPD'])){
288 unset($this->dialog);
289 $this->dialog=NULL;
290 }
292 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
293 if(isset($this->member['AddUser'][$_POST['UserMember']])){
294 unset($this->member['AddUser'][$_POST['UserMember']]);
295 }
296 }
298 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
299 if(isset($this->member['AddGroup'][$_POST['UserMember']])){
300 unset($this->member['AddGroup'][$_POST['UserMember']]);
301 }
302 }
304 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
305 if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
306 unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
307 }
308 }
310 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
311 if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
312 unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
313 }
314 }
316 if(isset($_POST['PrinterSave'])){
317 $this->dialog->save_object();
318 if(count($this->dialog->check())){
319 foreach($this->dialog->check() as $msg){
320 print_red($msg);
321 }
322 }else{
323 $new = $this->dialog->save();
324 $data = $new;
325 unset($data['type']);
327 if(preg_match("/User/",$new['type'])){
328 $use = "uid";
329 }else{
330 $use = "cn";
331 }
333 foreach($data as $mem){
334 $this->member[$new['type']][$mem[$use][0]]=$mem;
335 }
336 unset($this->dialog);
337 $this->dialog=NULL;
338 }
339 }
341 if($this->dialog != NULL){
342 $this->dialog->save_object();
343 $display = $this->dialog->execute();
344 return $display;
345 }
347 /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
348 $smarty->assign("staticAddress", "");
351 require_once ("class_ppdManager.inc");
352 $ppdManager= new ppdManager('/var/spool/ppd/');
353 if(!empty($this->gotoPrinterPPD)){
354 if((!file_exists($this->gotoPrinterPPD))){
355 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exists."),$this->gotoPrinterPPD))."</b>";
356 }else{
357 $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
358 }
359 }else{
360 $smarty->assign("driverInfo", _("not defined"));
361 }
363 $list=$this->generateList();
364 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
365 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
367 asort($userlist);
368 asort($adminlist);
369 if($this->is_terminalBased){
370 if($this->is_terminalBased == "Terminal"){
371 $smarty->assign("desc" ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
372 }else{
373 $smarty->assign("desc" ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
374 }
375 $smarty->assign("cnACL" ," disabled ");
376 }else{
377 $smarty->assign("desc" ,"");
378 }
379 $smarty->assign("UserMember" ,$this->UserMember);
380 $smarty->assign("UserMembers" ,$userlist);
381 $smarty->assign("UserMemberKeys",array_flip($userlist));
383 $smarty->assign("AdminMember" ,$this->AdminMember);
384 $smarty->assign("AdminMembers" ,$adminlist);
385 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
386 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
388 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
389 }
391 function remove_from_parent()
392 {
393 $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
394 $ldap= $this->config->get_ldap_link();
395 $ldap->rmdir($this->dn);
396 show_ldap_error($ldap->get_error());
397 $this->handle_post_events("remove");
399 /* Delete references to object groups */
400 $ldap->cd ($this->config->current['BASE']);
401 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
402 while ($ldap->fetch()){
403 $og= new ogroup($this->config, $ldap->getDN());
404 unset($og->member[$this->dn]);
405 $og->save ();
406 }
407 }
409 /* Save data to object */
410 function save_object()
411 {
412 plugin::save_object();
414 /* Save base, since this is no LDAP attribute */
415 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
416 $this->base= $_POST['base'];
417 }
418 }
420 /* Check supplied data */
421 function check()
422 {
423 $message= array();
424 $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
426 /* must: cn */
427 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
428 $message[]= "The required field 'Printer name' is not set.";
429 }
431 $ui= get_userinfo();
432 $acl= get_permissions ($this->dn, $ui->subtreeACL);
433 $acl= get_module_permission($acl, "printer", $this->dn);
434 if (chkacl($acl, "create") != ""){
435 $message[]= _("You have no permissions to create a printer on this 'Base'.");
436 }
438 if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
439 $ldap= $this->config->get_ldap_link();
440 $ldap->cd ($this->base);
441 $ldap->search ("(cn=".$this->cn.")", array("cn"));
442 if ($ldap->count() != 0){
443 while ($attrs= $ldap->fetch()){
444 if ($attrs['dn'] != $this->orig_dn){
445 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
446 break;
447 }
448 }
449 }
450 }
452 return ($message);
453 }
456 /* Save to LDAP */
457 function save()
458 {
459 if (!$this->is_account){
460 return;
461 }
463 if($this->PPDdialogToSave){
464 $this->PPDdialogToSave->save_ppd();
465 }
467 $dn= $this->dn;
468 plugin::save();
469 $ldap= $this->config->get_ldap_link();
471 if((in_array("gotoTerminal",$this->attrs['objectClass']))){
472 $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
473 }
475 if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
476 $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
477 }
479 /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn
480 */
481 if($this->is_terminal){
483 /* reduce objectClasses to minimun */
484 $this->attrs['objectClass']= $this->objectclasses;
486 /* If a printer with the given dn exists, modify else create new one */
487 $ldap->cat($this->dn);
488 if($ldap->fetch()){
489 $this->orig_dn= $this->dn;
490 }else{
491 $this->orig_dn = "new";
492 }
493 }
495 /* Remove all empty values */
496 if ($this->orig_dn == 'new'){
497 $attrs= array();
498 foreach ($this->attrs as $key => $val){
499 if (is_array($val) && count($val) == 0){
500 continue;
501 }
502 $attrs[$key]= $val;
503 }
504 $this->attrs= $attrs;
505 }
508 /* Append printer user
509 */
510 foreach($this->member['AddUser'] as $mem){
511 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
512 }
514 /* Append printer group
515 */
516 foreach($this->member['AddGroup'] as $mem){
517 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
518 }
520 /* Append printer admin user
521 */
522 foreach($this->member['AddAdminUser'] as $mem){
523 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
524 }
526 /* Append printer admin group
527 */
528 foreach($this->member['AddAdminGroup'] as $mem){
529 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
530 }
532 /* Write back to ldap */
533 $ldap= $this->config->get_ldap_link();
534 if ($this->orig_dn == 'new'){
535 $ldap->cd($this->config->current['BASE']);
536 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
537 $ldap->cd($this->dn);
538 $ldap->add($this->attrs);
539 $this->handle_post_events("add");
540 } else {
541 if ($this->orig_dn != $this->dn){
542 $this->move($this->orig_dn, $this->dn);
543 }
545 $ldap->cd($this->dn);
546 $ldap->modify($this->attrs);
547 $this->handle_post_events("modify");
548 }
549 show_ldap_error($ldap->get_error());
551 /* Optionally execute a command after we're done */
552 $this->postcreate();
553 }
555 function generateList(){
556 $a_return=array();
557 foreach($this->member as $type => $values){
558 $a_return[$type]=array();
559 foreach($values as $value){
560 if((preg_match("/Group/i",$type))){
561 if(!isset($value['description'])){
562 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
563 }else{
564 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
565 }
566 }else{
567 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
568 }
569 }
570 }
571 return($a_return);
572 }
573 }
577 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
578 ?>