1 <?php
3 class printgeneric extends plugin
4 {
5 /* Generic terminal attributes */
6 var $interfaces = array();
7 var $ignore_account = FALSE;
9 /* Needed values and lists */
10 var $base = "";
11 var $cn = "";
12 var $l = "";
13 var $description = "";
14 var $labeledURI = "";
15 var $gotoPrinterPPD = "";
16 var $initial_PPD = "";
17 var $orig_dn = "";
18 var $orig_cn = "";
19 var $orig_base = "";
21 var $UserMember ="";
22 var $UserMembers =array();
23 var $UserMemberKeys =array();
25 var $AdminMember ="";
26 var $AdminMembers =array();
27 var $AdminMemberKeys =array();
29 var $PPDdialogToSave = NULL;
30 var $BelongsTo = "unknown"; // Specifies if this is a standalone printer, or belongs to a terminal / WS
32 var $member =array();
33 var $strings = "";
34 var $netConfigDNS;
35 var $baseSelection = false;
36 var $macAddress = "";
38 var $gotoUserAdminPrinter;
39 var $gotoGroupAdminPrinter ;
40 var $gotoGroupPrinter;
41 var $gotoUserPrinter ;
43 /* attribute list for save action */
44 var $attributes = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress",
45 "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter","gosaUnitTag");
46 var $objectclasses = array("top", "gotoPrinter");
47 var $view_logged = FALSE;
48 var $parent;
50 function printgeneric (&$config, $dn,$parent_init,$parent)
51 {
52 $this->config = &$config;
53 $this->dn = $dn;
55 /* If parent was posted(the tabs object) we can detect the printer type. */
56 if($parent){
57 $this->parent = $parent;
58 $this->getTypeOfPrinter();
59 }else{
60 $this->BelongsTo = "unknown";
61 return;
62 }
64 /* Update dn, to ensure storing as printer instead of WS / terminal */
65 if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
66 $this->dn= preg_replace("/".normalizePreg(get_ou('terminalRDN'))."/",get_ou('printerRDN'),$this->dn);
67 }
69 if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
70 $this->dn= preg_replace("/".normalizePreg(get_ou('workstationRDN'))."/",get_ou('printerRDN'),$this->dn);
71 }
73 $this->orig_dn = $this->dn;
75 /* Get printer settings, possibly dn has changed */
76 plugin::plugin ($config, $this->dn);
78 /* Get is_account initially_was_account status */
79 $this->getTypeOfPrinter(true);
81 /* set orig dn to new if object is new */
82 $ldap= $this->config->get_ldap_link();
83 $ldap->cat($this->dn, array('dn'));
84 if(!$ldap->count()){
85 $this->orig_dn = "new";
86 }
88 /* create dns object */
89 $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
91 /* Set base */
92 if ($this->dn == "new"){
93 $ui= get_userinfo();
94 $this->base= dn2base($ui->dn);
95 $this->cn= "";
96 } else {
98 /* Set base and check if the extracted base exists */
99 if(preg_match("/".normalizePreg(get_ou('systemIncomingRDN'))."/",$this->dn)){
100 $this->base= preg_replace("/".normalizePreg(get_ou('systemIncomingRDN'))."/","",dn2base($this->dn));
101 }else{
102 $this->base= preg_replace("/".normalizePreg(get_ou('printerRDN'))."/","",dn2base($this->dn));
103 }
105 if(!isset($this->config->idepartments[$this->base])){
106 msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
107 $this->base = session::get('CurrentMainBase');
108 }
109 }
111 /* Extract selected ppd */
112 if(isset($this->gotoPrinterPPD)){
113 $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
114 }
116 $this->initial_PPD = $this->gotoPrinterPPD;
118 /* Prepare different member types */
119 foreach(array("AddUser" =>"gotoUserPrinter",
120 "AddGroup" =>"gotoGroupPrinter",
121 "AddAdminUser" =>"gotoUserAdminPrinter",
122 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
124 /* $this->members contains all members */
125 $this->member[$type]=array();
127 if(isset($this->attrs[$attr])){
128 $ldap->cd($this->config->current['BASE']) ;
129 for($i = 0 ; $i < $this->attrs[$attr]['count']; $i++){
131 $mem = $this->attrs[$attr][$i];
132 if(preg_match("/Group/",$type)){
133 $ldap->search("(&(|(objectClass=posixGroup)(objectClass=gosaGroupOfNames))(cn=".$mem."))",array("cn","description"));
134 if($ldap->count()){
135 $entry = $ldap->fetch();
136 $this->member[$type][$entry['cn'][0]]=$entry;
137 }
138 }else{
139 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
140 if($ldap->count()){
141 $entry = $ldap->fetch();
142 $this->member[$type][$entry['uid'][0]]=$entry;
143 }
144 }
145 }
146 }
147 }
148 $this->orig_cn = $this->cn;
149 $this->orig_base = $this->base;
150 }
152 function set_acl_base($base)
153 {
154 plugin::set_acl_base($base);
155 if(is_object($this->netConfigDNS)){
156 $this->netConfigDNS->set_acl_base($base);
157 }
158 }
160 function set_acl_category($cat)
161 {
162 plugin::set_acl_category($cat);
163 if(is_object($this->netConfigDNS)){
164 $this->netConfigDNS->set_acl_category($cat);
165 }
166 }
168 /* Detect type of printer.
169 * Printer can be stand alone, belong to a workstation or belong to a terminal.
170 * We can detect the type printer type when comparing the tabs objects
171 */
172 function getTypeOfPrinter($UpdateAccountStatus = false)
173 {
174 /* Disable account as default
175 */
176 $this->is_account = $this->initially_was_account = false;
178 /* Detect type of printer via parent tabs.
179 */
181 $class = get_class($this->parent);
182 if(isset($this->parent->by_object['workgeneric'])){
184 /* Exclude templates
185 */
186 $this->cn = $this->parent->by_object['workgeneric']->cn;
187 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
188 $this->BelongsTo = "WorkstationTemplate";
189 }else{
190 $this->BelongsTo = "Workstation";
191 }
192 }elseif(isset($this->parent->by_object['termgeneric'])){
194 /* Exclude templates
195 */
196 $this->cn = $this->parent->by_object['termgeneric']->cn;
197 if($this->parent->by_object['termgeneric']->cn == "default"){
198 $this->BelongsTo = "TerminalTemplate";
199 }else{
200 $this->BelongsTo = "Terminal";
201 }
202 }elseif(isset($this->parent->by_name['printgeneric'])){
203 $this->BelongsTo = "Printer";
204 }
206 if($UpdateAccountStatus){
208 /* Set is_account / was account
209 */
210 if($this->dn == "new"){
211 $this->initially_was_account = false;
212 }
214 /* If is printer it must be a true account.
215 */
216 if(preg_match("/printer/i",$this->BelongsTo)){
217 $this->is_account = true;
218 }
220 /* Update dn, to ensure storing as printer instead of WS / terminal
221 */
222 if(preg_match("/terminal/i",$this->BelongsTo)){
223 $this->dn= preg_replace("/".normalizePreg(get_ou('terminalRDN'))."/",get_ou('printerRDN'),$this->dn);
224 }
226 if(preg_match("/workstation/i",$this->BelongsTo)){
227 $this->dn= preg_replace("/".normalizePreg(get_ou('workstationRDN'))."/",get_ou('printerRDN'),$this->dn);
228 }
230 /* Detect if this is a valid printer account;
231 */
232 $ldap = $this->config->get_ldap_link();
233 $ldap->cat($this->dn, array('objectClass'));
235 if($ldap->count()){
236 $attrs = $ldap->fetch();
237 if(in_array("gotoPrinter",$attrs['objectClass'])){
238 $this->initially_was_account = true;
239 $this->is_account = true;
240 }else{
241 $this->is_account = false;
242 }
243 }
244 }
245 }
247 function execute()
248 {
249 /* Call parent execute */
250 plugin::execute();
252 if($this->is_account && !$this->view_logged){
253 $this->view_logged = TRUE;
254 new log("view","printer/".get_class($this),$this->dn);
255 }
258 /* If type of printer couldn't be detected (because of missing parent object in construction)
259 * hide this tab.
260 */
261 if(preg_match("/unknown/i",$this->BelongsTo)){
262 $display= $this->show_enable_header(_("Add printer extension"),
263 _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
264 return($display);
265 }
267 /* Templates can't have printer extensions
268 */
269 if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
270 $display= $this->show_enable_header(_("Add printer extension"),
271 _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
272 return($display);
273 }
274 if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
275 $display= $this->show_enable_header(_("Add printer extension"),
276 _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
277 return($display);
278 }
280 /* Get cn from base object */
281 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
282 $this->cn = $this->parent->by_object['workgeneric']->cn;
283 }
284 if(preg_match("/^Terminal$/i",$this->BelongsTo)){
285 $this->cn = $this->parent->by_object['termgeneric']->cn;
286 }
288 $smarty= get_smarty();
290 /* Assign acls */
291 $tmp = $this->plInfo();
292 foreach($tmp['plProvidedAcls'] as $name => $translation){
293 $smarty->assign($name."ACL", $this->getacl($name));
294 }
296 $display="";
298 /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
299 if(preg_match("/^Printer$/i",$this->BelongsTo)){
300 $smarty->assign("StandAlone",true);
301 }else{
302 $smarty->assign("StandAlone",false);
303 }
305 /* Do we need to flip is_account state? */
306 if(isset($_POST['modify_state'])){
307 if($this->is_account && $this->acl_is_removeable()){
308 $this->is_account= FALSE;
309 }elseif(!$this->is_account && $this->acl_is_createable()){
310 $this->is_account= TRUE;
311 }
312 }
314 /* Do we represent a valid printer? */
315 if (!$this->is_account && $this->parent === NULL){
316 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
317 msgPool::noValidExtension(_("printer"))."</b>";
318 return($display);
319 }
321 /* If this is a WS / Terminal depending printer, display account state button */
322 if(!preg_match("/^Printer$/i",$this->BelongsTo)){
323 if($this->cn == "" && ($this->dn != "new")){
324 $display= $this->show_enable_header(_("Add printer extension"),
325 msgPool::featuresDisabled(_("printer"))._("You can't enable it while 'cn' is not present in entry. Possibly you are currently creating a new terminal template."),TRUE,TRUE);
326 $this->is_account= false;
327 return $display;
328 }
330 if (($this->is_account)){
331 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
332 $display= $this->show_disable_header(_("Remove printer extension"),
333 msgPool::featuresEnabled(_("printer")));
334 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
335 $display= $this->show_disable_header(_("Remove printer extension"),
336 msgPool::featuresDisabled(_("printer")));
337 }
338 }else{
339 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
340 $display= $this->show_enable_header(_("Add printer extension"),
341 msgPool::featuresEnabled(_("printer")));
342 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
343 $display= $this->show_enable_header(_("Add printer extension"),
344 msgPool::featuresDisabled(_("printer")));
345 }
346 return ($display);
347 }
348 }
350 /* Base select dialog */
351 $once = true;
352 foreach($_POST as $name => $value){
353 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
354 $once = false;
355 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
356 $this->dialog->setCurrentBase($this->base);
357 $this->baseSelection = true;
358 }
359 }
361 /* Dialog handling for base select dialog
362 * Check if base was selected, dialog aborted etc */
363 if(is_object($this->dialog)){
365 $this->dialog->save_object();
366 if($this->baseSelection){
367 if($this->dialog->isClosed()){
368 $this->dialog = false;
369 $this->baseSelection = false;
370 }elseif($this->dialog->isSelected()){
372 /* A new base was selected, check if it is a valid one */
373 $tmp = $this->get_allowed_bases();
374 if(isset($tmp[$this->dialog->isSelected()])){
375 $this->base = $this->dialog->isSelected();
376 }
378 $this->dialog= false;
379 $this->baseSelection = false;
380 }else{
381 return($this->dialog->execute());
382 }
383 }
384 }
386 /* Fill templating stuff */
387 $smarty->assign("bases", $this->get_allowed_bases());
388 $smarty->assign("base_select", $this->base);
390 /* Assign attributes */
391 foreach ($this->attributes as $attr){
392 $smarty->assign("$attr", $this->$attr);
393 }
395 if(isset($_POST['AddUser'])){
396 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
397 }
398 if(isset($_POST['AddGroup'])){
399 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
400 }
401 if(isset($_POST['AddAdminUser'])){
402 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
403 }
404 if(isset($_POST['AddAdminGroup'])){
405 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
406 }
408 /* Display ppd configure/select dialog */
409 if(isset($_POST['EditDriver'])){
410 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
411 $this->dialog = $this->PPDdialogToSave;
412 }else{
413 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
414 $this->dialog->cn= $this->cn;
415 }
416 }
418 /* remove ppd */
419 if(isset($_POST['RemoveDriver'])){
420 $this->gotoPrinterPPD = array();
421 $this->PPDdialogToSave = NULL;
422 }
424 /* Close ppd dialog */
425 if(isset($_POST['ClosePPD'])){
426 unset($this->dialog);
427 $this->dialog=FALSE;
428 }
430 /* Save selected ppd */
431 if(isset($_POST['SavePPD'])){
432 $this->dialog->save_object();
433 if(count($this->dialog->check())){
434 foreach($this->dialog->check() as $msg){
435 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
436 }
437 }else{
438 $this->gotoPrinterPPD = array();
439 $this->gotoPrinterPPD = $this->dialog->save();
440 $this->PPDdialogToSave = $this->dialog;
441 unset($this->dialog);
442 $this->dialog=FALSE;
443 }
444 }
446 /* Member management, delete user / group / admin ..*/
447 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
448 foreach($_POST['UserMember'] as $mem){
449 $this->DelMember('AddUser',$mem);
450 }
451 }
453 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
454 foreach($_POST['UserMember'] as $mem){
455 $this->DelMember('AddGroup',$mem);
456 }
457 }
459 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
460 foreach($_POST['AdminMember'] as $mem){
461 $this->DelMember('AddAdminUser',$mem);
462 }
463 }
465 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
466 foreach($_POST['AdminMember'] as $mem){
467 $this->DelMember('AddAdminGroup',$mem);
468 }
469 }
471 /* Abort user / group adding dialog */
472 if(isset($_POST['PrinterCancel'])){
473 unset($this->dialog);
474 $this->dialog= FALSE;
475 }
477 /* Save selected users / groups */
478 if(isset($_POST['PrinterSave'])){
479 $this->dialog->save_object();
480 if(count($this->dialog->check())){
481 foreach($this->dialog->check() as $msg){
482 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
483 }
484 }else{
485 $data= $new = $this->dialog->save();
486 unset($data['type']);
487 foreach($data as $mem){
488 $this->AddMember($new['type'], $mem['dn']);
489 }
490 unset($this->dialog);
491 $this->dialog=FALSE;
492 }
493 }
495 /* Display dialog, if there is currently one open*/
496 if(is_object($this->dialog)){
497 $this->dialog->save_object();
498 $display = $this->dialog->execute();
499 return $display;
500 }
502 /* Parse selected ppd file */
503 $config = session::get('config');
504 if ($config->get_cfg_value("ppdPath") != ""){
505 $path = $config->get_cfg_value("ppdPath");
506 if(!preg_match("/\/$/",$path)){
507 $path = $path."/";
508 }
510 $ppdManager= new ppdManager($path);
511 if(!empty($this->gotoPrinterPPD)){
512 if((!file_exists($path.$this->gotoPrinterPPD))){
513 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
514 }else{
515 $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
516 }
517 }else{
518 $smarty->assign("driverInfo", _("Not defined"));
519 }
520 }else{
521 $smarty->assign("driverInfo",_("Can't get ppd informations."));
522 }
524 /* Create user & admin user list */
525 $list=$this->generateList();
526 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
527 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
529 asort($userlist);
530 asort($adminlist);
532 if(!preg_match("/Printer/i",$this->BelongsTo)){
533 if(preg_match("/Terminal/i",$this->BelongsTo)){
534 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
535 }else{
536 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
537 }
538 $smarty->assign("cnACL" , $this->getacl("cn",true));
539 }else{
540 $smarty->assign("desc" ,"");
541 }
542 $smarty->assign("UserMember" ,$this->UserMember);
543 $smarty->assign("UserMembers" ,$userlist);
544 $smarty->assign("UserMemberKeys",array_flip($userlist));
546 $smarty->assign("AdminMember" ,$this->AdminMember);
547 $smarty->assign("AdminMembers" ,$adminlist);
548 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
549 if(preg_match("/Printer/i",$this->BelongsTo)){
551 /* Show main page */
552 $str = $this->netConfigDNS->execute();
553 if(is_object($this->netConfigDNS->dialog)){
554 return($str);
555 }
556 $smarty->assign("netconfig", $str);
557 } else {
558 $smarty->assign("netconfig", "");
559 }
561 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
562 }
564 function remove_from_parent()
565 {
566 /* Only remove if there was initially an account */
567 if($this->initially_was_account){
569 /* Update dn, to ensure storing as printer instead of WS / terminal
570 */
571 if(preg_match("/terminal/i",$this->BelongsTo)){
572 $this->dn= preg_replace("/".normalizePreg(get_ou('terminalRDN')).",/",get_ou('printerRDN'),$this->dn);
573 }
575 if(preg_match("/workstation/i",$this->BelongsTo)){
576 $this->dn= preg_replace("/".normalizePreg(get_ou('workstationRDN'))."/",get_ou('printerRDN'),$this->dn);
577 }
579 /* Check if this dn points to a printer, to avoid deleting something else */
580 $ldap= $this->config->get_ldap_link();
581 $ldap->cat($this->dn, array('dn',"objectClass"));
582 if(!$ldap->count()){
583 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
584 return;
585 }
587 /* Check if obejct is a printer */
588 $CheckPrinter = $ldap->fetch();
589 if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
590 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
591 return;
592 }
594 /* Remove account & dns extension */
595 $this->netConfigDNS->remove_from_parent();
596 $ldap->rmdir($this->dn);
598 new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
600 if (!$ldap->success()){
601 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
602 }
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=".LDAP::prepare4filter($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.get_ou('printerRDN').",".$this->base;
668 /* must: cn */
669 if(($this->BelongsTo == "Printer") && $this->cn == ""){
670 $message[]= msgPool::required(_("Name"));
671 }
673 /* must: cn */
674 if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
675 $message[]= msgPool::invalid(_("Name"));
676 }
678 /* must: labeledURI */
679 if(empty($this->labeledURI)){
680 $message[]= msgPool::required(_("Printer URL"));
681 }
683 /* Check if there is already an entry with this cn*/
684 if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
685 $ldap= $this->config->get_ldap_link();
686 $ldap->cd ($this->base);
687 $ldap->ls("(cn=".$this->cn.")",get_ou('printerRDN').$this->base, array("cn"));
688 if ($ldap->count() != 0){
689 while ($attrs= $ldap->fetch()){
690 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
691 continue;
692 }
693 if ($attrs['dn'] != $this->orig_dn){
694 $message[]= msgPool::duplicated(_("Name"));
695 break;
696 }
697 }
698 }
699 }
701 /* Check if we are allowed to create or move this object
702 */
703 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
704 $message[] = msgPool::permCreate();
705 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
706 $message[] = msgPool::permMove();
707 }
709 return ($message);
710 }
713 /* Save to LDAP */
714 function save()
715 {
716 /* Update dn, to ensure storing as printer instead of WS / terminal
717 */
718 if(preg_match("/terminal/i",$this->BelongsTo)){
719 $this->dn= preg_replace("/".normalizePreg(get_ou('terminalRDN')).",/",get_ou('printerRDN'),$this->dn);
720 }
722 if(preg_match("/workstation/i",$this->BelongsTo)){
723 $this->dn= preg_replace("/".normalizePreg(get_ou('workstationRDN'))."/",get_ou('printerRDN'),$this->dn);
724 }
726 if(!$this->is_account) return;
727 if(isset($this->parent->by_object['workgeneric'])){
728 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
729 return;
730 }
732 /* Adapt IP & mac from parent object */
733 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
734 $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
736 }elseif(isset($this->parent->by_object['termgeneric'])){
737 if($this->parent->by_object['termgeneric']->cn == "default"){
738 return;
739 }
741 /* Adapt IP & mac from parent object */
742 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
743 $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
744 }
746 /* If type is still unknown, the initialisation of this printer failed, abort. */
747 if(preg_match("/unknown/i",$this->BelongsTo)){
748 return;
749 }
751 /* save ppd configuration */
752 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
753 $this->PPDdialogToSave->save_ppd();
754 }
755 if($this->orig_dn != $this->dn){
756 if(!empty($this->gotoPrinterPPD)) {
757 $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
758 $this->PPDdialogToSave->cn = $this->cn;
759 $this->PPDdialogToSave->generateProperties();
760 $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
761 }
762 }
764 /* Remove previously selected ppd file.*/
765 if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
766 if(!empty($this->initial_PPD)){
767 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
768 $tmp->removeModifiedPPD();
769 }
770 }
772 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
773 $method="https://";
774 }else{
775 $method="http://";
776 }
778 /* Get servername */
779 $server = $_SERVER['SERVER_NAME'];
780 if(tests::is_ip($server)){
781 $server_name = gethostbyaddr($server);
782 }else{
783 $server_name = gethostbyaddr(gethostbyname($server));
784 }
786 /* If no ppd is selected, remove this attribute */
787 if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
788 $this->gotoPrinterPPD = $method.str_replace("//","/",$server_name."/ppd/".$this->gotoPrinterPPD);
789 }else{
790 $this->gotoPrinterPPD = array();
791 }
793 $dn= $this->dn;
795 /* reduce objectClasses to minimun */
796 $this->attrs['objectClass']= $this->objectclasses;
798 plugin::save();
799 $ldap= $this->config->get_ldap_link();
801 /* Remove all empty values */
802 if ($this->orig_dn == 'new'){
803 $attrs= array();
804 foreach ($this->attrs as $key => $val){
805 if (is_array($val) && count($val) == 0){
806 continue;
807 }
808 $attrs[$key]= $val;
809 }
810 $this->attrs= $attrs;
811 }
813 /* Append printer user
814 */
815 $this->attrs['gotoUserPrinter']=array();
816 foreach($this->member['AddUser'] as $mem){
817 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
818 }
820 /* Append printer group
821 */
822 $this->attrs['gotoGroupPrinter'] = array();
823 foreach($this->member['AddGroup'] as $mem){
824 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
825 }
827 /* Append printer admin user
828 */
829 $this->attrs['gotoUserAdminPrinter'] = array();
830 foreach($this->member['AddAdminUser'] as $mem){
831 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
832 }
834 /* Append printer admin group
835 */
836 $this->attrs['gotoGroupAdminPrinter']= array();
837 foreach($this->member['AddAdminGroup'] as $mem){
838 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
839 }
841 if($this->orig_dn == 'new'){
842 foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
843 if(count($this->attrs[$checkVar]) == 0 || empty($this->attrs[$checkVar])){
844 unset($this->attrs[$checkVar]);
845 }
846 }
847 }
849 /* Ensure to create a new object */
850 if(preg_match("/".normalizePreg(get_ou('systemIncomingRDN'))."/",$this->orig_dn)){
851 $this->orig_dn = "new";
852 }
854 /* Move object in necessary*/
855 if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
856 $this->move($this->orig_dn, $this->dn);
857 }
859 /* Write back to ldap */
860 $ldap= $this->config->get_ldap_link();
861 $ldap->cat($this->dn);
862 if(!$ldap->count()){
863 $ldap->cd($this->config->current['BASE']);
864 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
865 $ldap->cd($this->dn);
867 /* Remove empty values */
868 foreach($this->attrs as $name => $value){
869 if(empty($value)){
870 unset($this->attrs[$name]);
871 }
872 }
874 $ldap->add($this->attrs);
875 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
876 new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
877 } else {
878 $ldap->cd($this->dn);
879 $this->cleanup();
880 $ldap->modify ($this->attrs);
881 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
882 new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
883 }
884 if (!$ldap->success()){
885 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
886 }
888 #if(preg_match("/printer/i",$this->BelongsTo)){
889 $this->netConfigDNS->cn = $this->cn;
890 $this->netConfigDNS->dn = $this->dn;
891 $this->netConfigDNS->save();
892 #}
893 }
895 function generateList(){
896 $a_return=array();
898 foreach($this->member as $type => $values){
899 $a_return[$type]=array();
900 foreach($values as $value){
901 if((preg_match("/Group/i",$type))){
902 if(!isset($value['description'])){
903 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
904 }else{
905 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
906 }
907 }else{
908 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
909 }
910 }
911 }
912 return($a_return);
913 }
915 /* Return plugin informations for acl handling
916 #FIXME FAIscript seams to ununsed within this class... */
917 static function plInfo()
918 {
919 return (array(
920 "plShortName" => _("Generic"),
921 "plDescription" => _("Print generic"),
922 "plSelfModify" => FALSE,
923 "plDepends" => array(),
924 "plPriority" => 4,
925 "plSection" => array("administration"),
926 "plCategory" => array("printer" => array("description" => _("Printer"),
927 "objectClass" => "gotoPrinter"),"workstation","terminal"),
928 "plProvidedAcls"=> array(
929 "cn" => _("Name"),
930 "base" => _("Base") ,
931 "description" => _("Description"),
932 "l" => _("Location"),
933 "labeledURI" => _("LabeledURL"),
934 "gotoPrinterPPD" => _("Printer PPD"),
935 "gotoUserPrinter" => _("Permissions"))
936 ));
937 }
940 /* Delete member */
941 function DelMember($type,$id)
942 {
943 /* Check if there was a printer "dn" given, or the "cn" */
944 foreach($this->member[$type] as $key => $printer){
945 if($printer['dn'] == $id) {
946 $id = $key;
947 }
948 }
950 if(!$this->acl_is_writeable("gotoUserPrinter")){
951 msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
952 return(FALSE);
953 }
955 if(isset($this->member[$type][$id])){
956 unset($this->member[$type][$id]);
957 return(TRUE);
958 }
959 return(FALSE);
960 }
963 /* Add given obejct to members */
964 function AddMember($type,$dn)
965 {
966 $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
967 if(!in_array_ics($type, $types)){
968 msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
969 return(FALSE);
970 }
972 if(!$this->acl_is_writeable("gotoUserPrinter")){
973 msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
974 return(FALSE);
975 }
977 /* Get name of index attributes */
978 if(preg_match("/user/i",$type)){
979 $var = "uid";
980 }else{
981 $var = "cn";
982 }
984 $ldap = $this->config->get_ldap_link();
985 $ldap->cd($dn);
986 $ldap->cat($dn,array($var,"cn"));
987 if($ldap->count()){
989 $attrs = $ldap->fetch();
991 if(isset($attrs[$var][0])){
992 $name = $attrs[$var][0];
994 /* Check if this uid/cn is already assigned to any permission */
995 foreach($types as $ctype){
997 /* If we want to add a user, only check user/userAdmin members */
998 if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
999 continue;
1000 }
1002 /* If we want to add a group, only check groups/adminGroups .. */
1003 if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1004 continue;
1005 }
1007 if(isset( $this->member[$ctype][$name])){
1008 msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1009 return(FALSE);
1010 }
1011 }
1013 /* Everything is fine. So add the given object to members */
1014 $this->member[$type][$attrs[$var][0]] = $attrs ;
1015 }else{
1016 print_a($attrs);
1017 }
1018 }else{
1019 msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1020 return(FALSE);
1021 }
1022 return(TRUE);
1023 }
1026 /* Display generic part for server copy & paste */
1027 function getCopyDialog()
1028 {
1029 $vars = array("cn");
1030 $smarty = get_smarty();
1031 $smarty->assign("cn" ,$this->cn);
1032 $smarty->assign("object","printer");
1033 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1034 $ret = array();
1035 $ret['string'] = $str;
1036 $ret['status'] = "";
1037 return($ret);
1038 }
1041 function saveCopyDialog()
1042 {
1043 if(isset($_POST['cn'])){
1044 $this->cn = $_POST['cn'];
1045 }
1046 }
1048 function PrepareForCopyPaste($source)
1049 {
1050 plugin::PrepareForCopyPaste($source);
1051 if(isset($source['macAddress'][0])){
1052 $this->netConfigDNS->macAddress = $source['macAddress'][0];
1053 }
1054 if(isset($source['ipHostNumber'][0])){
1055 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1056 }
1058 $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1059 foreach($this->attributes as $attr){
1060 $this->$attr = $source_o->$attr;
1061 }
1062 $this->member = $source_o -> member;
1064 $this->gotoPrinterPPD = "";
1065 }
1066 }
1068 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1069 ?>