8c794ba4b64d11db22a0d047dedd6a1442b9e023
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 $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
355 }else{
356 $smarty->assign("driverInfo", _("not defined"));
357 }
359 $list=$this->generateList();
360 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
361 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
363 asort($userlist);
364 asort($adminlist);
365 if($this->is_terminalBased){
366 if($this->is_terminalBased == "Terminal"){
367 $smarty->assign("desc" ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
368 }else{
369 $smarty->assign("desc" ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
370 }
371 $smarty->assign("cnACL" ," disabled ");
372 }else{
373 $smarty->assign("desc" ,"");
374 }
375 $smarty->assign("UserMember" ,$this->UserMember);
376 $smarty->assign("UserMembers" ,$userlist);
377 $smarty->assign("UserMemberKeys",array_flip($userlist));
379 $smarty->assign("AdminMember" ,$this->AdminMember);
380 $smarty->assign("AdminMembers" ,$adminlist);
381 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
382 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
384 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
385 }
387 function remove_from_parent()
388 {
389 $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
390 $ldap= $this->config->get_ldap_link();
391 $ldap->rmdir($this->dn);
392 show_ldap_error($ldap->get_error());
393 $this->handle_post_events("remove");
395 /* Delete references to object groups */
396 $ldap->cd ($this->config->current['BASE']);
397 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
398 while ($ldap->fetch()){
399 $og= new ogroup($this->config, $ldap->getDN());
400 unset($og->member[$this->dn]);
401 $og->save ();
402 }
403 }
405 /* Save data to object */
406 function save_object()
407 {
408 plugin::save_object();
410 /* Save base, since this is no LDAP attribute */
411 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
412 $this->base= $_POST['base'];
413 }
414 }
416 /* Check supplied data */
417 function check()
418 {
419 $message= array();
420 $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
422 /* must: cn */
423 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
424 $message[]= "The required field 'Printer name' is not set.";
425 }
427 $ui= get_userinfo();
428 $acl= get_permissions ($this->dn, $ui->subtreeACL);
429 $acl= get_module_permission($acl, "printer", $this->dn);
430 if (chkacl($acl, "create") != ""){
431 $message[]= _("You have no permissions to create a printer on this 'Base'.");
432 }
434 if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
435 $ldap= $this->config->get_ldap_link();
436 $ldap->cd ($this->base);
437 $ldap->search ("(cn=".$this->cn.")", array("cn"));
438 if ($ldap->count() != 0){
439 while ($attrs= $ldap->fetch()){
440 if ($attrs['dn'] != $this->orig_dn){
441 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
442 break;
443 }
444 }
445 }
446 }
448 return ($message);
449 }
452 /* Save to LDAP */
453 function save()
454 {
455 if (!$this->is_account){
456 return;
457 }
459 if($this->PPDdialogToSave){
460 $this->PPDdialogToSave->save_ppd();
461 }
463 $dn= $this->dn;
464 plugin::save();
465 $ldap= $this->config->get_ldap_link();
467 if((in_array("gotoTerminal",$this->attrs['objectClass']))){
468 $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
469 }
471 if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
472 $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
473 }
475 /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn
476 */
477 if($this->is_terminal){
479 /* reduce objectClasses to minimun */
480 $this->attrs['objectClass']= $this->objectclasses;
482 /* If a printer with the given dn exists, modify else create new one */
483 $ldap->cat($this->dn);
484 if($ldap->fetch()){
485 $this->orig_dn= $this->dn;
486 }else{
487 $this->orig_dn = "new";
488 }
489 }
491 /* Remove all empty values */
492 if ($this->orig_dn == 'new'){
493 $attrs= array();
494 foreach ($this->attrs as $key => $val){
495 if (is_array($val) && count($val) == 0){
496 continue;
497 }
498 $attrs[$key]= $val;
499 }
500 $this->attrs= $attrs;
501 }
504 /* Append printer user
505 */
506 foreach($this->member['AddUser'] as $mem){
507 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
508 }
510 /* Append printer group
511 */
512 foreach($this->member['AddGroup'] as $mem){
513 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
514 }
516 /* Append printer admin user
517 */
518 foreach($this->member['AddAdminUser'] as $mem){
519 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
520 }
522 /* Append printer admin group
523 */
524 foreach($this->member['AddAdminGroup'] as $mem){
525 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
526 }
528 /* Write back to ldap */
529 $ldap= $this->config->get_ldap_link();
530 if ($this->orig_dn == 'new'){
531 $ldap->cd($this->config->current['BASE']);
532 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
533 $ldap->cd($this->dn);
534 $ldap->add($this->attrs);
535 $this->handle_post_events("add");
536 } else {
537 if ($this->orig_dn != $this->dn){
538 $this->move($this->orig_dn, $this->dn);
539 }
541 $ldap->cd($this->dn);
542 $ldap->modify($this->attrs);
543 $this->handle_post_events("modify");
544 }
545 show_ldap_error($ldap->get_error());
547 /* Optionally execute a command after we're done */
548 $this->postcreate();
549 }
551 function generateList(){
552 $a_return=array();
553 foreach($this->member as $type => $values){
554 $a_return[$type]=array();
555 foreach($values as $value){
556 if((preg_match("/Group/i",$type))){
557 if(!isset($value['description'])){
558 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
559 }else{
560 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
561 }
562 }else{
563 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
564 }
565 }
566 }
567 return($a_return);
568 }
569 }
573 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
574 ?>