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 $l = "";
18 var $description = "";
19 var $labeledURI = "";
20 var $gotoPrinterPPD = "";
21 var $orig_dn = "";
22 var $is_terminal = false;
24 var $UserMember ="";
25 var $UserMembers =array();
26 var $UserMemberKeys =array();
28 var $AdminMember ="";
29 var $AdminMembers =array();
30 var $AdminMemberKeys =array();
32 var $is_terminalBased = false;
34 var $PPDdialogToSave = NULL;
36 var $member =array();
37 var $strings = "";
38 var $type = "";
39 var $dialog =NULL;
41 var $netConfigDNS;
43 /* attribute list for save action */
44 var $attributes = array("cn", "description", "l", "labeledURI", "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 $this->netConfigDNS = new termDNS($this->config, $this->dn,$this->objectclasses);
55 /* Set base */
56 if ($this->dn == "new"){
57 $ui= get_userinfo();
58 $this->base= dn2base($ui->dn);
59 $this->cn= "";
60 $this->is_terminal = true;
61 } else {
62 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
63 }
65 if(isset($this->gotoPrinterPPD)){
66 $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
67 }
69 /* In case of gotoWorkstation this tab is calles from workstation plugin
70 * in case of gotoTerminal it is called from a terminal tab
71 * else it is a standalone printer
72 */
73 if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
74 /* Fix for new Workstations */
75 if(preg_match("/ou=incoming/",$this->dn)){
76 $this->is_terminal = "true";
77 $this->dn = preg_replace("/ou=incoming/","ou=printers",$this->dn);
78 $this->type = "station";
79 }else{
80 $this->is_terminal = "true";
81 $this->dn = preg_replace("/ou=workstations/","ou=printers",$this->dn);
82 $this->type = "station";
83 }
84 }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
85 if(preg_match("/ou=incoming/",$this->dn)){
86 $this->is_terminal = "true";
87 $this->dn = preg_replace("/ou=incoming/","ou=printers",$this->dn);
88 $this->type = "terminal";
89 }else{
90 $this->type = "terminal";
91 $this->is_terminal = "true";
92 $this->dn = preg_replace("/ou=terminals/","ou=printers",$this->dn);
93 }
94 }else{
95 /* Save dn for later references */
96 $this->orig_dn= $this->dn;
97 }
99 /* If it is no standalone printer
100 */
101 if($this->is_terminal){
102 // Reload plugin with new dn... (ou=printers instead of ou=terminals)
103 plugin::plugin ($this->config, $this->dn);
104 $ldap->cat($this->dn);
105 if(count($ldap->fetch())>0){
106 $this->orig_dn= $this->dn;
107 $this->is_account=true;
108 $this->initially_was_account = true;
109 }else{
110 $this->orig_dn = "new";
111 $this->is_account=false;
112 $this->initially_was_account = false;
113 }
114 }
116 /* Prepare different member types
117 */
118 foreach(array("AddUser" =>"gotoUserPrinter",
119 "AddGroup" =>"gotoGroupPrinter",
120 "AddAdminUser" =>"gotoUserAdminPrinter",
121 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
123 /* $this->members contains all members */
124 $this->member[$type]=array();
126 if (isset($this->attrs[$attr]['count'])) {
127 unset($this->attrs[$attr]['count']);
128 }
130 if(isset($this->attrs[$attr])){
131 foreach($this->attrs[$attr] as $mem){
132 if(preg_match("/Group/",$type)){
133 $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
134 $entry = $ldap->fetch();
135 if(isset($entry['description'])){
136 $this->member[$type][$entry['cn'][0]]=$entry;
137 }
138 }else{
139 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
140 $entry = $ldap->fetch();
141 if(isset($entry['uid'])){
142 $this->member[$type][$entry['uid'][0]]=$entry;
143 }
144 }
145 }
146 }
147 }
149 $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
150 if($attrs = $ldap->fetch()){
151 if(in_array("gotoTerminal",$attrs['objectClass'])){
152 $this->is_terminalBased = "Terminal";
153 }else{
154 $this->is_terminalBased = "Workstation";
155 }
156 }
158 /* A printer was only saved if the printer belongs to a workstation.
159 * If there was a new single printer created, it was never saved.
160 * -This allows new printers to be saved.
161 */
162 if(($this->dn == "new")&&(!$this->is_terminal)){
163 $this->is_account = true;
164 }
165 }
167 function execute()
168 {
169 /* Call parent execute */
170 plugin::execute();
172 if(isset($this->parent->by_name['workgeneric'])){
173 // echo "workstation";
174 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
175 $display= $this->show_header(_("Add printer extension"),
176 _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
177 $this->is_account= false;
178 return $display;
179 }
180 }elseif(isset($this->parent->by_name['termgeneric'])){
181 if($this->parent->by_object['termgeneric']->cn == "default"){
182 $display= $this->show_header(_("Add printer extension"),
183 _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
184 $this->is_account= false;
185 return $display;
186 }
187 }elseif(isset($this->parent->by_name['printgeneric'])){
188 // echo "printer";
189 }
192 $smarty= get_smarty();
193 $display="";
194 /* Template management.
195 * There are two ways to call this tab.
196 * 1. From a printer Dialog, here we will see the full template, without a toggle state button
197 * 2. As a terminal tab, here we hide the top (name,base,description) of the template.
198 * Toggle Account state will be shown in this case, to disable or enable this tab.
199 *
200 * $this->is_terminal indecates this two different types.
201 */
202 if($this->is_terminal){
203 $smarty->assign("is_terminal","true");
204 }else{
205 $smarty->assign("is_terminal","false");
206 }
208 /* Do we need to flip is_account state? */
209 if (isset($_POST['modify_state'])){
210 $this->is_modified = true;
211 $this->is_account= !$this->is_account;
212 }
214 if($this->is_terminal){
215 if(empty($this->cn)){
216 if(!in_array("gotoPrinter",$this->objectclasses)){
217 $display= $this->show_header(_("Add printer extension"),
218 _("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);
219 $this->is_account= false;
220 return $display;
221 }else{
222 $smarty->assign("is_terminal","false");
223 $skip = true;
224 }
225 }
227 if(!isset($skip)){
228 if (($this->is_account)){
229 if($this->type=="station"){
230 $display= $this->show_header(_("Remove printer extension"),
231 _("This workstation has printer extension enabled.You can disable it by clicking below."));
232 }else{
233 $display= $this->show_header(_("Remove printer extension"),
234 _("This terminal has printer extension enabled. You can disable it by clicking below."));
235 }
236 }else{
237 if($this->type=="station"){
238 $display= $this->show_header(_("Add printer extension"),
239 _("This workstation has printer extension disabled. You can enable it by clicking below."));
240 }else{
241 $display= $this->show_header(_("Add printer extension"),
242 _("This terminal has printer extension disabled. You can enable it by clicking below."));
243 }
244 return ($display);
245 }
246 }
247 }
250 /* Do we represent a valid printer? */
251 if (!$this->is_account && $this->parent == NULL){
252 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
253 _("This 'dn' has no printer features.")."</b>";
254 return($display);
255 }
257 /* Fill templating stuff */
258 $smarty->assign("bases", $this->config->idepartments);
259 $smarty->assign("base_select", $this->base);
261 /* Assign attributes */
262 foreach ($this->attributes as $attr){
263 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
264 $smarty->assign("$attr", $this->$attr);
265 }
267 if(isset($_POST['AddUser'])){
268 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
269 }
270 if(isset($_POST['AddGroup'])){
271 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
272 }
273 if(isset($_POST['AddAdminUser'])){
274 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
275 }
276 if(isset($_POST['AddAdminGroup'])){
277 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
278 }
280 if(isset($_POST['EditDriver'])){
281 if($this->PPDdialogToSave){
282 $this->dialog = $this->PPDdialogToSave;
283 }else{
284 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
285 }
286 }
288 if(isset($_POST['PrinterCancel'])){
289 unset($this->dialog);
290 $this->dialog= NULL;
291 }
293 if(isset($_POST['RemoveDriver'])){
294 $this->gotoPrinterPPD = array();
295 $this->PPDdialogToSave = NULL;
296 }
298 if(isset($_POST['SavePPD'])){
299 $this->dialog->save_object();
300 if(count($this->dialog->check())){
301 foreach($this->dialog->check() as $msg){
302 print_red($msg);
303 }
304 }else{
305 $this->gotoPrinterPPD = array();
307 $this->gotoPrinterPPD = $this->dialog->save();
308 $this->PPDdialogToSave = $this->dialog;
309 unset($this->dialog);
310 $this->dialog=NULL;
311 }
313 }
315 if(isset($_POST['ClosePPD'])){
316 unset($this->dialog);
317 $this->dialog=NULL;
318 }
322 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
323 if(isset($this->member['AddUser'][$_POST['UserMember']])){
324 unset($this->member['AddUser'][$_POST['UserMember']]);
325 }
326 }
328 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
329 if(isset($this->member['AddGroup'][$_POST['UserMember']])){
330 unset($this->member['AddGroup'][$_POST['UserMember']]);
331 }
332 }
334 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
335 if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
336 unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
337 }
338 }
340 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
341 if(isset($this->member['AddAdminGroup'][$_POST['AdminMember']])){
342 unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
343 }
344 }
346 if(isset($_POST['PrinterSave'])){
347 $this->dialog->save_object();
348 if(count($this->dialog->check())){
349 foreach($this->dialog->check() as $msg){
350 print_red($msg);
351 }
352 }else{
353 $new = $this->dialog->save();
354 $data = $new;
355 unset($data['type']);
357 if(preg_match("/User/",$new['type'])){
358 $use = "uid";
359 }else{
360 $use = "cn";
361 }
363 foreach($data as $mem){
364 $this->member[$new['type']][$mem[$use][0]]=$mem;
365 }
366 unset($this->dialog);
367 $this->dialog=NULL;
368 }
369 }
371 if($this->dialog != NULL){
372 $this->dialog->save_object();
373 $display = $this->dialog->execute();
374 return $display;
375 }
377 require_once ("class_ppdManager.inc");
379 if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
381 $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
382 if(!preg_match("/\/$/",$path)){
383 $path = $path."/";
384 }
386 $ppdManager= new ppdManager($path);
387 if(!empty($this->gotoPrinterPPD)){
388 if((!file_exists($path.$this->gotoPrinterPPD))){
389 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
390 }else{
391 $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
392 }
393 }else{
394 $smarty->assign("driverInfo", _("not defined"));
395 }
396 }else{
397 $smarty->assign("driverInfo",_("can't get ppd informations."));
398 }
401 $list=$this->generateList();
402 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
403 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
405 asort($userlist);
406 asort($adminlist);
407 if($this->is_terminalBased){
408 if($this->is_terminalBased == "Terminal"){
409 $smarty->assign("desc" ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
410 }else{
411 $smarty->assign("desc" ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
412 }
413 $smarty->assign("cnACL" ," disabled ");
414 }else{
415 $smarty->assign("desc" ,"");
416 }
417 $smarty->assign("UserMember" ,$this->UserMember);
418 $smarty->assign("UserMembers" ,$userlist);
419 $smarty->assign("UserMemberKeys",array_flip($userlist));
421 $smarty->assign("AdminMember" ,$this->AdminMember);
422 $smarty->assign("AdminMembers" ,$adminlist);
423 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
424 $smarty->assign("netconfig", $this->netConfigDNS->execute());
426 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
427 }
429 function remove_from_parent()
430 {
431 if($this->initially_was_account){
432 $ldap= $this->config->get_ldap_link();
433 $ldap->cat($this->dn);
434 if(count($ldap->fetch()) ){
436 $this->netConfigDNS->remove_from_parent();
437 $ldap->rmdir($this->dn);
438 show_ldap_error($ldap->get_error());
439 $this->handle_post_events("remove");
441 /* Delete references to object groups */
442 $ldap->cd ($this->config->current['BASE']);
443 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
444 while ($ldap->fetch()){
445 $og= new ogroup($this->config, $ldap->getDN());
446 unset($og->member[$this->dn]);
447 $og->save ();
448 }
449 }
450 }
451 }
452 /* Save data to object */
453 function save_object()
454 {
455 plugin::save_object();
456 $this->netConfigDNS->save_object();
457 /* Save base, since this is no LDAP attribute */
458 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
459 $this->base= $_POST['base'];
460 }
461 }
463 /* Check supplied data */
464 function check()
465 {
466 $message= $this->netConfigDNS->check();
468 if(isset($this->parent->by_name['workgeneric'])){
469 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
470 return $message;
471 }
472 }elseif(isset($this->parent->by_name['termgeneric'])){
473 if($this->parent->by_object['termgeneric']->cn == "default"){
474 return $message;
475 }
476 }
478 $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
480 /* must: cn */
481 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
482 $message[]= "The required field 'Printer name' is not set.";
483 }
485 $ui= get_userinfo();
486 $acl= get_permissions ($this->dn, $ui->subtreeACL);
487 $acl= get_module_permission($acl, "printer", $this->dn);
488 if (chkacl($acl, "create") != ""){
489 $message[]= _("You have no permissions to create a printer on this 'Base'.");
490 }
492 if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
493 $ldap= $this->config->get_ldap_link();
494 $ldap->cd ($this->base);
495 $ldap->search ("(cn=".$this->cn.")", array("cn"));
496 if ($ldap->count() != 0){
497 while ($attrs= $ldap->fetch()){
498 if ($attrs['dn'] != $this->orig_dn){
499 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
500 break;
501 }
502 }
503 }
504 }
506 return ($message);
507 }
510 /* Save to LDAP */
511 function save()
512 {
513 if (!$this->is_account){
514 return;
515 }
516 if(isset($this->parent->by_name['workgeneric'])){
517 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
518 return;
519 }
520 }elseif(isset($this->parent->by_name['termgeneric'])){
521 if($this->parent->by_object['termgeneric']->cn == "default"){
522 return;
523 }
524 }
526 if($this->PPDdialogToSave){
527 $this->PPDdialogToSave->save_ppd();
528 }
529 $dn= $this->dn;
530 plugin::save();
531 $ldap= $this->config->get_ldap_link();
533 if((in_array("gotoTerminal",$this->attrs['objectClass']))){
534 $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
535 }
537 if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
538 $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
539 }
541 /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn
542 */
543 if($this->is_terminal){
545 /* reduce objectClasses to minimun */
546 $this->attrs['objectClass']= $this->objectclasses;
548 /* If a printer with the given dn exists, modify else create new one */
549 $ldap->cat($this->dn);
550 if($ldap->fetch()){
551 $this->orig_dn= $this->dn;
552 }else{
553 $this->orig_dn = "new";
554 }
555 }
557 /* Remove all empty values */
558 if ($this->orig_dn == 'new'){
559 $attrs= array();
560 foreach ($this->attrs as $key => $val){
561 if (is_array($val) && count($val) == 0){
562 continue;
563 }
564 $attrs[$key]= $val;
565 }
566 $this->attrs= $attrs;
567 }
569 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
570 $method="https://";
571 }else{
572 $method="http://";
573 }
575 if(!is_array($this->attrs['gotoPrinterPPD'])) {
576 $this->attrs['gotoPrinterPPD'] = $this->attrs['gotoPrinterPPD'];
577 $this->attrs['gotoPrinterPPD'] = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->attrs['gotoPrinterPPD']);
578 }
580 /* Append printer user
581 */
582 foreach($this->member['AddUser'] as $mem){
583 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
584 }
586 /* Append printer group
587 */
588 foreach($this->member['AddGroup'] as $mem){
589 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
590 }
592 /* Append printer admin user
593 */
594 foreach($this->member['AddAdminUser'] as $mem){
595 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
596 }
598 /* Append printer admin group
599 */
600 foreach($this->member['AddAdminGroup'] as $mem){
601 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
602 }
604 /* Write back to ldap */
605 $ldap= $this->config->get_ldap_link();
606 if ($this->orig_dn == 'new'){
607 $ldap->cd($this->config->current['BASE']);
608 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
609 $ldap->cd($this->dn);
610 $ldap->add($this->attrs);
611 $this->handle_post_events("add");
612 } else {
613 if ($this->orig_dn != $this->dn){
614 $this->move($this->orig_dn, $this->dn);
615 }
617 $ldap->cd($this->dn);
618 $ldap->modify($this->attrs);
619 $this->handle_post_events("modify");
620 }
621 show_ldap_error($ldap->get_error());
623 $this->netConfigDNS->cn = $this->cn;
624 $this->netConfigDNS->save($this->dn);
626 /* Optionally execute a command after we're done */
627 $this->postcreate();
628 }
630 function generateList(){
631 $a_return=array();
632 foreach($this->member as $type => $values){
633 $a_return[$type]=array();
634 foreach($values as $value){
635 if((preg_match("/Group/i",$type))){
636 if(!isset($value['description'])){
637 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
638 }else{
639 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
640 }
641 }else{
642 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
643 }
644 }
645 }
646 return($a_return);
647 }
648 }
652 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
653 ?>