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 }
62 if(isset($this->gotoPrinterPPD)){
63 $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
64 }
66 /* In case of gotoWorkstation this tab is calles from workstation plugin
67 * in case of gotoTerminal it is called from a terminal tab
68 * else it is a standalone printer
69 */
70 if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
71 /* Fix for new Workstations */
72 if(preg_match("/ou=incoming/",$this->dn)){
73 $this->is_terminal = "true";
74 $this->dn = preg_replace("/ou=incoming/","ou=printers",$this->dn);
75 $this->type = "station";
76 }else{
77 $this->is_terminal = "true";
78 $this->dn = preg_replace("/ou=workstations/","ou=printers",$this->dn);
79 $this->type = "station";
80 }
81 }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
82 if(preg_match("/ou=incoming/",$this->dn)){
83 $this->is_terminal = "true";
84 $this->dn = preg_replace("/ou=incoming/","ou=printers",$this->dn);
85 $this->type = "terminal";
86 }else{
87 $this->type = "terminal";
88 $this->is_terminal = "true";
89 $this->dn = preg_replace("/ou=terminal/","ou=printers",$this->dn);
90 }
91 }else{
92 /* Save dn for later references */
93 $this->orig_dn= $this->dn;
94 }
96 /* If it is no standalone printer
97 */
98 if($this->is_terminal){
99 // Reload plugin with new dn... (ou=printers instead of ou=terminals)
100 plugin::plugin ($this->config, $this->dn);
101 $ldap->cat($this->dn);
102 if(count($ldap->fetch())>0){
103 $this->orig_dn= $this->dn;
104 $this->is_account=true;
105 $this->initially_was_account = true;
106 }else{
107 $this->orig_dn = "new";
108 $this->is_account=false;
109 $this->initially_was_account = false;
110 }
111 }
113 /* Prepare different member types
114 */
115 foreach(array("AddUser" =>"gotoUserPrinter",
116 "AddGroup" =>"gotoGroupPrinter",
117 "AddAdminUser" =>"gotoUserAdminPrinter",
118 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
120 /* $this->members contains all members */
121 $this->member[$type]=array();
123 if (isset($this->attrs[$attr]['count'])) {
124 unset($this->attrs[$attr]['count']);
125 }
127 if(isset($this->attrs[$attr])){
128 foreach($this->attrs[$attr] as $mem){
129 if(preg_match("/Group/",$type)){
130 $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
131 $entry = $ldap->fetch();
132 if(isset($entry['description'])){
133 $this->member[$type][$entry['cn'][0]]=$entry;
134 }
135 }else{
136 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
137 $entry = $ldap->fetch();
138 if(isset($entry['uid'])){
139 $this->member[$type][$entry['uid'][0]]=$entry;
140 }
141 }
142 }
143 }
144 }
146 $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
147 if($attrs = $ldap->fetch()){
148 if(in_array("gotoTerminal",$attrs['objectClass'])){
149 $this->is_terminalBased = "Terminal";
150 }else{
151 $this->is_terminalBased = "Workstation";
152 }
153 }
155 /* A printer was only saved if the printer belongs to a workstation.
156 * If there was a new single printer created, it was never saved.
157 * -This allows new printers to be saved.
158 */
159 if($this->dn == "new"){
160 $this->is_account = true;
161 }
162 }
164 function execute()
165 {
166 /* Call parent execute */
167 plugin::execute();
169 $smarty= get_smarty();
170 $display="";
171 /* Template management.
172 * There are two ways to call this tab.
173 * 1. From a printer Dialog, here we will see the full template, without a toggle state button
174 * 2. As a terminal tab, here we hide the top (name,base,description) of the template.
175 * Toggle Account state will be shown in this case, to disable or enable this tab.
176 *
177 * $this->is_terminal indecates this two different types.
178 */
179 if($this->is_terminal){
180 $smarty->assign("is_terminal","true");
181 }else{
182 $smarty->assign("is_terminal","false");
183 }
185 /* Do we need to flip is_account state? */
186 if (isset($_POST['modify_state'])){
187 $this->is_modified = true;
188 $this->is_account= !$this->is_account;
189 }
191 if($this->is_terminal){
192 if(empty($this->cn)){
193 if(!in_array("gotoPrinter",$this->objectclasses)){
194 $display= $this->show_header(_("Add printer extension"),
195 _("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);
196 $this->is_account= false;
197 return $display;
198 }else{
199 $smarty->assign("is_terminal","false");
200 $skip = true;
201 }
202 }
204 if(!isset($skip)){
205 if (($this->is_account)){
206 if($this->type=="station"){
207 $display= $this->show_header(_("Remove printer extension"),
208 _("This workstation has printer extension enabled.You can disable it by clicking below."));
209 }else{
210 $display= $this->show_header(_("Remove printer extension"),
211 _("This terminal has printer extension enabled. You can disable it by clicking below."));
212 }
213 }else{
214 if($this->type=="station"){
215 $display= $this->show_header(_("Add printer extension"),
216 _("This workstation has printer extension disabled. You can enable it by clicking below."));
217 }else{
218 $display= $this->show_header(_("Add printer extension"),
219 _("This terminal has printer extension disabled. You can enable it by clicking below."));
220 }
221 return ($display);
222 }
223 }
224 }
227 /* Do we represent a valid printer? */
228 if (!$this->is_account && $this->parent == NULL){
229 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
230 _("This 'dn' has no printer features.")."</b>";
231 return($display);
232 }
234 /* Fill templating stuff */
235 $smarty->assign("bases", $this->config->idepartments);
236 $smarty->assign("base_select", $this->base);
238 /* Assign attributes */
239 foreach ($this->attributes as $attr){
240 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
241 $smarty->assign("$attr", $this->$attr);
242 }
244 if(isset($_POST['AddUser'])){
245 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
246 }
247 if(isset($_POST['AddGroup'])){
248 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
249 }
250 if(isset($_POST['AddAdminUser'])){
251 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
252 }
253 if(isset($_POST['AddAdminGroup'])){
254 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
255 }
257 if(isset($_POST['EditDriver'])){
258 if($this->PPDdialogToSave){
259 $this->dialog = $this->PPDdialogToSave;
260 }else{
261 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
262 }
263 }
265 if(isset($_POST['PrinterCancel'])){
266 unset($this->dialog);
267 $this->dialog= NULL;
268 }
270 if(isset($_POST['RemoveDriver'])){
271 $this->gotoPrinterPPD = array();
272 $this->PPDdialogToSave = NULL;
273 }
275 if(isset($_POST['SavePPD'])){
276 $this->dialog->save_object();
277 if(count($this->dialog->check())){
278 foreach($this->dialog->check() as $msg){
279 print_red($msg);
280 }
281 }else{
282 $this->gotoPrinterPPD = array();
284 $this->gotoPrinterPPD = $this->dialog->save();
285 $this->PPDdialogToSave = $this->dialog;
286 unset($this->dialog);
287 $this->dialog=NULL;
288 }
290 }
292 if(isset($_POST['ClosePPD'])){
293 unset($this->dialog);
294 $this->dialog=NULL;
295 }
299 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
300 if(isset($this->member['AddUser'][$_POST['UserMember']])){
301 unset($this->member['AddUser'][$_POST['UserMember']]);
302 }
303 }
305 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
306 if(isset($this->member['AddGroup'][$_POST['UserMember']])){
307 unset($this->member['AddGroup'][$_POST['UserMember']]);
308 }
309 }
311 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
312 if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
313 unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
314 }
315 }
317 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
318 if(isset($this->member['AddAdminGroup'][$_POST['AdminMember']])){
319 unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
320 }
321 }
323 if(isset($_POST['PrinterSave'])){
324 $this->dialog->save_object();
325 if(count($this->dialog->check())){
326 foreach($this->dialog->check() as $msg){
327 print_red($msg);
328 }
329 }else{
330 $new = $this->dialog->save();
331 $data = $new;
332 unset($data['type']);
334 if(preg_match("/User/",$new['type'])){
335 $use = "uid";
336 }else{
337 $use = "cn";
338 }
340 foreach($data as $mem){
341 $this->member[$new['type']][$mem[$use][0]]=$mem;
342 }
343 unset($this->dialog);
344 $this->dialog=NULL;
345 }
346 }
348 if($this->dialog != NULL){
349 $this->dialog->save_object();
350 $display = $this->dialog->execute();
351 return $display;
352 }
354 /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
355 $smarty->assign("staticAddress", "");
358 require_once ("class_ppdManager.inc");
360 if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
361 $ppdManager= new ppdManager($_SESSION['config']->data['MAIN']['PPD_PATH']);
362 if(!empty($this->gotoPrinterPPD)){
363 if((!file_exists($_SESSION['config']->data['MAIN']['PPD_PATH'].$this->gotoPrinterPPD))){
364 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$this->gotoPrinterPPD))."</b>";
365 }else{
366 $smarty->assign("driverInfo", $ppdManager->loadDescription($_SESSION['config']->data['MAIN']['PPD_PATH'].$this->gotoPrinterPPD));
367 }
368 }else{
369 $smarty->assign("driverInfo", _("not defined"));
370 }
371 }else{
372 $smarty->assign("driverInfo",_("can't get ppd informations."));
373 }
376 $list=$this->generateList();
377 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
378 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
380 asort($userlist);
381 asort($adminlist);
382 if($this->is_terminalBased){
383 if($this->is_terminalBased == "Terminal"){
384 $smarty->assign("desc" ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
385 }else{
386 $smarty->assign("desc" ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
387 }
388 $smarty->assign("cnACL" ," disabled ");
389 }else{
390 $smarty->assign("desc" ,"");
391 }
392 $smarty->assign("UserMember" ,$this->UserMember);
393 $smarty->assign("UserMembers" ,$userlist);
394 $smarty->assign("UserMemberKeys",array_flip($userlist));
396 $smarty->assign("AdminMember" ,$this->AdminMember);
397 $smarty->assign("AdminMembers" ,$adminlist);
398 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
399 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
401 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
402 }
404 function remove_from_parent()
405 {
406 $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
407 $ldap= $this->config->get_ldap_link();
408 $ldap->rmdir($this->dn);
409 show_ldap_error($ldap->get_error());
410 $this->handle_post_events("remove");
412 /* Delete references to object groups */
413 $ldap->cd ($this->config->current['BASE']);
414 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
415 while ($ldap->fetch()){
416 $og= new ogroup($this->config, $ldap->getDN());
417 unset($og->member[$this->dn]);
418 $og->save ();
419 }
420 }
422 /* Save data to object */
423 function save_object()
424 {
425 plugin::save_object();
427 /* Save base, since this is no LDAP attribute */
428 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
429 $this->base= $_POST['base'];
430 }
431 }
433 /* Check supplied data */
434 function check()
435 {
436 $message= array();
437 $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
439 /* must: cn */
440 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
441 $message[]= "The required field 'Printer name' is not set.";
442 }
444 $ui= get_userinfo();
445 $acl= get_permissions ($this->dn, $ui->subtreeACL);
446 $acl= get_module_permission($acl, "printer", $this->dn);
447 if (chkacl($acl, "create") != ""){
448 $message[]= _("You have no permissions to create a printer on this 'Base'.");
449 }
451 if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
452 $ldap= $this->config->get_ldap_link();
453 $ldap->cd ($this->base);
454 $ldap->search ("(cn=".$this->cn.")", array("cn"));
455 if ($ldap->count() != 0){
456 while ($attrs= $ldap->fetch()){
457 if ($attrs['dn'] != $this->orig_dn){
458 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
459 break;
460 }
461 }
462 }
463 }
465 return ($message);
466 }
469 /* Save to LDAP */
470 function save()
471 {
472 if (!$this->is_account){
473 return;
474 }
476 if($this->PPDdialogToSave){
477 $this->PPDdialogToSave->save_ppd();
478 }
479 $dn= $this->dn;
480 plugin::save();
481 $ldap= $this->config->get_ldap_link();
483 if((in_array("gotoTerminal",$this->attrs['objectClass']))){
484 $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
485 }
487 if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
488 $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
489 }
491 /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn
492 */
493 if($this->is_terminal){
495 /* reduce objectClasses to minimun */
496 $this->attrs['objectClass']= $this->objectclasses;
498 /* If a printer with the given dn exists, modify else create new one */
499 $ldap->cat($this->dn);
500 if($ldap->fetch()){
501 $this->orig_dn= $this->dn;
502 }else{
503 $this->orig_dn = "new";
504 }
505 }
507 /* Remove all empty values */
508 if ($this->orig_dn == 'new'){
509 $attrs= array();
510 foreach ($this->attrs as $key => $val){
511 if (is_array($val) && count($val) == 0){
512 continue;
513 }
514 $attrs[$key]= $val;
515 }
516 $this->attrs= $attrs;
517 }
519 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
520 $method="https://";
521 }else{
522 $method="http://";
523 }
525 if(!is_array($this->attrs['gotoPrinterPPD'])) {
526 $this->attrs['gotoPrinterPPD'] = $this->attrs['gotoPrinterPPD'];
527 $this->attrs['gotoPrinterPPD'] = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->attrs['gotoPrinterPPD']);
528 }
530 /* Append printer user
531 */
532 foreach($this->member['AddUser'] as $mem){
533 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
534 }
536 /* Append printer group
537 */
538 foreach($this->member['AddGroup'] as $mem){
539 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
540 }
542 /* Append printer admin user
543 */
544 foreach($this->member['AddAdminUser'] as $mem){
545 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
546 }
548 /* Append printer admin group
549 */
550 foreach($this->member['AddAdminGroup'] as $mem){
551 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
552 }
554 /* Write back to ldap */
555 $ldap= $this->config->get_ldap_link();
556 if ($this->orig_dn == 'new'){
557 $ldap->cd($this->config->current['BASE']);
558 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
559 $ldap->cd($this->dn);
560 $ldap->add($this->attrs);
561 $this->handle_post_events("add");
562 } else {
563 if ($this->orig_dn != $this->dn){
564 $this->move($this->orig_dn, $this->dn);
565 }
567 $ldap->cd($this->dn);
568 $ldap->modify($this->attrs);
569 $this->handle_post_events("modify");
570 }
571 show_ldap_error($ldap->get_error());
573 /* Optionally execute a command after we're done */
574 $this->postcreate();
575 }
577 function generateList(){
578 $a_return=array();
579 foreach($this->member as $type => $values){
580 $a_return[$type]=array();
581 foreach($values as $value){
582 if((preg_match("/Group/i",$type))){
583 if(!isset($value['description'])){
584 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
585 }else{
586 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
587 }
588 }else{
589 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
590 }
591 }
592 }
593 return($a_return);
594 }
595 }
599 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
600 ?>