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 $initial_PPD = "";
22 var $orig_dn = "";
24 var $UserMember ="";
25 var $UserMembers =array();
26 var $UserMemberKeys =array();
28 var $AdminMember ="";
29 var $AdminMembers =array();
30 var $AdminMemberKeys =array();
32 var $PPDdialogToSave = NULL;
33 var $BelongsTo = "unknown"; // Specifies if this is a standalone printer, or belongs to a terminal / WS
35 var $member =array();
36 var $strings = "";
37 var $dialog =NULL;
39 var $netConfigDNS;
40 var $baseSelection = false;
41 var $macAddress = "";
43 var $gotoUserAdminPrinter;
44 var $gotoGroupAdminPrinter ;
45 var $gotoGroupPrinter;
46 var $gotoUserPrinter ;
48 /* attribute list for save action */
49 var $attributes = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress",
50 "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter");
51 var $objectclasses = array("top", "gotoPrinter");
52 var $view_logged = FALSE;
53 var $parent;
55 function printgeneric (&$config, $dn,$parent_init,$parent)
56 {
57 $this->config = &$config;
58 $this->dn = $dn;
60 /* If parent was posted(the tabs object) we can detect the printer type. */
61 if($parent){
62 $this->parent = $parent;
63 $this->getTypeOfPrinter();
64 }else{
65 $this->BelongsTo = "unknown";
66 return;
67 }
69 /* Update dn, to ensure storing as printer instead of WS / terminal */
70 if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
71 $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
72 }
74 if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
75 $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
76 }
78 $this->orig_dn = $this->dn;
80 /* Get printer settings, possibly dn has changed */
81 plugin::plugin ($config, $this->dn);
83 /* Get is_account initially_was_account status */
84 $this->getTypeOfPrinter(true);
86 /* set orig dn to new if object is new */
87 $ldap= $this->config->get_ldap_link();
88 $ldap->cat($this->dn, array('dn'));
89 if(!$ldap->count()){
90 $this->orig_dn = "new";
91 }
93 /* create dns object */
94 $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
96 /* Set base */
97 if ($this->dn == "new"){
98 $ui= get_userinfo();
99 $this->base= dn2base($ui->dn);
100 $this->cn= "";
101 } else {
103 /* Set base and check if the extracted base exists */
104 if(preg_match("/ou=incoming,/",$this->dn)){
105 $this->base= preg_replace("/ou=incoming,/","",dn2base($this->dn));
106 }else{
107 $this->base= preg_replace("/ou=printers,ou=systems,/","",dn2base($this->dn));
108 }
110 if(!isset($this->config->idepartments[$this->base])){
111 print_red(_("Can't extract a valid base out of object dn, setting base to '%s'."),$_SESSION['CurrentMainBase']);
112 $this->base = $_SESSION['CurrentMainBase'];
113 }
114 }
116 /* Extract selected ppd */
117 if(isset($this->gotoPrinterPPD)){
118 $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
119 }
121 $this->initial_PPD = $this->gotoPrinterPPD;
123 /* Prepare different member types */
124 foreach(array("AddUser" =>"gotoUserPrinter",
125 "AddGroup" =>"gotoGroupPrinter",
126 "AddAdminUser" =>"gotoUserAdminPrinter",
127 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
129 /* $this->members contains all members */
130 $this->member[$type]=array();
132 if (isset($this->attrs[$attr]['count'])) {
133 unset($this->attrs[$attr]['count']);
134 }
136 if(isset($this->attrs[$attr])){
137 foreach($this->attrs[$attr] as $mem){
138 if(preg_match("/Group/",$type)){
139 $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
140 if($ldap->count()){
141 $entry = $ldap->fetch();
142 $this->member[$type][$entry['cn'][0]]=$entry;
143 }
144 }else{
145 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
146 if($ldap->count()){
147 $entry = $ldap->fetch();
148 $this->member[$type][$entry['uid'][0]]=$entry;
149 }
150 }
151 }
152 }
153 }
154 }
156 function set_acl_base($base)
157 {
158 plugin::set_acl_base($base);
159 if(is_object($this->netConfigDNS)){
160 $this->netConfigDNS->set_acl_base($base);
161 }
162 }
164 function set_acl_category($cat)
165 {
166 plugin::set_acl_category($cat);
167 if(is_object($this->netConfigDNS)){
168 $this->netConfigDNS->set_acl_category($cat);
169 }
170 }
172 /* Detect type of printer.
173 * Printer can be stand alone, belong to a workstation or belong to a terminal.
174 * We can detect the type printer type when comparing the tabs objects
175 */
176 function getTypeOfPrinter($UpdateAccountStatus = false)
177 {
178 /* Disable account as default
179 */
180 $this->is_account = $this->initially_was_account = false;
182 /* Detect type of printer via parent tabs.
183 */
185 $class = get_class($this->parent);
186 if(isset($this->parent->by_object['workgeneric'])){
188 /* Exclude templates
189 */
190 $this->cn = $this->parent->by_object['workgeneric']->cn;
191 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
192 $this->BelongsTo = "WorkstationTemplate";
193 }else{
194 $this->BelongsTo = "Workstation";
195 }
196 }elseif(isset($this->parent->by_object['termgeneric'])){
198 /* Exclude templates
199 */
200 $this->cn = $this->parent->by_object['termgeneric']->cn;
201 if($this->parent->by_object['termgeneric']->cn == "default"){
202 $this->BelongsTo = "TerminalTemplate";
203 }else{
204 $this->BelongsTo = "Terminal";
205 }
206 }elseif(isset($this->parent->by_name['printgeneric'])){
207 $this->BelongsTo = "Printer";
208 }
210 if($UpdateAccountStatus){
212 /* Set is_account / was account
213 */
214 if($this->dn == "new"){
215 $this->initially_was_account = false;
216 }
218 /* If is printer it must be a true account.
219 */
220 if(preg_match("/printer/i",$this->BelongsTo)){
221 $this->is_account = true;
222 }
224 /* Update dn, to ensure storing as printer instead of WS / terminal
225 */
226 if(preg_match("/terminal/i",$this->BelongsTo)){
227 $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
228 }
230 if(preg_match("/workstation/i",$this->BelongsTo)){
231 $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
232 }
234 /* Detect if this is a valid printer account;
235 */
236 $ldap = $this->config->get_ldap_link();
237 $ldap->cat($this->dn, array('objectClass'));
239 if($ldap->count()){
240 $attrs = $ldap->fetch();
241 if(in_array("gotoPrinter",$attrs['objectClass'])){
242 $this->initially_was_account = true;
243 $this->is_account = true;
244 }else{
245 $this->is_account = false;
246 }
247 }
248 }
249 }
251 function execute()
252 {
253 /* Call parent execute */
254 plugin::execute();
256 if($this->is_account && !$this->view_logged){
257 $this->view_logged = TRUE;
258 new log("view","printer/".get_class($this),$this->dn);
259 }
262 /* If type of printer couldn't be detected (because of missing parent object in construction)
263 * hide this tab.
264 */
265 if(preg_match("/unknown/i",$this->BelongsTo)){
266 $display= $this->show_enable_header(_("Add printer extension"),
267 _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
268 return($display);
269 }
271 /* Templates can't have printer extensions
272 */
273 if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
274 $display= $this->show_enable_header(_("Add printer extension"),
275 _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
276 return($display);
277 }
278 if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
279 $display= $this->show_enable_header(_("Add printer extension"),
280 _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
281 return($display);
282 }
284 /* Get cn from base object */
285 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
286 $this->cn = $this->parent->by_object['workgeneric']->cn;
287 }
288 if(preg_match("/^Terminal$/i",$this->BelongsTo)){
289 $this->cn = $this->parent->by_object['termgeneric']->cn;
290 }
292 $smarty= get_smarty();
294 /* Assign acls */
295 $tmp = $this->plInfo();
296 foreach($tmp['plProvidedAcls'] as $name => $translation){
297 $smarty->assign($name."ACL", $this->getacl($name));
298 }
300 $display="";
302 /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
303 if(preg_match("/^Printer$/i",$this->BelongsTo)){
304 $smarty->assign("StandAlone",true);
305 }else{
306 $smarty->assign("StandAlone",false);
307 }
309 /* Do we need to flip is_account state? */
310 if(isset($_POST['modify_state'])){
311 if($this->is_account && $this->acl_is_removeable()){
312 $this->is_account= FALSE;
313 }elseif(!$this->is_account && $this->acl_is_createable()){
314 $this->is_account= TRUE;
315 }
316 }
318 /* Do we represent a valid printer? */
319 if (!$this->is_account && $this->parent === NULL){
320 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
321 _("This 'dn' has no printer features.")."</b>";
322 return($display);
323 }
325 /* If this is a WS / Terminal depending printer, display account state button */
326 if(!preg_match("/^Printer$/i",$this->BelongsTo)){
327 if((empty($this->cn)) && ($this->dn != "new")){
328 $display= $this->show_enable_header(_("Add printer extension"),
329 _("This object 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);
330 $this->is_account= false;
331 return $display;
332 }
334 if (($this->is_account)){
335 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
336 $display= $this->show_disable_header(_("Remove printer extension"),
337 _("This workstation has printer extension enabled.You can disable it by clicking below."));
338 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
339 $display= $this->show_disable_header(_("Remove printer extension"),
340 _("This terminal has printer extension enabled. You can disable it by clicking below."));
341 }
342 }else{
343 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
344 $display= $this->show_disable_header(_("Add printer extension"),
345 _("This workstation has printer extension disabled. You can enable it by clicking below."));
346 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
347 $display= $this->show_enable_header(_("Add printer extension"),
348 _("This terminal has printer extension disabled. You can enable it by clicking below."));
349 }
350 return ($display);
351 }
352 }
354 /* Base select dialog */
355 $once = true;
356 foreach($_POST as $name => $value){
357 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
358 $once = false;
359 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
360 $this->dialog->setCurrentBase($this->base);
361 $this->baseSelection = true;
362 }
363 }
365 /* Dialog handling for base select dialog
366 * Check if base was selected, dialog aborted etc */
367 if(is_object($this->dialog)){
369 $this->dialog->save_object();
370 if($this->baseSelection){
371 if($this->dialog->isClosed()){
372 $this->dialog = false;
373 $this->baseSelection = false;
374 }elseif($this->dialog->isSelected()){
376 /* A new base was selected, check if it is a valid one */
377 $tmp = $this->get_allowed_bases();
378 if(isset($tmp[$this->dialog->isSelected()])){
379 $this->base = $this->dialog->isSelected();
380 }
382 $this->dialog= false;
383 $this->baseSelection = false;
384 }else{
385 return($this->dialog->execute());
386 }
387 }
388 }
390 /* Fill templating stuff */
391 $smarty->assign("bases", $this->get_allowed_bases());
392 $smarty->assign("base_select", $this->base);
394 /* Assign attributes */
395 foreach ($this->attributes as $attr){
396 $smarty->assign("$attr", $this->$attr);
397 }
399 if(isset($_POST['AddUser'])){
400 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
401 }
402 if(isset($_POST['AddGroup'])){
403 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
404 }
405 if(isset($_POST['AddAdminUser'])){
406 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
407 }
408 if(isset($_POST['AddAdminGroup'])){
409 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
410 }
412 /* Display ppd configure/select dialog */
413 if(isset($_POST['EditDriver'])){
414 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
415 $this->dialog = $this->PPDdialogToSave;
416 }else{
417 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
418 $this->dialog->cn= $this->cn;
419 }
420 }
422 /* remove ppd */
423 if(isset($_POST['RemoveDriver'])){
424 $this->gotoPrinterPPD = array();
425 $this->PPDdialogToSave = NULL;
426 }
428 /* Close ppd dialog */
429 if(isset($_POST['ClosePPD'])){
430 unset($this->dialog);
431 $this->dialog=NULL;
432 }
434 /* Save selected ppd */
435 if(isset($_POST['SavePPD'])){
436 $this->dialog->save_object();
437 if(count($this->dialog->check())){
438 foreach($this->dialog->check() as $msg){
439 print_red($msg);
440 }
441 }else{
442 $this->gotoPrinterPPD = array();
443 $this->gotoPrinterPPD = $this->dialog->save();
444 $this->PPDdialogToSave = $this->dialog;
445 unset($this->dialog);
446 $this->dialog=NULL;
447 }
448 }
450 /* Member management, delete user / group / admin ..*/
451 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
452 foreach($_POST['UserMember'] as $mem){
453 $this->DelMember('AddUser',$mem);
454 }
455 }
457 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
458 foreach($_POST['UserMember'] as $mem){
459 $this->DelMember('AddGroup',$mem);
460 }
461 }
463 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
464 foreach($_POST['AdminMember'] as $mem){
465 $this->DelMember('AddAdminUser',$mem);
466 }
467 }
469 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
470 foreach($_POST['AdminMember'] as $mem){
471 $this->DelMember('AddAdminGroup',$mem);
472 }
473 }
475 /* Abort user / group adding dialog */
476 if(isset($_POST['PrinterCancel'])){
477 unset($this->dialog);
478 $this->dialog= NULL;
479 }
481 /* Save selected users / groups */
482 if(isset($_POST['PrinterSave'])){
483 $this->dialog->save_object();
484 if(count($this->dialog->check())){
485 foreach($this->dialog->check() as $msg){
486 print_red($msg);
487 }
488 }else{
489 $data= $new = $this->dialog->save();
490 unset($data['type']);
491 foreach($data as $mem){
492 $this->AddMember($new['type'], $mem['dn']);
493 }
494 unset($this->dialog);
495 $this->dialog=NULL;
496 }
497 }
499 /* Display dialog, if there is currently one open*/
500 if($this->dialog !== NULL){
501 $this->dialog->save_object();
502 $display = $this->dialog->execute();
503 return $display;
504 }
506 /* Parse selected ppd file */
507 if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
509 $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
510 if(!preg_match("/\/$/",$path)){
511 $path = $path."/";
512 }
514 $ppdManager= new ppdManager($path);
515 if(!empty($this->gotoPrinterPPD)){
516 if((!file_exists($path.$this->gotoPrinterPPD))){
517 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
518 }else{
519 $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
520 }
521 }else{
522 $smarty->assign("driverInfo", _("Not defined"));
523 }
524 }else{
525 $smarty->assign("driverInfo",_("Can't get ppd informations."));
526 }
528 /* Create user & admin user list */
529 $list=$this->generateList();
530 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
531 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
533 asort($userlist);
534 asort($adminlist);
536 if(!preg_match("/Printer/i",$this->BelongsTo)){
537 if(preg_match("/Terminal/i",$this->BelongsTo)){
538 $smarty->assign("desc" ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
539 }else{
540 $smarty->assign("desc" ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
541 }
542 $smarty->assign("cnACL" , $this->getacl("cn",true));
543 }else{
544 $smarty->assign("desc" ,"");
545 }
546 $smarty->assign("UserMember" ,$this->UserMember);
547 $smarty->assign("UserMembers" ,$userlist);
548 $smarty->assign("UserMemberKeys",array_flip($userlist));
550 $smarty->assign("AdminMember" ,$this->AdminMember);
551 $smarty->assign("AdminMembers" ,$adminlist);
552 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
553 if(preg_match("/Printer/i",$this->BelongsTo)){
554 $smarty->assign("netconfig", $this->netConfigDNS->execute());
555 } else {
556 $smarty->assign("netconfig", "");
557 }
559 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
560 }
562 function remove_from_parent()
563 {
564 /* Only remove if there was initially an account */
565 if($this->initially_was_account){
567 /* Update dn, to ensure storing as printer instead of WS / terminal
568 */
569 if(preg_match("/terminal/i",$this->BelongsTo)){
570 $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
571 }
573 if(preg_match("/workstation/i",$this->BelongsTo)){
574 $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
575 }
577 /* Check if this dn points to a printer, to avoid deleting something else */
578 $ldap= $this->config->get_ldap_link();
579 $ldap->cat($this->dn, array('dn',"objectClass"));
580 if(!$ldap->count()){
581 print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
582 return;
583 }
585 /* Check if obejct is a printer */
586 $CheckPrinter = $ldap->fetch();
587 if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
588 print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
589 return;
590 }
592 /* Remove account & dns extension */
593 $this->netConfigDNS->remove_from_parent();
594 $ldap->rmdir($this->dn);
596 new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
598 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system print/generic with dn '%s' failed."),$this->dn));
599 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
601 /* Delete references to object groups */
602 $ldap->cd ($this->config->current['BASE']);
603 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
604 while ($ldap->fetch()){
605 $og= new ogroup($this->config, $ldap->getDN());
606 unset($og->member[$this->dn]);
607 $og->save ();
608 }
610 /* Remove previously selected ppd file.*/
611 if(!empty($this->initial_PPD)){
612 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
613 $tmp->removeModifiedPPD();
614 }
615 }
616 }
619 /* Save data to object */
620 function save_object()
621 {
622 /* Create a base backup and reset the
623 base directly after calling plugin::save_object();
624 Base will be set seperatly a few lines below */
625 $base_tmp = $this->base;
626 plugin::save_object();
627 $this->base = $base_tmp;
629 if(is_object($this->netConfigDNS)){
630 $this->netConfigDNS->save_object();
631 }
633 /* Set new base if allowed */
634 $tmp = $this->get_allowed_bases();
635 if(isset($_POST['base'])){
636 if(isset($tmp[$_POST['base']])){
637 $this->base= $_POST['base'];
638 }
639 }
640 }
642 /* Check supplied data */
643 function check()
644 {
645 /* Call common method to give check the hook */
646 $message= plugin::check();
647 if (preg_match("/printer/i",$this->BelongsTo)){
648 $message= array_merge($message, $this->netConfigDNS->check());
649 }
651 /* Don't display check messages if this is a template object */
652 if(isset($this->parent->by_object['workgeneric'])){
653 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
654 return $message;
655 }
656 }elseif(isset($this->parent->by_object['termgeneric'])){
657 if($this->parent->by_object['termgeneric']->cn == "default"){
658 return $message;
659 }
660 }
662 $dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
664 /* must: cn */
665 if(preg_match("/printer/i",$this->BelongsTo) && (empty($this->cn))){
666 $message[]= "The required field 'Printer name' is not set.";
667 }
669 /* must: labeledURI */
670 if(empty($this->labeledURI)){
671 $message[]= "The required field 'Printer URL' is not set.";
672 }
674 /* Check if there is already an entry with this cn*/
675 if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
676 $ldap= $this->config->get_ldap_link();
677 $ldap->cd ($this->base);
678 $ldap->ls("(cn=".$this->cn.")","ou=printers,ou=systems,".$this->base, array("cn"));
679 if ($ldap->count() != 0){
680 while ($attrs= $ldap->fetch()){
681 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
682 continue;
683 }
684 if ($attrs['dn'] != $this->orig_dn){
685 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
686 break;
687 }
688 }
689 }
690 }
692 return ($message);
693 }
696 /* Save to LDAP */
697 function save()
698 {
699 /* Update dn, to ensure storing as printer instead of WS / terminal
700 */
701 if(preg_match("/terminal/i",$this->BelongsTo)){
702 $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
703 }
705 if(preg_match("/workstation/i",$this->BelongsTo)){
706 $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
707 }
709 if(!$this->is_account) return;
710 if(isset($this->parent->by_object['workgeneric'])){
711 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
712 return;
713 }
714 }elseif(isset($this->parent->by_object['termgeneric'])){
715 if($this->parent->by_object['termgeneric']->cn == "default"){
716 return;
717 }
718 }
720 /* If type is still unknown, the initialisation of this printer failed, abort. */
721 if(preg_match("/unknown/i",$this->BelongsTo)){
722 return;
723 }
725 /* save ppd configuration */
726 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
727 $this->PPDdialogToSave->save_ppd();
728 }
730 /* Remove previously selected ppd file.*/
731 if($this->initial_PPD != $this->gotoPrinterPPD){
732 if(!empty($this->initial_PPD)){
733 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
734 $tmp->removeModifiedPPD();
735 }
736 }
738 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
739 $method="https://";
740 }else{
741 $method="http://";
742 }
744 /* If no ppd is selected, remove this attribute */
745 if(!empty($this->gotoPrinterPPD)) {
746 $this->gotoPrinterPPD = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
747 }else{
748 $this->gotoPrinterPPD = array();
749 }
751 $dn= $this->dn;
752 plugin::save();
753 $ldap= $this->config->get_ldap_link();
755 /* reduce objectClasses to minimun */
756 $this->attrs['objectClass']= $this->objectclasses;
758 /* Remove all empty values */
759 if ($this->orig_dn == 'new'){
760 $attrs= array();
761 foreach ($this->attrs as $key => $val){
762 if (is_array($val) && count($val) == 0){
763 continue;
764 }
765 $attrs[$key]= $val;
766 }
767 $this->attrs= $attrs;
768 }
770 /* Append printer user
771 */
772 $this->attrs['gotoUserPrinter']=array();
773 foreach($this->member['AddUser'] as $mem){
774 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
775 }
777 /* Append printer group
778 */
779 $this->attrs['gotoGroupPrinter'] = array();
780 foreach($this->member['AddGroup'] as $mem){
781 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
782 }
784 /* Append printer admin user
785 */
786 $this->attrs['gotoUserAdminPrinter'] = array();
787 foreach($this->member['AddAdminUser'] as $mem){
788 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
789 }
791 /* Append printer admin group
792 */
793 $this->attrs['gotoGroupAdminPrinter']= array();
794 foreach($this->member['AddAdminGroup'] as $mem){
795 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
796 }
798 if($this->orig_dn == 'new'){
799 foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
800 if(count($this->attrs[$checkVar]) == 0 || empty($this->attrs[$checkVar])){
801 unset($this->attrs[$checkVar]);
802 }
803 }
804 }else{
805 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
806 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
807 }
808 }
810 /* Ensure to create a new object */
811 if(preg_match("/ou=incoming,/",$this->orig_dn)){
812 $this->orig_dn = "new";
813 }
815 /* Move object in necessary*/
816 if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
817 $this->move($this->orig_dn, $this->dn);
818 }
820 /* Write back to ldap */
821 $ldap= $this->config->get_ldap_link();
822 $ldap->cat($this->dn);
823 if(!$ldap->count()){
824 $ldap->cd($this->config->current['BASE']);
825 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
826 $ldap->cd($this->dn);
828 /* Remove empty values */
829 foreach($this->attrs as $name => $value){
830 if(empty($value)){
831 unset($this->attrs[$name]);
832 }
833 }
835 $ldap->add($this->attrs);
836 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
837 new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
838 } else {
839 $ldap->cd($this->dn);
840 $this->cleanup();
841 $ldap->modify ($this->attrs);
842 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
843 new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
844 }
845 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system print/generic with dn '%s' failed."),$this->dn));
847 if(preg_match("/printer/i",$this->BelongsTo)){
848 $this->netConfigDNS->cn = $this->cn;
849 $this->netConfigDNS->dn = $this->dn;
850 $this->netConfigDNS->save();
851 }
853 /* This is a multi object. Handle tagging here... */
854 $this->handle_object_tagging();
855 }
857 function generateList(){
858 $a_return=array();
860 foreach($this->member as $type => $values){
861 $a_return[$type]=array();
862 foreach($values as $value){
863 if((preg_match("/Group/i",$type))){
864 if(!isset($value['description'])){
865 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
866 }else{
867 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
868 }
869 }else{
870 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
871 }
872 }
873 }
874 return($a_return);
875 }
877 /* Return plugin informations for acl handling
878 #FIXME FAIscript seams to ununsed within this class... */
879 function plInfo()
880 {
881 return (array(
882 "plShortName" => _("Generic"),
883 "plDescription" => _("Print generic"),
884 "plSelfModify" => FALSE,
885 "plDepends" => array(),
886 "plPriority" => 4,
887 "plSection" => array("administration"),
888 "plCategory" => array("printer" => array("description" => _("Printer"),
889 "objectClass" => "gotoPrinter"),"workstation","terminal"),
890 "plProvidedAcls"=> array(
891 "cn" => _("Name"),
892 "base" => _("Base") ,
893 "description" => _("Description"),
894 "l" => _("Location"),
895 "labeledURI" => _("LabeledURL"),
896 "gotoPrinterPPD" => _("Printer PPD"),
897 "gotoUserPrinter" => _("Permissions"))
898 ));
899 }
902 /* Delete member */
903 function DelMember($type,$id)
904 {
905 /* Check if there was a printer "dn" given, or the "cn" */
906 foreach($this->member[$type] as $key => $printer){
907 if($printer['dn'] == $id) {
908 $id = $key;
909 }
910 }
912 if(!$this->acl_is_writeable("gotoUserPrinter")){
913 print_red(sprintf(_("You are not allowed to remove the given object '%s' from the list of members of printer '%s'."),$id,$this->dn));
914 return(FALSE);
915 }
917 if(isset($this->member[$type][$id])){
918 unset($this->member[$type][$id]);
919 return(TRUE);
920 }
921 return(FALSE);
922 }
925 /* Add given obejct to members */
926 function AddMember($type,$dn)
927 {
928 $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
929 if(!in_array_ics($type, $types)){
930 print_red(sprintf(_("Illegal printer type while adding '%s' to the list of '%s' printers,"),$dn,$type));
931 return(FALSE);
932 }
934 if(!$this->acl_is_writeable("gotoUserPrinter")){
935 print_red(sprintf(_("You are not allowed to add the given object '%s' to the list of members of '%s'."),$dn,$this->dn));
936 return(FALSE);
937 }
939 /* Get name of index attributes */
940 if(preg_match("/user/i",$type)){
941 $var = "uid";
942 }else{
943 $var = "cn";
944 }
946 $ldap = $this->config->get_ldap_link();
947 $ldap->cd($dn);
948 $ldap->cat($dn,array($var,"cn"));
949 if($ldap->count()){
951 $attrs = $ldap->fetch();
953 if(isset($attrs[$var][0])){
954 $name = $attrs[$var][0];
956 /* Check if this uid/cn is already assigned to any permission */
957 foreach($types as $ctype){
959 /* If we want to add a user, only check user/userAdmin members */
960 if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
961 continue;
962 }
964 /* If we want to add a group, only check groups/adminGroups .. */
965 if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
966 continue;
967 }
969 if(isset( $this->member[$ctype][$name])){
970 print_red(sprintf(_("Can't add '%s' to the list of members, it is already used."),$attrs[$var][0]));
971 return(FALSE);
972 }
973 }
975 /* Everything is fine. So add the given object to members */
976 $this->member[$type][$attrs[$var][0]] = $attrs ;
977 }else{
978 print_a($attrs);
979 }
980 }else{
981 print_red(sprintf(_("Can't add '%s' to list of members, it is not reachable."),$dn));
982 return(FALSE);
983 }
984 return(TRUE);
985 }
988 /* Display generic part for server copy & paste */
989 function getCopyDialog()
990 {
991 $vars = array("cn");
992 $smarty = get_smarty();
993 $smarty->assign("cn" ,$this->cn);
994 $smarty->assign("object","printer");
995 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
996 $ret = array();
997 $ret['string'] = $str;
998 $ret['status'] = "";
999 return($ret);
1000 }
1003 function saveCopyDialog()
1004 {
1005 if(isset($_POST['cn'])){
1006 $this->cn = $_POST['cn'];
1007 }
1008 }
1010 function PrepareForCopyPaste($source)
1011 {
1012 plugin::PrepareForCopyPaste($source);
1013 if(isset($source['macAddress'][0])){
1014 $this->netConfigDNS->macAddress = $source['macAddress'][0];
1015 }
1016 if(isset($source['ipHostNumber'][0])){
1017 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1018 }
1020 $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1021 foreach($this->attributes as $attr){
1022 $this->$attr = $source_o->$attr;
1023 }
1024 $this->member = $source_o -> member;
1026 $this->gotoPrinterPPD = "";
1027 }
1028 }
1030 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1031 ?>