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