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 /* This attributes is sometimes written with Uri instead of URI
159 In RFC 2079 it is defined with URI ...
160 */
161 if(isset($this->attrs['labeledUri'])){
162 $this->labeledURI = $this->attrs['labeledUri'][0];
163 }
165 /* A printer was only saved if the printer belongs to a workstation.
166 * If there was a new single printer created, it was never saved.
167 * -This allows new printers to be saved.
168 */
169 if($this->dn == "new"){
170 $this->is_account = true;
171 }
172 }
174 function execute()
175 {
176 /* Call parent execute */
177 plugin::execute();
179 if(isset($this->parent->by_name['workgeneric'])){
180 // echo "workstation";
181 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
182 $display= $this->show_header(_("Add printer extension"),
183 _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
184 $this->is_account= false;
185 return $display;
186 }
187 }elseif(isset($this->parent->by_name['termgeneric'])){
188 if($this->parent->by_object['termgeneric']->cn == "default"){
189 $display= $this->show_header(_("Add printer extension"),
190 _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
191 $this->is_account= false;
192 return $display;
193 }
194 }elseif(isset($this->parent->by_name['printgeneric'])){
195 // echo "printer";
196 }
199 $smarty= get_smarty();
200 $display="";
201 /* Template management.
202 * There are two ways to call this tab.
203 * 1. From a printer Dialog, here we will see the full template, without a toggle state button
204 * 2. As a terminal tab, here we hide the top (name,base,description) of the template.
205 * Toggle Account state will be shown in this case, to disable or enable this tab.
206 *
207 * $this->is_terminal indecates this two different types.
208 */
209 if($this->is_terminal){
210 $smarty->assign("is_terminal","true");
211 }else{
212 $smarty->assign("is_terminal","false");
213 }
215 /* Do we need to flip is_account state? */
216 if (isset($_POST['modify_state'])){
217 $this->is_modified = true;
218 $this->is_account= !$this->is_account;
219 }
221 if($this->is_terminal){
222 if(empty($this->cn)){
223 if(!in_array("gotoPrinter",$this->objectclasses)){
224 $display= $this->show_header(_("Add printer extension"),
225 _("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);
226 $this->is_account= false;
227 return $display;
228 }else{
229 $smarty->assign("is_terminal","false");
230 $skip = true;
231 }
232 }
234 if(!isset($skip)){
235 if (($this->is_account)){
236 if($this->type=="station"){
237 $display= $this->show_header(_("Remove printer extension"),
238 _("This workstation has printer extension enabled.You can disable it by clicking below."));
239 }else{
240 $display= $this->show_header(_("Remove printer extension"),
241 _("This terminal has printer extension enabled. You can disable it by clicking below."));
242 }
243 }else{
244 if($this->type=="station"){
245 $display= $this->show_header(_("Add printer extension"),
246 _("This workstation has printer extension disabled. You can enable it by clicking below."));
247 }else{
248 $display= $this->show_header(_("Add printer extension"),
249 _("This terminal has printer extension disabled. You can enable it by clicking below."));
250 }
251 return ($display);
252 }
253 }
254 }
257 /* Do we represent a valid printer? */
258 if (!$this->is_account && $this->parent == NULL){
259 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
260 _("This 'dn' has no printer features.")."</b>";
261 return($display);
262 }
264 /* Fill templating stuff */
265 $smarty->assign("bases", $this->config->idepartments);
266 $smarty->assign("base_select", $this->base);
268 /* Assign attributes */
269 foreach ($this->attributes as $attr){
270 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
271 $smarty->assign("$attr", $this->$attr);
272 }
274 if(isset($_POST['AddUser'])){
275 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
276 }
277 if(isset($_POST['AddGroup'])){
278 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
279 }
280 if(isset($_POST['AddAdminUser'])){
281 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
282 }
283 if(isset($_POST['AddAdminGroup'])){
284 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
285 }
287 if(isset($_POST['EditDriver'])){
288 if($this->PPDdialogToSave){
289 $this->dialog = $this->PPDdialogToSave;
290 }else{
291 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
292 }
293 }
295 if(isset($_POST['PrinterCancel'])){
296 unset($this->dialog);
297 $this->dialog= NULL;
298 }
300 if(isset($_POST['RemoveDriver'])){
301 $this->gotoPrinterPPD = array();
302 $this->PPDdialogToSave = NULL;
303 }
305 if(isset($_POST['SavePPD'])){
306 $this->dialog->save_object();
307 if(count($this->dialog->check())){
308 foreach($this->dialog->check() as $msg){
309 print_red($msg);
310 }
311 }else{
312 $this->gotoPrinterPPD = array();
314 $this->gotoPrinterPPD = $this->dialog->save();
315 $this->PPDdialogToSave = $this->dialog;
316 unset($this->dialog);
317 $this->dialog=NULL;
318 }
320 }
322 if(isset($_POST['ClosePPD'])){
323 unset($this->dialog);
324 $this->dialog=NULL;
325 }
329 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
330 if(isset($this->member['AddUser'][$_POST['UserMember']])){
331 unset($this->member['AddUser'][$_POST['UserMember']]);
332 }
333 }
335 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
336 if(isset($this->member['AddGroup'][$_POST['UserMember']])){
337 unset($this->member['AddGroup'][$_POST['UserMember']]);
338 }
339 }
341 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
342 if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
343 unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
344 }
345 }
347 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
348 if(isset($this->member['AddAdminGroup'][$_POST['AdminMember']])){
349 unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
350 }
351 }
353 if(isset($_POST['PrinterSave'])){
354 $this->dialog->save_object();
355 if(count($this->dialog->check())){
356 foreach($this->dialog->check() as $msg){
357 print_red($msg);
358 }
359 }else{
360 $new = $this->dialog->save();
361 $data = $new;
362 unset($data['type']);
364 if(preg_match("/User/",$new['type'])){
365 $use = "uid";
366 }else{
367 $use = "cn";
368 }
370 foreach($data as $mem){
371 $this->member[$new['type']][$mem[$use][0]]=$mem;
372 }
373 unset($this->dialog);
374 $this->dialog=NULL;
375 }
376 }
378 if($this->dialog != NULL){
379 $this->dialog->save_object();
380 $display = $this->dialog->execute();
381 return $display;
382 }
384 require_once ("class_ppdManager.inc");
386 if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
388 $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
389 if(!preg_match("/\/$/",$path)){
390 $path = $path."/";
391 }
393 $ppdManager= new ppdManager($path);
394 if(!empty($this->gotoPrinterPPD)){
395 if((!file_exists($path.$this->gotoPrinterPPD))){
396 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
397 }else{
398 $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
399 }
400 }else{
401 $smarty->assign("driverInfo", _("not defined"));
402 }
403 }else{
404 $smarty->assign("driverInfo",_("can't get ppd informations."));
405 }
408 $list=$this->generateList();
409 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
410 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
412 asort($userlist);
413 asort($adminlist);
414 if($this->is_terminalBased){
415 if($this->is_terminalBased == "Terminal"){
416 $smarty->assign("desc" ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
417 }else{
418 $smarty->assign("desc" ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
419 }
420 $smarty->assign("cnACL" ," disabled ");
421 }else{
422 $smarty->assign("desc" ,"");
423 }
424 $smarty->assign("UserMember" ,$this->UserMember);
425 $smarty->assign("UserMembers" ,$userlist);
426 $smarty->assign("UserMemberKeys",array_flip($userlist));
428 $smarty->assign("AdminMember" ,$this->AdminMember);
429 $smarty->assign("AdminMembers" ,$adminlist);
430 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
431 $smarty->assign("netconfig", $this->netConfigDNS->execute());
433 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
434 }
436 function remove_from_parent()
437 {
438 if($this->initially_was_account){
439 $ldap= $this->config->get_ldap_link();
440 $ldap->cat($this->dn);
441 if(count($ldap->fetch()) ){
443 $ldap->rmdir($this->dn);
444 show_ldap_error($ldap->get_error());
445 $this->handle_post_events("remove");
447 /* Delete references to object groups */
448 $ldap->cd ($this->config->current['BASE']);
449 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
450 while ($ldap->fetch()){
451 $og= new ogroup($this->config, $ldap->getDN());
452 unset($og->member[$this->dn]);
453 $og->save ();
454 }
456 $this->netConfigDNS->remove_from_parent();
457 }
458 }
459 }
460 /* Save data to object */
461 function save_object()
462 {
463 plugin::save_object();
464 $this->netConfigDNS->save_object();
465 /* Save base, since this is no LDAP attribute */
466 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
467 $this->base= $_POST['base'];
468 }
469 }
471 /* Check supplied data */
472 function check()
473 {
474 $message= $this->netConfigDNS->check();
476 if(isset($this->parent->by_name['workgeneric'])){
477 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
478 return $message;
479 }
480 }elseif(isset($this->parent->by_name['termgeneric'])){
481 if($this->parent->by_object['termgeneric']->cn == "default"){
482 return $message;
483 }
484 }
486 $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
488 /* must: cn */
489 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
490 $message[]= "The required field 'Printer name' is not set.";
491 }
493 $ui= get_userinfo();
494 $acl= get_permissions ($this->dn, $ui->subtreeACL);
495 $acl= get_module_permission($acl, "printer", $this->dn);
496 if (chkacl($acl, "create") != ""){
497 $message[]= _("You have no permissions to create a printer on this 'Base'.");
498 }
500 if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
501 $ldap= $this->config->get_ldap_link();
502 $ldap->cd ($this->base);
503 $ldap->search ("(cn=".$this->cn.")", array("cn"));
504 if ($ldap->count() != 0){
505 while ($attrs= $ldap->fetch()){
506 if ($attrs['dn'] != $this->orig_dn){
507 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
508 break;
509 }
510 }
511 }
512 }
514 return ($message);
515 }
518 /* Save to LDAP */
519 function save()
520 {
521 if (!$this->is_account){
522 return;
523 }
524 if(isset($this->parent->by_name['workgeneric'])){
525 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
526 return;
527 }
528 }elseif(isset($this->parent->by_name['termgeneric'])){
529 if($this->parent->by_object['termgeneric']->cn == "default"){
530 return;
531 }
532 }
534 if($this->PPDdialogToSave){
535 $this->PPDdialogToSave->save_ppd();
536 }
537 $dn= $this->dn;
538 plugin::save();
539 $ldap= $this->config->get_ldap_link();
541 if((in_array("gotoTerminal",$this->attrs['objectClass']))){
542 $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
543 }
545 if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
546 $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
547 }
549 /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn
550 */
551 if($this->is_terminal){
553 /* reduce objectClasses to minimun */
554 $this->attrs['objectClass']= $this->objectclasses;
556 /* If a printer with the given dn exists, modify else create new one */
557 $ldap->cat($this->dn);
558 if($ldap->fetch()){
559 $this->orig_dn= $this->dn;
560 }else{
561 $this->orig_dn = "new";
562 }
563 }
565 /* Remove all empty values */
566 if ($this->orig_dn == 'new'){
567 $attrs= array();
568 foreach ($this->attrs as $key => $val){
569 if (is_array($val) && count($val) == 0){
570 continue;
571 }
572 $attrs[$key]= $val;
573 }
574 $this->attrs= $attrs;
575 }
577 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
578 $method="https://";
579 }else{
580 $method="http://";
581 }
583 if(!is_array($this->attrs['gotoPrinterPPD'])) {
584 $this->attrs['gotoPrinterPPD'] = $this->attrs['gotoPrinterPPD'];
585 $this->attrs['gotoPrinterPPD'] = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->attrs['gotoPrinterPPD']);
586 }
588 /* Append printer user
589 */
590 foreach($this->member['AddUser'] as $mem){
591 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
592 }
594 /* Append printer group
595 */
596 foreach($this->member['AddGroup'] as $mem){
597 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
598 }
600 /* Append printer admin user
601 */
602 foreach($this->member['AddAdminUser'] as $mem){
603 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
604 }
606 /* Append printer admin group
607 */
608 foreach($this->member['AddAdminGroup'] as $mem){
609 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
610 }
612 /* Write back to ldap */
613 $ldap= $this->config->get_ldap_link();
614 if ($this->orig_dn == 'new'){
615 $ldap->cd($this->config->current['BASE']);
616 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
617 $ldap->cd($this->dn);
618 $ldap->add($this->attrs);
619 $this->handle_post_events("add");
620 } else {
621 if ($this->orig_dn != $this->dn){
622 $this->move($this->orig_dn, $this->dn);
623 }
625 $ldap->cd($this->dn);
626 $ldap->modify($this->attrs);
627 $this->handle_post_events("modify");
628 }
629 show_ldap_error($ldap->get_error());
631 $this->netConfigDNS->save($this->dn);
633 /* Optionally execute a command after we're done */
634 $this->postcreate();
635 }
637 function generateList(){
638 $a_return=array();
639 foreach($this->member as $type => $values){
640 $a_return[$type]=array();
641 foreach($values as $value){
642 if((preg_match("/Group/i",$type))){
643 if(!isset($value['description'])){
644 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
645 }else{
646 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
647 }
648 }else{
649 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
650 }
651 }
652 }
653 return($a_return);
654 }
655 }
659 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
660 ?>