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 $ppdServerPart = "";
31 var $PPDdialogToSave = NULL;
32 var $BelongsTo = "unknown"; // Specifies if this is a standalone printer, or belongs to a terminal / WS
34 var $member =array();
35 var $strings = "";
36 var $netConfigDNS;
37 var $baseSelection = false;
38 var $macAddress = "";
40 var $gotoUserAdminPrinter;
41 var $gotoGroupAdminPrinter ;
42 var $gotoGroupPrinter;
43 var $gotoUserPrinter ;
44 var $baseSelector;
46 /* attribute list for save action */
47 var $attributes = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress",
48 "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter","gosaUnitTag");
49 var $objectclasses = array("top", "gotoPrinter");
50 var $view_logged = FALSE;
51 var $parent;
54 // Memeber dialog object
55 var $userSelect;
56 var $adminUserSelect;
58 function printgeneric (&$config, $dn,$parent_init,$parent)
59 {
60 $this->config = &$config;
61 $this->dn = $dn;
63 /* If parent was posted(the tabs object) we can detect the printer type. */
64 if($parent){
65 $this->parent = $parent;
66 $this->getTypeOfPrinter();
67 }else{
68 $this->BelongsTo = "unknown";
69 return;
70 }
72 /* Update dn, to ensure storing as printer instead of WS / terminal */
73 if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
74 $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
75 }
77 if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
78 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
79 }
81 $this->orig_dn = $this->dn;
83 /* Get printer settings, possibly dn has changed */
84 plugin::plugin ($config, $this->dn);
86 /* Get is_account initially_was_account status */
87 $this->getTypeOfPrinter(true);
89 /* set orig dn to new if object is new */
90 $ldap= $this->config->get_ldap_link();
91 $ldap->cat($this->dn, array('dn'));
92 if(!$ldap->count()){
93 $this->orig_dn = "new";
94 }
96 /* create dns object */
97 $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
99 /* Set base */
100 if ($this->dn == "new"){
101 $ui= get_userinfo();
102 $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
103 $this->cn= "";
104 } else {
106 /* Set base and check if the extracted base exists */
107 if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i",$this->dn)){
108 $this->base= preg_replace("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i","",dn2base($this->dn));
109 }else{
110 $this->base= preg_replace("/".preg_quote(get_ou('printerRDN'), '/')."/i","",dn2base($this->dn));
111 }
113 if(!isset($this->config->idepartments[$this->base])){
114 msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
115 $this->base = session::get('CurrentMainBase');
116 }
117 }
119 /* If no ppd is selected, remove this attribute */
120 if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
121 $this->ppdServerPart = preg_replace("/^(http.*ppd)\/.*$/i","\\1",$this->gotoPrinterPPD);
122 }else{
124 /* Detect PPD server part */
125 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
126 $method="https://";
127 }else{
128 $method="http://";
129 }
131 /* Get servername */
132 $server = $_SERVER['SERVER_NAME'];
133 if(tests::is_ip($server)){
134 $server_name = @gethostbyaddr($server);
135 }else{
136 $server_name = @gethostbyaddr(gethostbyname($server));
137 }
138 $this->ppdServerPart = $method.str_replace("//","/",$server_name."/ppd");
139 }
141 /* Extract selected ppd */
142 if(isset($this->gotoPrinterPPD)){
143 $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
144 }
146 $this->initial_PPD = $this->gotoPrinterPPD;
148 /* Prepare different member types */
149 foreach(array("AddUser" =>"gotoUserPrinter",
150 "AddGroup" =>"gotoGroupPrinter",
151 "AddAdminUser" =>"gotoUserAdminPrinter",
152 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
154 /* $this->members contains all members */
155 $this->member[$type]=array();
157 if(isset($this->attrs[$attr])){
158 $ldap->cd($this->config->current['BASE']) ;
159 for($i = 0 ; $i < $this->attrs[$attr]['count']; $i++){
161 $mem = $this->attrs[$attr][$i];
162 if(preg_match("/Group/",$type)){
163 $ldap->search("(&(|(objectClass=posixGroup)(objectClass=gosaGroupOfNames))(cn=".$mem."))",array("cn","description"));
164 if($ldap->count()){
165 $entry = $ldap->fetch();
166 $this->member[$type][$entry['cn'][0]]=$entry;
167 }
168 }else{
169 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
170 if($ldap->count()){
171 $entry = $ldap->fetch();
172 $this->member[$type][$entry['uid'][0]]=$entry;
173 }
174 }
175 }
176 }
177 }
178 $this->orig_cn = $this->cn;
179 $this->orig_base = $this->base;
181 /* Instanciate base selector */
182 $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
183 $this->baseSelector->setSubmitButton(false);
184 $this->baseSelector->setHeight(300);
185 $this->baseSelector->update(true);
186 }
188 function set_acl_base($base)
189 {
190 plugin::set_acl_base($base);
191 if(is_object($this->netConfigDNS)){
192 $this->netConfigDNS->set_acl_base($base);
193 }
194 }
196 function set_acl_category($cat)
197 {
198 plugin::set_acl_category($cat);
199 if(is_object($this->netConfigDNS)){
200 $this->netConfigDNS->set_acl_category($cat);
201 }
202 }
204 /* Detect type of printer.
205 * Printer can be stand alone, belong to a workstation or belong to a terminal.
206 * We can detect the type printer type when comparing the tabs objects
207 */
208 function getTypeOfPrinter($UpdateAccountStatus = false)
209 {
210 /* Disable account as default
211 */
212 $this->is_account = $this->initially_was_account = false;
214 /* Detect type of printer via parent tabs.
215 */
217 $class = get_class($this->parent);
218 if(isset($this->parent->by_object['workgeneric'])){
220 /* Exclude templates
221 */
222 $this->cn = $this->parent->by_object['workgeneric']->cn;
223 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
224 $this->BelongsTo = "WorkstationTemplate";
225 }else{
226 $this->BelongsTo = "Workstation";
227 }
228 }elseif(isset($this->parent->by_object['termgeneric'])){
230 /* Exclude templates
231 */
232 $this->cn = $this->parent->by_object['termgeneric']->cn;
233 if($this->parent->by_object['termgeneric']->cn == "default"){
234 $this->BelongsTo = "TerminalTemplate";
235 }else{
236 $this->BelongsTo = "Terminal";
237 }
238 }elseif(isset($this->parent->by_name['printgeneric'])){
239 $this->BelongsTo = "Printer";
240 }
242 if($UpdateAccountStatus){
244 /* Set is_account / was account
245 */
246 if($this->dn == "new"){
247 $this->initially_was_account = false;
248 }
250 /* If is printer it must be a true account.
251 */
252 if(preg_match("/printer/i",$this->BelongsTo)){
253 $this->is_account = true;
254 }
256 /* Update dn, to ensure storing as printer instead of WS / terminal
257 */
258 if(preg_match("/terminal/i",$this->BelongsTo)){
259 $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
260 }
262 if(preg_match("/workstation/i",$this->BelongsTo)){
263 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
264 }
266 /* Detect if this is a valid printer account;
267 */
268 $ldap = $this->config->get_ldap_link();
269 $ldap->cat($this->dn, array('objectClass'));
271 if($ldap->count()){
272 $attrs = $ldap->fetch();
273 if(in_array("gotoPrinter",$attrs['objectClass'])){
274 $this->initially_was_account = true;
275 $this->is_account = true;
276 }else{
277 $this->is_account = false;
278 }
279 }
280 }
281 }
283 function execute()
284 {
285 /* Call parent execute */
286 plugin::execute();
288 if($this->is_account && !$this->view_logged){
289 $this->view_logged = TRUE;
290 new log("view","printer/".get_class($this),$this->dn);
291 }
293 /* If type of printer couldn't be detected (because of missing parent object in construction)
294 * hide this tab.
295 */
296 if(preg_match("/unknown/i",$this->BelongsTo)){
297 $display= $this->show_enable_header(_("Add printer extension"),
298 _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
299 return($display);
300 }
302 /* Templates can't have printer extensions
303 */
304 if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
305 $display= $this->show_enable_header(_("Add printer extension"),
306 _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
307 return($display);
308 }
309 if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
310 $display= $this->show_enable_header(_("Add printer extension"),
311 _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
312 return($display);
313 }
315 /* Get cn from base object */
316 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
317 $this->cn = $this->parent->by_object['workgeneric']->cn;
318 }
319 if(preg_match("/^Terminal$/i",$this->BelongsTo)){
320 $this->cn = $this->parent->by_object['termgeneric']->cn;
321 }
323 $smarty= get_smarty();
324 $smarty->assign("usePrototype", "true");
326 /* Assign acls */
327 $tmp = $this->plInfo();
328 foreach($tmp['plProvidedAcls'] as $name => $translation){
329 $smarty->assign($name."ACL", $this->getacl($name));
330 }
332 $display="";
334 /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
335 if(preg_match("/^Printer$/i",$this->BelongsTo)){
336 $smarty->assign("StandAlone",true);
337 }else{
338 $smarty->assign("StandAlone",false);
339 }
341 /* Do we need to flip is_account state? */
342 if(isset($_POST['modify_state'])){
343 if($this->is_account && $this->acl_is_removeable()){
344 $this->is_account= FALSE;
345 }elseif(!$this->is_account && $this->acl_is_createable()){
346 $this->is_account= TRUE;
347 }
348 }
350 /* Do we represent a valid printer? */
351 if (!$this->is_account && $this->parent === NULL){
352 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
353 msgPool::noValidExtension(_("printer"))."</b>";
354 return($display);
355 }
357 /* If this is a WS / Terminal depending printer, display account state button */
358 if(!preg_match("/^Printer$/i",$this->BelongsTo)){
359 if($this->cn == "" && ($this->dn != "new")){
360 $display= $this->show_enable_header(_("Add printer extension"),
361 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);
362 $this->is_account= false;
363 return $display;
364 }
366 if (($this->is_account)){
367 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
368 $display= $this->show_disable_header(_("Remove printer extension"),
369 msgPool::featuresEnabled(_("printer")));
370 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
371 $display= $this->show_disable_header(_("Remove printer extension"),
372 msgPool::featuresDisabled(_("printer")));
373 }
374 }else{
375 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
376 $display= $this->show_enable_header(_("Add printer extension"),
377 msgPool::featuresEnabled(_("printer")));
378 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
379 $display= $this->show_enable_header(_("Add printer extension"),
380 msgPool::featuresDisabled(_("printer")));
381 }
382 return ($display);
383 }
384 }
386 /* Assign attributes */
387 foreach ($this->attributes as $attr){
388 $smarty->assign("$attr", $this->$attr);
389 }
391 $smarty->assign("base", $this->baseSelector->render());
393 // Act on add user/grouo requests
394 if(isset($_POST['AddUser'])){
395 $this->userSelect = new userGroupSelect($this->config, get_userinfo());
396 $this->dialog = TRUE;
397 }
398 if(isset($_POST['AddAdminUser'])){
399 $this->adminUserSelect = new userGroupSelect($this->config, get_userinfo());
400 $this->dialog = TRUE;
401 }
403 /* Display ppd configure/select dialog */
404 if(isset($_POST['EditDriver'])){
405 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
406 $this->dialog = $this->PPDdialogToSave;
407 }else{
409 if(is_array($this->gotoPrinterPPD)){
410 $this->dialog = new printerPPDDialog($this->config, $this->dn,"");
411 }else{
412 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
413 }
414 $this->dialog->cn= $this->cn;
415 }
417 // Detect edit acl base
418 $ldap = $this->config->get_ldap_link();
419 if($ldap->dn_exists($this->dn)){
420 $acl_base = $this->dn;
421 }else{
422 $acl_base = $this->base;
423 }
425 $this->dialog->set_acl_base($acl_base);
426 }
428 /* remove ppd */
429 if(isset($_POST['RemoveDriver'])){
430 /* Detect PPD server part */
431 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
432 $method="https://";
433 }else{
434 $method="http://";
435 }
437 /* Get servername */
438 $server = $_SERVER['SERVER_NAME'];
439 if(tests::is_ip($server)){
440 $server_name = @gethostbyaddr($server);
441 }else{
442 $server_name = @gethostbyaddr(gethostbyname($server));
443 }
444 $this->ppdServerPart = $method.str_replace("//","/",$server_name."/ppd");
445 $this->gotoPrinterPPD = array();
446 $this->PPDdialogToSave = NULL;
447 }
449 /* Close ppd dialog */
450 if(isset($_POST['ClosePPD'])){
451 unset($this->dialog);
452 $this->dialog=FALSE;
453 }
455 /* Save selected ppd */
456 if(isset($_POST['SavePPD'])){
457 $this->dialog->save_object();
458 if(count($this->dialog->check())){
459 foreach($this->dialog->check() as $msg){
460 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
461 }
462 }else{
463 $this->gotoPrinterPPD = array();
464 $this->gotoPrinterPPD = $this->dialog->save();
465 $this->PPDdialogToSave = $this->dialog;
466 unset($this->dialog);
467 $this->dialog=FALSE;
468 }
469 }
471 /* Member management, delete user / group / admin ..*/
472 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
473 foreach($_POST['UserMember'] as $mem){
474 $this->DelMember('AddUser',$mem);
475 }
476 }
478 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
479 foreach($_POST['UserMember'] as $mem){
480 $this->DelMember('AddGroup',$mem);
481 }
482 }
484 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
485 foreach($_POST['AdminMember'] as $mem){
486 $this->DelMember('AddAdminUser',$mem);
487 }
488 }
490 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
491 foreach($_POST['AdminMember'] as $mem){
492 $this->DelMember('AddAdminGroup',$mem);
493 }
494 }
496 // Abort user / group adding dialog
497 if(isset($_POST['userGroupSelect_cancel'])){
498 $this->dialog=FALSE;
499 $this->userSelect = NULL;
500 $this->adminUserSelect = NULL;
501 }
503 // Save selected users / groups
504 if(isset($_POST['userGroupSelect_save'])){
505 $users = array();
506 if($this->userSelect instanceOf userGroupSelect){
507 $users = $this->userSelect->save();
508 $add = '';
509 }elseif($this->adminUserSelect instanceOf userGroupSelect){
510 $users = $this->adminUserSelect->save();
511 $add = 'Admin';
512 }
513 foreach($users as $user){
514 if(in_array('gosaAccount', $user['objectClass'])){
515 $type = 'Add'.$add.'User';
516 }else{
517 $type = 'Add'.$add.'Group';
518 }
519 $this->AddMember($type, $user['dn']);
520 }
521 $this->dialog=FALSE;
522 $this->userSelect = NULL;
523 $this->adminUserSelect = NULL;
524 }
526 // Display add user/group dialogs
527 if($this->userSelect instanceOf userGroupSelect || $this->adminUserSelect instanceOf userGroupSelect){
529 // Build up blocklist
530 $used = array();
531 foreach($this->member as $type => $members){
532 foreach($members as $member){
533 $used['dn'][] = $member['dn'];
534 }
535 }
536 session::set('filterBlacklist', $used);
537 if($this->userSelect instanceOf userGroupSelect){
538 return($this->userSelect->execute());
539 }elseif($this->adminUserSelect instanceOf userGroupSelect){
540 return($this->adminUserSelect->execute());
541 }
542 }
545 /* Display dialog, if there is currently one open*/
546 if(is_object($this->dialog)){
547 $this->dialog->save_object();
548 $display = $this->dialog->execute();
549 return $display;
550 }
552 /* Parse selected ppd file */
553 $config = session::get('config');
554 if ($config->get_cfg_value("ppdPath") != ""){
555 $path = $config->get_cfg_value("ppdPath");
556 if(!preg_match("/\/$/",$path)){
557 $path = $path."/";
558 }
560 $ppdManager= new ppdManager($path);
561 $smarty->assign("displayServerPath",true);
562 if(!empty($this->gotoPrinterPPD)){
563 if((!file_exists($path.$this->gotoPrinterPPD))){
564 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
565 }else{
566 $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD);
567 $smarty->assign("driverInfo", $ppdDesc['name']);
568 }
569 }else{
570 $smarty->assign("driverInfo", _("Not defined"));
571 $smarty->assign("displayServerPath",false);
572 }
573 }else{
574 $smarty->assign("driverInfo",_("Can't get ppd informations."));
575 $smarty->assign("displayServerPath",true);
576 }
577 $smarty->assign("ppdServerPart",$this->ppdServerPart);
580 /* Create user & admin user list */
581 $list=$this->generateList();
582 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
583 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
585 asort($userlist);
586 asort($adminlist);
588 if(!preg_match("/Printer/i",$this->BelongsTo)){
589 if(preg_match("/Terminal/i",$this->BelongsTo)){
590 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
591 }else{
592 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
593 }
594 $smarty->assign("cnACL" , $this->getacl("cn",true));
595 }else{
596 $smarty->assign("desc" ,"");
597 }
598 $smarty->assign("UserMember" ,$this->UserMember);
599 $smarty->assign("UserMembers" ,$userlist);
600 $smarty->assign("UserMemberKeys",array_flip($userlist));
602 $smarty->assign("AdminMember" ,$this->AdminMember);
603 $smarty->assign("AdminMembers" ,$adminlist);
604 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
605 if(preg_match("/Printer/i",$this->BelongsTo)){
607 /* Show main page */
608 $str = $this->netConfigDNS->execute();
609 if(is_object($this->netConfigDNS->dialog)){
610 return($str);
611 }
612 $smarty->assign("netconfig", $str);
613 } else {
614 $smarty->assign("netconfig", "");
615 }
617 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
618 }
620 function remove_from_parent()
621 {
622 /* Only remove if there was initially an account */
623 if($this->initially_was_account){
625 /* Update dn, to ensure storing as printer instead of WS / terminal
626 */
627 if(preg_match("/terminal/i",$this->BelongsTo)){
628 $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/').",/i",get_ou('printerRDN'),$this->dn);
629 }
631 if(preg_match("/workstation/i",$this->BelongsTo)){
632 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
633 }
635 /* Check if this dn points to a printer, to avoid deleting something else */
636 $ldap= $this->config->get_ldap_link();
637 $ldap->cat($this->dn, array('dn',"objectClass"));
638 if(!$ldap->count()){
639 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
640 return;
641 }
643 /* Check if obejct is a printer */
644 $CheckPrinter = $ldap->fetch();
645 if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
646 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
647 return;
648 }
650 /* Remove account & dns extension */
651 $this->netConfigDNS->remove_from_parent();
652 $ldap->rmdir($this->dn);
654 new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
656 if (!$ldap->success()){
657 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
658 }
659 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
661 /* Delete references to object groups */
662 $ldap->cd ($this->config->current['BASE']);
663 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
664 while ($ldap->fetch()){
665 $og= new ogroup($this->config, $ldap->getDN());
666 unset($og->member[$this->dn]);
667 $og->save ();
668 }
670 /* Remove previously selected ppd file.*/
671 if(!empty($this->initial_PPD)){
672 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
673 $tmp->removeModifiedPPD();
674 }
675 }
676 }
679 /* Save data to object */
680 function save_object()
681 {
682 /* Create a base backup and reset the
683 base directly after calling plugin::save_object();
684 Base will be set seperatly a few lines below */
685 $base_tmp = $this->base;
686 plugin::save_object();
687 $this->base = $base_tmp;
689 if(isset($_POST['ppdServerPart'])){
690 $this->ppdServerPart = get_post('ppdServerPart');
691 }
693 if(is_object($this->netConfigDNS)){
694 $this->netConfigDNS->save_object();
695 }
697 /* Refresh base */
698 if ($this->acl_is_moveable($this->base)){
699 if (!$this->baseSelector->update()) {
700 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
701 }
702 if ($this->base != $this->baseSelector->getBase()) {
703 $this->base= $this->baseSelector->getBase();
704 $this->is_modified= TRUE;
705 }
706 }
708 }
710 /* Check supplied data */
711 function check()
712 {
713 /* Call common method to give check the hook */
714 $message= plugin::check();
715 if (preg_match("/printer/i",$this->BelongsTo)){
716 $message= array_merge($message, $this->netConfigDNS->check());
717 }
719 /* Don't display check messages if this is a template object */
720 if(isset($this->parent->by_object['workgeneric'])){
721 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
722 return $message;
723 }
724 }elseif(isset($this->parent->by_object['termgeneric'])){
725 if($this->parent->by_object['termgeneric']->cn == "default"){
726 return $message;
727 }
728 }
730 $dn= "cn=".$this->cn.get_ou('printerRDN').",".$this->base;
732 /* must: cn */
733 if(($this->BelongsTo == "Printer") && $this->cn == ""){
734 $message[]= msgPool::required(_("Name"));
735 }
737 // Check if a wrong base was supplied
738 if(!$this->baseSelector->checkLastBaseUpdate()){
739 $message[]= msgPool::check_base();
740 }
742 /* must: cn */
743 if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
744 $message[]= msgPool::invalid(_("Name"));
745 }
747 /* must: labeledURI */
748 if(empty($this->labeledURI)){
749 $message[]= msgPool::required(_("Printer URL"));
750 }
752 /* Check if there is already an entry with this cn*/
753 if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
754 $ldap= $this->config->get_ldap_link();
755 $ldap->cd ($this->base);
756 $ldap->ls("(cn=".$this->cn.")",get_ou('printerRDN').$this->base, array("cn"));
757 if ($ldap->count() != 0){
758 while ($attrs= $ldap->fetch()){
759 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
760 continue;
761 }
762 if ($attrs['dn'] != $this->orig_dn){
763 $message[]= msgPool::duplicated(_("Name"));
764 break;
765 }
766 }
767 }
768 }
770 /* Check if we are allowed to create or move this object
771 */
772 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
773 $message[] = msgPool::permCreate();
774 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
775 $message[] = msgPool::permMove();
776 }
778 return ($message);
779 }
782 /* Save to LDAP */
783 function save()
784 {
785 /* Update dn, to ensure storing as printer instead of WS / terminal
786 */
787 if(preg_match("/terminal/i",$this->BelongsTo)){
788 $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
789 }
791 if(preg_match("/workstation/i",$this->BelongsTo)){
792 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
793 }
795 if(!$this->is_account) return;
796 if(isset($this->parent->by_object['workgeneric'])){
797 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
798 return;
799 }
801 /* Adapt IP & mac from parent object */
802 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
803 $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
805 }elseif(isset($this->parent->by_object['termgeneric'])){
806 if($this->parent->by_object['termgeneric']->cn == "default"){
807 return;
808 }
810 /* Adapt IP & mac from parent object */
811 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
812 $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
813 }
815 /* If type is still unknown, the initialisation of this printer failed, abort. */
816 if(preg_match("/unknown/i",$this->BelongsTo)){
817 return;
818 }
820 /* save ppd configuration */
821 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
823 $this->PPDdialogToSave->save_ppd();
825 /* Rename the generated ppd to match the gzip ending '.gz', if necessary.
826 */
827 $path = $this->config->get_cfg_value("ppdPath");
828 if(!preg_match("/\/$/",$path)){
829 $path = $path."/";
830 }
831 $ppdManager= new ppdManager($path);
832 if($ppdManager->useGzip && !preg_match('/\.gz$/',$this->gotoPrinterPPD)){
833 if(rename($path.$this->gotoPrinterPPD,$path.$this->gotoPrinterPPD.'.gz')){
834 $this->gotoPrinterPPD .= '.gz';
835 }
836 }elseif(!$ppdManager->useGzip && preg_match('/\.gz$/',$this->gotoPrinterPPD)){
837 $new_ppd = preg_replace('/\.gz$/','',$this->gotoPrinterPPD);
838 if(rename($path.$this->gotoPrinterPPD,$path.$new_ppd)){
839 $this->gotoPrinterPPD = $new_ppd;
840 }
841 }
842 }
843 if($this->orig_dn != $this->dn && $this->orig_dn != "new"){
844 if(!empty($this->gotoPrinterPPD)) {
845 $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
846 $this->PPDdialogToSave->cn = $this->cn;
847 $this->PPDdialogToSave->generateProperties();
848 $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
849 }
850 }
852 /* Remove previously selected ppd file.*/
853 if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
854 if(!empty($this->initial_PPD)){
855 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
856 $tmp->removeModifiedPPD();
857 }
858 }
860 /* If no ppd is selected, remove this attribute */
861 if(!empty($this->gotoPrinterPPD)) {
862 $this->gotoPrinterPPD = $this->ppdServerPart.'/'.$this->gotoPrinterPPD;
863 }else{
864 $this->gotoPrinterPPD = array();
865 }
867 $dn= $this->dn;
869 /* reduce objectClasses to minimun */
870 $this->attrs['objectClass']= $this->objectclasses;
872 plugin::save();
873 $ldap= $this->config->get_ldap_link();
875 /* Remove all empty values */
876 if ($this->orig_dn == 'new'){
877 $attrs= array();
878 foreach ($this->attrs as $key => $val){
879 if (is_array($val) && count($val) == 0){
880 continue;
881 }
882 $attrs[$key]= $val;
883 }
884 $this->attrs= $attrs;
885 }
887 /* Append printer user
888 */
889 $this->attrs['gotoUserPrinter']=array();
890 foreach($this->member['AddUser'] as $mem){
891 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
892 }
894 /* Append printer group
895 */
896 $this->attrs['gotoGroupPrinter'] = array();
897 foreach($this->member['AddGroup'] as $mem){
898 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
899 }
901 /* Append printer admin user
902 */
903 $this->attrs['gotoUserAdminPrinter'] = array();
904 foreach($this->member['AddAdminUser'] as $mem){
905 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
906 }
908 /* Append printer admin group
909 */
910 $this->attrs['gotoGroupAdminPrinter']= array();
911 foreach($this->member['AddAdminGroup'] as $mem){
912 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
913 }
915 if($this->orig_dn == 'new'){
916 foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
917 if(count($this->attrs[$checkVar]) == 0 || empty($this->attrs[$checkVar])){
918 unset($this->attrs[$checkVar]);
919 }
920 }
921 }
923 /* Ensure to create a new object */
924 if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i",$this->orig_dn)){
925 $this->orig_dn = "new";
926 }
928 /* Move object in necessary*/
929 if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
930 $this->move($this->orig_dn, $this->dn);
931 }
933 /* Write back to ldap */
934 $ldap= $this->config->get_ldap_link();
935 $ldap->cat($this->dn);
936 if(!$ldap->count()){
937 $ldap->cd($this->config->current['BASE']);
938 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
939 $ldap->cd($this->dn);
941 /* Remove empty values */
942 foreach($this->attrs as $name => $value){
943 if(empty($value)){
944 unset($this->attrs[$name]);
945 }
946 }
948 $ldap->add($this->attrs);
949 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
950 new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
951 } else {
952 $ldap->cd($this->dn);
953 $this->cleanup();
954 $ldap->modify ($this->attrs);
955 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
956 new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
957 }
958 if (!$ldap->success()){
959 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
960 }
962 #if(preg_match("/printer/i",$this->BelongsTo)){
963 $this->netConfigDNS->cn = $this->cn;
964 $this->netConfigDNS->dn = $this->dn;
965 $this->netConfigDNS->save();
966 #}
967 }
969 function generateList(){
970 $a_return=array();
972 foreach($this->member as $type => $values){
973 $a_return[$type]=array();
974 foreach($values as $value){
975 if((preg_match("/Group/i",$type))){
976 if(!isset($value['description'])){
977 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
978 }else{
979 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
980 }
981 }else{
982 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
983 }
984 }
985 }
986 return($a_return);
987 }
989 /* Return plugin informations for acl handling
990 #FIXME FAIscript seams to ununsed within this class... */
991 static function plInfo()
992 {
993 return (array(
994 "plShortName" => _("Generic"),
995 "plDescription" => _("Print generic"),
996 "plSelfModify" => FALSE,
997 "plDepends" => array(),
998 "plPriority" => 4,
999 "plSection" => array("administration"),
1000 "plCategory" => array("printer" => array("description" => _("Printer"),
1001 "objectClass" => "gotoPrinter"),"workstation","terminal"),
1002 "plProvidedAcls"=> array(
1003 "cn" => _("Name"),
1004 "base" => _("Base") ,
1005 "description" => _("Description"),
1006 "l" => _("Location"),
1007 "labeledURI" => _("LabeledURL"),
1008 "macAddress" => _("Mac address"),
1009 "gotoPrinterPPD" => _("Printer PPD"),
1010 "gotoUserPrinter" => _("Permissions"))
1011 ));
1012 }
1015 /* Delete member */
1016 function DelMember($type,$id)
1017 {
1018 /* Check if there was a printer "dn" given, or the "cn" */
1019 foreach($this->member[$type] as $key => $printer){
1020 if($printer['dn'] == $id) {
1021 $id = $key;
1022 }
1023 }
1025 if(!$this->acl_is_writeable("gotoUserPrinter")){
1026 msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
1027 return(FALSE);
1028 }
1030 if(isset($this->member[$type][$id])){
1031 unset($this->member[$type][$id]);
1032 return(TRUE);
1033 }
1034 return(FALSE);
1035 }
1038 /* Add given obejct to members */
1039 function AddMember($type,$dn)
1040 {
1041 $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
1042 if(!in_array_ics($type, $types)){
1043 msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
1044 return(FALSE);
1045 }
1047 if(!$this->acl_is_writeable("gotoUserPrinter")){
1048 msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
1049 return(FALSE);
1050 }
1052 /* Get name of index attributes */
1053 if(preg_match("/user/i",$type)){
1054 $var = "uid";
1055 }else{
1056 $var = "cn";
1057 }
1059 $ldap = $this->config->get_ldap_link();
1060 $ldap->cd($dn);
1061 $ldap->cat($dn,array($var,"cn"));
1062 if($ldap->count()){
1064 $attrs = $ldap->fetch();
1066 if(isset($attrs[$var][0])){
1067 $name = $attrs[$var][0];
1069 /* Check if this uid/cn is already assigned to any permission */
1070 foreach($types as $ctype){
1072 /* If we want to add a user, only check user/userAdmin members */
1073 if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
1074 continue;
1075 }
1077 /* If we want to add a group, only check groups/adminGroups .. */
1078 if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1079 continue;
1080 }
1082 if(isset( $this->member[$ctype][$name])){
1083 msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1084 return(FALSE);
1085 }
1086 }
1088 /* Everything is fine. So add the given object to members */
1089 $this->member[$type][$attrs[$var][0]] = $attrs ;
1090 }else{
1091 print_a($attrs);
1092 }
1093 }else{
1094 msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1095 return(FALSE);
1096 }
1097 return(TRUE);
1098 }
1101 /* Display generic part for server copy & paste */
1102 function getCopyDialog()
1103 {
1104 $vars = array("cn");
1105 $smarty = get_smarty();
1106 $smarty->assign("cn" ,$this->cn);
1107 $smarty->assign("object","printer");
1108 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1109 $ret = array();
1110 $ret['string'] = $str;
1111 $ret['status'] = "";
1112 return($ret);
1113 }
1116 function saveCopyDialog()
1117 {
1118 if(isset($_POST['cn'])){
1119 $this->cn = $_POST['cn'];
1120 }
1121 }
1123 function PrepareForCopyPaste($source)
1124 {
1125 plugin::PrepareForCopyPaste($source);
1126 if(isset($source['macAddress'][0])){
1127 $this->netConfigDNS->macAddress = $source['macAddress'][0];
1128 }
1129 if(isset($source['ipHostNumber'][0])){
1130 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1131 }
1133 $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1134 foreach($this->attributes as $attr){
1135 $this->$attr = $source_o->$attr;
1136 }
1137 $this->member = $source_o -> member;
1139 $this->gotoPrinterPPD = "";
1140 }
1143 function is_modal_dialog()
1144 {
1145 return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
1146 }
1148 }
1150 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1151 ?>