11936d4f071bd993880524bbb4cf887d3b9e20f8
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("termgeneric", "terminalRDN"), '/')."/i",get_ou("printgeneric", "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("workgeneric", "workstationRDN"), '/')."/i",get_ou("printgeneric", "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(class_available("ArpNewDevice") &&
108 preg_match("/".preg_quote(get_ou("ArpNewDevice", "systemIncomingRDN"), '/')."/i",$this->dn)){
109 $this->base= preg_replace("/".preg_quote(get_ou("ArpNewDevice", "systemIncomingRDN"), '/')."/i","",dn2base($this->dn));
110 }else{
111 $this->base= preg_replace("/".preg_quote(get_ou("printgeneric", "printerRDN"), '/')."/i","",dn2base($this->dn));
112 }
114 if(!isset($this->config->idepartments[$this->base])){
115 msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
116 $this->base = session::get('CurrentMainBase');
117 }
118 }
120 /* If no ppd is selected, remove this attribute */
121 if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
122 $this->ppdServerPart = preg_replace("/^(http.*ppd)\/.*$/i","\\1",$this->gotoPrinterPPD);
123 }else{
125 /* Detect PPD server part */
126 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
127 $method="https://";
128 }else{
129 $method="http://";
130 }
132 /* Get servername */
133 $server = $_SERVER['SERVER_NAME'];
134 if(tests::is_ip($server)){
135 $server_name = @gethostbyaddr($server);
136 }else{
137 $server_name = @gethostbyaddr(gethostbyname($server));
138 }
139 $this->ppdServerPart = $method.str_replace("//","/",$server_name."/ppd");
140 }
142 /* Extract selected ppd */
143 if(isset($this->gotoPrinterPPD)){
144 $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
145 }
147 $this->initial_PPD = $this->gotoPrinterPPD;
149 /* Prepare different member types */
150 foreach(array("AddUser" =>"gotoUserPrinter",
151 "AddGroup" =>"gotoGroupPrinter",
152 "AddAdminUser" =>"gotoUserAdminPrinter",
153 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
155 /* $this->members contains all members */
156 $this->member[$type]=array();
158 if(isset($this->attrs[$attr])){
159 $ldap->cd($this->config->current['BASE']) ;
160 for($i = 0 ; $i < $this->attrs[$attr]['count']; $i++){
162 $mem = $this->attrs[$attr][$i];
163 if(preg_match("/Group/",$type)){
164 $ldap->search("(&(|(objectClass=posixGroup)(objectClass=gosaGroupOfNames))(cn=".$mem."))",array("cn","description"));
165 if($ldap->count()){
166 $entry = $ldap->fetch();
167 $this->member[$type][$entry['cn'][0]]=$entry;
168 }
169 }else{
170 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
171 if($ldap->count()){
172 $entry = $ldap->fetch();
173 $this->member[$type][$entry['uid'][0]]=$entry;
174 }
175 }
176 }
177 }
178 }
179 $this->orig_cn = $this->cn;
180 $this->orig_base = $this->base;
182 /* Instanciate base selector */
183 $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
184 $this->baseSelector->setSubmitButton(false);
185 $this->baseSelector->setHeight(300);
186 $this->baseSelector->update(true);
187 }
189 function set_acl_base($base)
190 {
191 plugin::set_acl_base($base);
192 if(is_object($this->netConfigDNS)){
193 $this->netConfigDNS->set_acl_base($base);
194 }
195 }
197 function set_acl_category($cat)
198 {
199 plugin::set_acl_category($cat);
200 if(is_object($this->netConfigDNS)){
201 $this->netConfigDNS->set_acl_category($cat);
202 }
203 }
205 /* Detect type of printer.
206 * Printer can be stand alone, belong to a workstation or belong to a terminal.
207 * We can detect the type printer type when comparing the tabs objects
208 */
209 function getTypeOfPrinter($UpdateAccountStatus = false)
210 {
211 /* Disable account as default
212 */
213 $this->is_account = $this->initially_was_account = false;
215 /* Detect type of printer via parent tabs.
216 */
218 $class = get_class($this->parent);
219 if(isset($this->parent->by_object['workgeneric'])){
221 /* Exclude templates
222 */
223 $this->cn = $this->parent->by_object['workgeneric']->cn;
224 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
225 $this->BelongsTo = "WorkstationTemplate";
226 }else{
227 $this->BelongsTo = "Workstation";
228 }
229 }elseif(isset($this->parent->by_object['termgeneric'])){
231 /* Exclude templates
232 */
233 $this->cn = $this->parent->by_object['termgeneric']->cn;
234 if($this->parent->by_object['termgeneric']->cn == "default"){
235 $this->BelongsTo = "TerminalTemplate";
236 }else{
237 $this->BelongsTo = "Terminal";
238 }
239 }elseif(isset($this->parent->by_name['printgeneric'])){
240 $this->BelongsTo = "Printer";
241 }
243 if($UpdateAccountStatus){
245 /* Set is_account / was account
246 */
247 if($this->dn == "new"){
248 $this->initially_was_account = false;
249 }
251 /* If is printer it must be a true account.
252 */
253 if(preg_match("/printer/i",$this->BelongsTo)){
254 $this->is_account = true;
255 }
257 /* Update dn, to ensure storing as printer instead of WS / terminal
258 */
259 if(preg_match("/terminal/i",$this->BelongsTo)){
260 $this->dn= preg_replace("/".preg_quote(get_ou("termgeneric", "terminalRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
261 }
263 if(preg_match("/workstation/i",$this->BelongsTo)){
264 $this->dn= preg_replace("/".preg_quote(get_ou("workgeneric", "workstationRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
265 }
267 /* Detect if this is a valid printer account;
268 */
269 $ldap = $this->config->get_ldap_link();
270 $ldap->cat($this->dn, array('objectClass'));
272 if($ldap->count()){
273 $attrs = $ldap->fetch();
274 if(in_array("gotoPrinter",$attrs['objectClass'])){
275 $this->initially_was_account = true;
276 $this->is_account = true;
277 }else{
278 $this->is_account = false;
279 }
280 }
281 }
282 }
284 function execute()
285 {
286 /* Call parent execute */
287 plugin::execute();
289 if($this->is_account && !$this->view_logged){
290 $this->view_logged = TRUE;
291 new log("view","printer/".get_class($this),$this->dn);
292 }
294 /* If type of printer couldn't be detected (because of missing parent object in construction)
295 * hide this tab.
296 */
297 if(preg_match("/unknown/i",$this->BelongsTo)){
298 $display= $this->show_enable_header(_("Add printer extension"),
299 _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
300 return($display);
301 }
303 /* Templates can't have printer extensions
304 */
305 if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
306 $display= $this->show_enable_header(_("Add printer extension"),
307 _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
308 return($display);
309 }
310 if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
311 $display= $this->show_enable_header(_("Add printer extension"),
312 _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
313 return($display);
314 }
316 /* Get cn from base object */
317 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
318 $this->cn = $this->parent->by_object['workgeneric']->cn;
319 }
320 if(preg_match("/^Terminal$/i",$this->BelongsTo)){
321 $this->cn = $this->parent->by_object['termgeneric']->cn;
322 }
324 $smarty= get_smarty();
327 /* Assign acls */
328 $tmp = $this->plInfo();
329 foreach($tmp['plProvidedAcls'] as $name => $translation){
330 $smarty->assign($name."ACL", $this->getacl($name));
331 }
333 $display="";
335 /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
336 if(preg_match("/^Printer$/i",$this->BelongsTo)){
337 $smarty->assign("StandAlone",true);
338 }else{
339 $smarty->assign("StandAlone",false);
340 }
342 /* Do we need to flip is_account state? */
343 if(isset($_POST['modify_state'])){
344 if($this->is_account && $this->acl_is_removeable()){
345 $this->is_account= FALSE;
346 }elseif(!$this->is_account && $this->acl_is_createable()){
347 $this->is_account= TRUE;
348 }
349 }
351 /* Do we represent a valid printer? */
352 if (!$this->is_account && $this->parent === NULL){
353 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
354 msgPool::noValidExtension(_("printer"))."</b>";
355 return($display);
356 }
358 /* If this is a WS / Terminal depending printer, display account state button */
359 if(!preg_match("/^Printer$/i",$this->BelongsTo)){
360 if($this->cn == "" && ($this->dn != "new")){
361 $display= $this->show_enable_header(_("Add printer extension"),
362 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);
363 $this->is_account= false;
364 return $display;
365 }
367 if (($this->is_account)){
368 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
369 $display= $this->show_disable_header(_("Remove printer extension"),
370 msgPool::featuresEnabled(_("printer")));
371 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
372 $display= $this->show_disable_header(_("Remove printer extension"),
373 msgPool::featuresDisabled(_("printer")));
374 }
375 }else{
376 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
377 $display= $this->show_enable_header(_("Add printer extension"),
378 msgPool::featuresEnabled(_("printer")));
379 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
380 $display= $this->show_enable_header(_("Add printer extension"),
381 msgPool::featuresDisabled(_("printer")));
382 }
383 return ($display);
384 }
385 }
387 /* Assign attributes */
388 foreach ($this->attributes as $attr){
389 $smarty->assign("$attr", set_post($this->$attr));
390 }
392 $smarty->assign("base", $this->baseSelector->render());
394 // Act on add user/grouo requests
395 if(isset($_POST['AddUser'])){
396 $this->userSelect = new userGroupSelect($this->config, get_userinfo());
397 $this->dialog = TRUE;
398 }
399 if(isset($_POST['AddAdminUser'])){
400 $this->adminUserSelect = new userGroupSelect($this->config, get_userinfo());
401 $this->dialog = TRUE;
402 }
404 /* Display ppd configure/select dialog */
405 if(isset($_POST['EditDriver'])){
406 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
407 $this->dialog = $this->PPDdialogToSave;
408 }else{
410 if(is_array($this->gotoPrinterPPD)){
411 $this->dialog = new printerPPDDialog($this->config, $this->dn,"");
412 }else{
413 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
414 }
415 $this->dialog->cn= $this->cn;
416 }
418 // Detect edit acl base
419 $ldap = $this->config->get_ldap_link();
420 if($ldap->dn_exists($this->dn)){
421 $acl_base = $this->dn;
422 }else{
423 $acl_base = $this->base;
424 }
426 $this->dialog->set_acl_base($acl_base);
427 }
429 /* remove ppd */
430 if(isset($_POST['RemoveDriver'])){
431 /* Detect PPD server part */
432 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
433 $method="https://";
434 }else{
435 $method="http://";
436 }
438 /* Get servername */
439 $server = $_SERVER['SERVER_NAME'];
440 if(tests::is_ip($server)){
441 $server_name = @gethostbyaddr($server);
442 }else{
443 $server_name = @gethostbyaddr(gethostbyname($server));
444 }
445 $this->ppdServerPart = $method.str_replace("//","/",$server_name."/ppd");
446 $this->gotoPrinterPPD = array();
447 $this->PPDdialogToSave = NULL;
448 }
450 /* Close ppd dialog */
451 if(isset($_POST['ClosePPD'])){
452 unset($this->dialog);
453 $this->dialog=FALSE;
454 }
456 /* Save selected ppd */
457 if(isset($_POST['SavePPD'])){
458 $this->dialog->save_object();
459 if(count($this->dialog->check())){
460 foreach($this->dialog->check() as $msg){
461 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
462 }
463 }else{
464 $this->gotoPrinterPPD = array();
465 $this->gotoPrinterPPD = $this->dialog->save();
466 $this->PPDdialogToSave = $this->dialog;
467 unset($this->dialog);
468 $this->dialog=FALSE;
469 }
470 }
472 /* Member management, delete user / group / admin ..*/
473 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
474 foreach($_POST['UserMember'] as $mem){
475 $this->DelMember('AddUser',$mem);
476 }
477 }
479 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
480 foreach($_POST['UserMember'] as $mem){
481 $this->DelMember('AddGroup',$mem);
482 }
483 }
485 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
486 foreach($_POST['AdminMember'] as $mem){
487 $this->DelMember('AddAdminUser',$mem);
488 }
489 }
491 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
492 foreach($_POST['AdminMember'] as $mem){
493 $this->DelMember('AddAdminGroup',$mem);
494 }
495 }
497 // Abort user / group adding dialog
498 if(isset($_POST['userGroupSelect_cancel'])){
499 $this->dialog=FALSE;
500 $this->userSelect = NULL;
501 $this->adminUserSelect = NULL;
502 }
504 // Save selected users / groups
505 if(isset($_POST['userGroupSelect_save'])){
506 $users = array();
507 if($this->userSelect instanceOf userGroupSelect){
508 $users = $this->userSelect->save();
509 $add = '';
510 }elseif($this->adminUserSelect instanceOf userGroupSelect){
511 $users = $this->adminUserSelect->save();
512 $add = 'Admin';
513 }
514 foreach($users as $user){
515 if(in_array('gosaAccount', $user['objectClass'])){
516 $type = 'Add'.$add.'User';
517 }else{
518 $type = 'Add'.$add.'Group';
519 }
520 $this->AddMember($type, $user['dn']);
521 }
522 $this->dialog=FALSE;
523 $this->userSelect = NULL;
524 $this->adminUserSelect = NULL;
525 }
527 // Display add user/group dialogs
528 if($this->userSelect instanceOf userGroupSelect || $this->adminUserSelect instanceOf userGroupSelect){
530 // Build up blocklist
531 $used = array();
532 foreach($this->member as $type => $members){
533 foreach($members as $member){
534 $used['dn'][] = $member['dn'];
535 }
536 }
537 session::set('filterBlacklist', $used);
538 if($this->userSelect instanceOf userGroupSelect){
539 return($this->userSelect->execute());
540 }elseif($this->adminUserSelect instanceOf userGroupSelect){
541 return($this->adminUserSelect->execute());
542 }
543 }
546 /* Display dialog, if there is currently one open*/
547 if(is_object($this->dialog)){
548 $this->dialog->save_object();
549 $display = $this->dialog->execute();
550 return $display;
551 }
553 /* Parse selected ppd file */
554 $config = session::get('config');
555 if ($config->get_cfg_value("core","ppdPath") != ""){
556 $path = $config->get_cfg_value("core","ppdPath");
557 if(!preg_match("/\/$/",$path)){
558 $path = $path."/";
559 }
561 $ppdManager= new ppdManager($path);
562 $smarty->assign("displayServerPath",true);
563 if(!empty($this->gotoPrinterPPD)){
564 if((!file_exists($path.$this->gotoPrinterPPD))){
565 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
566 }else{
567 $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD);
568 $smarty->assign("driverInfo", set_post($ppdDesc['name']));
569 }
570 }else{
571 $smarty->assign("driverInfo", _("Not defined"));
572 $smarty->assign("displayServerPath",false);
573 }
574 }else{
575 $smarty->assign("driverInfo",_("Can't get PPD informations."));
576 $smarty->assign("displayServerPath",true);
577 }
578 $smarty->assign("ppdServerPart",$this->ppdServerPart);
581 /* Create user & admin user list */
582 $list=$this->generateList();
583 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
584 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
586 asort($userlist);
587 asort($adminlist);
589 if(!preg_match("/Printer/i",$this->BelongsTo)){
590 if(preg_match("/Terminal/i",$this->BelongsTo)){
591 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
592 }else{
593 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
594 }
595 $smarty->assign("cnACL" , $this->getacl("cn",true));
596 }else{
597 $smarty->assign("desc" ,"");
598 }
599 $smarty->assign("UserMember" ,$this->UserMember);
600 $smarty->assign("UserMembers" ,$userlist);
601 $smarty->assign("UserMemberKeys",array_flip($userlist));
603 $smarty->assign("AdminMember" ,$this->AdminMember);
604 $smarty->assign("AdminMembers" ,$adminlist);
605 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
606 if(preg_match("/Printer/i",$this->BelongsTo)){
608 /* Show main page */
609 $str = $this->netConfigDNS->execute();
610 if(is_object($this->netConfigDNS->dialog)){
611 return($str);
612 }
613 $smarty->assign("netconfig", $str);
614 } else {
615 $smarty->assign("netconfig", "");
616 }
618 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
619 }
621 function remove_from_parent()
622 {
623 /* Only remove if there was initially an account */
624 if($this->initially_was_account){
626 /* Update dn, to ensure storing as printer instead of WS / terminal
627 */
628 if(preg_match("/terminal/i",$this->BelongsTo)){
629 $this->dn= preg_replace("/".preg_quote(get_ou("termgeneric", "terminalRDN"), '/').",/i",get_ou("printgeneric", "printerRDN"),$this->dn);
630 }
632 if(preg_match("/workstation/i",$this->BelongsTo)){
633 $this->dn= preg_replace("/".preg_quote(get_ou("workgeneric", "workstationRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
634 }
636 /* Check if this dn points to a printer, to avoid deleting something else */
637 $ldap= $this->config->get_ldap_link();
638 $ldap->cat($this->dn, array('dn',"objectClass"));
639 if(!$ldap->count()){
640 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
641 return;
642 }
644 /* Check if obejct is a printer */
645 $CheckPrinter = $ldap->fetch();
646 if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
647 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
648 return;
649 }
651 /* Remove account & dns extension */
652 $this->netConfigDNS->remove_from_parent();
653 $ldap->rmdir($this->dn);
655 new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
657 if (!$ldap->success()){
658 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
659 }
660 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
662 /* Delete references to object groups */
663 $ldap->cd ($this->config->current['BASE']);
664 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
665 while ($ldap->fetch()){
666 $og= new ogroup($this->config, $ldap->getDN());
667 unset($og->member[$this->dn]);
668 $og->save ();
669 }
671 /* Remove previously selected ppd file.*/
672 if(!empty($this->initial_PPD)){
673 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
674 $tmp->removeModifiedPPD();
675 }
676 }
677 }
680 /* Save data to object */
681 function save_object()
682 {
683 /* Create a base backup and reset the
684 base directly after calling plugin::save_object();
685 Base will be set seperatly a few lines below */
686 $base_tmp = $this->base;
687 plugin::save_object();
688 $this->base = $base_tmp;
690 if(isset($_POST['ppdServerPart'])){
691 $this->ppdServerPart = get_post('ppdServerPart');
692 }
694 if(is_object($this->netConfigDNS)){
695 $this->netConfigDNS->save_object();
696 }
698 /* Refresh base */
699 if ($this->acl_is_moveable($this->base)){
700 if (!$this->baseSelector->update()) {
701 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
702 }
703 if ($this->base != $this->baseSelector->getBase()) {
704 $this->base= $this->baseSelector->getBase();
705 $this->is_modified= TRUE;
706 }
707 }
709 }
711 /* Check supplied data */
712 function check()
713 {
714 /* Call common method to give check the hook */
715 $message= plugin::check();
716 if (preg_match("/printer/i",$this->BelongsTo)){
717 $message= array_merge($message, $this->netConfigDNS->check());
718 }
720 /* Don't display check messages if this is a template object */
721 if(isset($this->parent->by_object['workgeneric'])){
722 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
723 return $message;
724 }
725 }elseif(isset($this->parent->by_object['termgeneric'])){
726 if($this->parent->by_object['termgeneric']->cn == "default"){
727 return $message;
728 }
729 }
731 $dn= "cn=".$this->cn.get_ou("printgeneric", "printerRDN").",".$this->base;
733 /* must: cn */
734 if(($this->BelongsTo == "Printer") && $this->cn == ""){
735 $message[]= msgPool::required(_("Name"));
736 }
738 // Check if a wrong base was supplied
739 if(!$this->baseSelector->checkLastBaseUpdate()){
740 $message[]= msgPool::check_base();
741 }
743 /* must: cn */
744 if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
745 $message[]= msgPool::invalid(_("Name"));
746 }
748 /* must: labeledURI */
749 if(empty($this->labeledURI)){
750 $message[]= msgPool::required(_("Printer URL"));
751 }
753 /* Check if there is already an entry with this cn*/
754 if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
755 $ldap= $this->config->get_ldap_link();
756 $ldap->cd ($this->base);
757 $ldap->ls("(cn=".$this->cn.")",get_ou("printgeneric", "printerRDN").$this->base, array("cn"));
758 if ($ldap->count() != 0){
759 while ($attrs= $ldap->fetch()){
760 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
761 continue;
762 }
763 if ($attrs['dn'] != $this->orig_dn){
764 $message[]= msgPool::duplicated(_("Name"));
765 break;
766 }
767 }
768 }
769 }
771 /* Check if we are allowed to create or move this object
772 */
773 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
774 $message[] = msgPool::permCreate();
775 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
776 $message[] = msgPool::permMove();
777 }
779 return ($message);
780 }
783 /* Save to LDAP */
784 function save()
785 {
786 /* Update dn, to ensure storing as printer instead of WS / terminal
787 */
788 if(preg_match("/terminal/i",$this->BelongsTo)){
789 $this->dn= preg_replace("/".preg_quote(get_ou("termgeneric", "terminalRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
790 }
792 if(preg_match("/workstation/i",$this->BelongsTo)){
793 $this->dn= preg_replace("/".preg_quote(get_ou("workgeneric", "workstationRDN"), '/')."/i",get_ou("printgeneric", "printerRDN"),$this->dn);
794 }
796 if(!$this->is_account) return;
797 if(isset($this->parent->by_object['workgeneric'])){
798 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
799 return;
800 }
802 /* Adapt IP & mac from parent object */
803 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
804 $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
806 }elseif(isset($this->parent->by_object['termgeneric'])){
807 if($this->parent->by_object['termgeneric']->cn == "default"){
808 return;
809 }
811 /* Adapt IP & mac from parent object */
812 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
813 $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
814 }
816 /* If type is still unknown, the initialisation of this printer failed, abort. */
817 if(preg_match("/unknown/i",$this->BelongsTo)){
818 return;
819 }
821 /* save ppd configuration */
822 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
824 $this->PPDdialogToSave->save_ppd();
826 /* Rename the generated ppd to match the gzip ending '.gz', if necessary.
827 */
828 $path = $this->config->get_cfg_value("core","ppdPath");
829 if(!preg_match("/\/$/",$path)){
830 $path = $path."/";
831 }
832 $ppdManager= new ppdManager($path);
833 if($ppdManager->useGzip && !preg_match('/\.gz$/',$this->gotoPrinterPPD)){
834 if(rename($path.$this->gotoPrinterPPD,$path.$this->gotoPrinterPPD.'.gz')){
835 $this->gotoPrinterPPD .= '.gz';
836 }
837 }elseif(!$ppdManager->useGzip && preg_match('/\.gz$/',$this->gotoPrinterPPD)){
838 $new_ppd = preg_replace('/\.gz$/','',$this->gotoPrinterPPD);
839 if(rename($path.$this->gotoPrinterPPD,$path.$new_ppd)){
840 $this->gotoPrinterPPD = $new_ppd;
841 }
842 }
843 }
844 if($this->orig_dn != $this->dn && $this->orig_dn != "new"){
845 if(!empty($this->gotoPrinterPPD)) {
846 $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
847 $this->PPDdialogToSave->cn = $this->cn;
848 $this->PPDdialogToSave->generateProperties();
849 $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
850 }
851 }
853 /* Remove previously selected ppd file.*/
854 if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
855 if(!empty($this->initial_PPD)){
856 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
857 $tmp->removeModifiedPPD();
858 }
859 }
861 /* If no ppd is selected, remove this attribute */
862 if(!empty($this->gotoPrinterPPD)) {
863 $this->gotoPrinterPPD = $this->ppdServerPart.'/'.$this->gotoPrinterPPD;
864 }else{
865 $this->gotoPrinterPPD = array();
866 }
868 $dn= $this->dn;
870 /* reduce objectClasses to minimun */
871 $this->attrs['objectClass']= $this->objectclasses;
873 plugin::save();
874 $ldap= $this->config->get_ldap_link();
876 /* Remove all empty values */
877 if ($this->orig_dn == 'new'){
878 $attrs= array();
879 foreach ($this->attrs as $key => $val){
880 if (is_array($val) && count($val) == 0){
881 continue;
882 }
883 $attrs[$key]= $val;
884 }
885 $this->attrs= $attrs;
886 }
888 /* Append printer user
889 */
890 $this->attrs['gotoUserPrinter']=array();
891 foreach($this->member['AddUser'] as $mem){
892 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
893 }
895 /* Append printer group
896 */
897 $this->attrs['gotoGroupPrinter'] = array();
898 foreach($this->member['AddGroup'] as $mem){
899 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
900 }
902 /* Append printer admin user
903 */
904 $this->attrs['gotoUserAdminPrinter'] = array();
905 foreach($this->member['AddAdminUser'] as $mem){
906 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
907 }
909 /* Append printer admin group
910 */
911 $this->attrs['gotoGroupAdminPrinter']= array();
912 foreach($this->member['AddAdminGroup'] as $mem){
913 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
914 }
916 if($this->orig_dn == 'new'){
917 foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
918 if(count($this->attrs[$checkVar]) == 0 || empty($this->attrs[$checkVar])){
919 unset($this->attrs[$checkVar]);
920 }
921 }
922 }
924 /* Ensure to create a new object */
925 if(class_available("ArpNewDevice") &&
926 preg_match("/".preg_quote(get_ou("ArpNewDevice", "systemIncomingRDN"), '/')."/i",$this->orig_dn)){
927 $this->orig_dn = "new";
928 }
930 /* Move object in necessary*/
931 if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
932 $this->move($this->orig_dn, $this->dn);
933 }
935 /* Write back to ldap */
936 $ldap= $this->config->get_ldap_link();
937 $ldap->cat($this->dn);
938 if(!$ldap->count()){
939 $ldap->cd($this->config->current['BASE']);
940 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
941 $ldap->cd($this->dn);
943 /* Remove empty values */
944 foreach($this->attrs as $name => $value){
945 if(empty($value)){
946 unset($this->attrs[$name]);
947 }
948 }
950 $ldap->add($this->attrs);
951 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
952 new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
953 } else {
954 $ldap->cd($this->dn);
955 $this->cleanup();
956 $ldap->modify ($this->attrs);
957 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
958 new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
959 }
960 if (!$ldap->success()){
961 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
962 }
964 #if(preg_match("/printer/i",$this->BelongsTo)){
965 $this->netConfigDNS->cn = $this->cn;
966 $this->netConfigDNS->dn = $this->dn;
967 $this->netConfigDNS->save();
968 #}
969 }
971 function generateList(){
972 $a_return=array();
974 foreach($this->member as $type => $values){
975 $a_return[$type]=array();
976 foreach($values as $value){
977 if((preg_match("/Group/i",$type))){
978 if(!isset($value['description'])){
979 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
980 }else{
981 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
982 }
983 }else{
984 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
985 }
986 }
987 }
988 return($a_return);
989 }
991 /* Return plugin informations for acl handling
992 #FIXME FAIscript seams to ununsed within this class... */
993 static function plInfo()
994 {
995 return (array(
996 "plShortName" => _("Generic"),
997 "plDescription" => _("Print generic"),
998 "plSelfModify" => FALSE,
999 "plDepends" => array(),
1000 "plPriority" => 4,
1001 "plSection" => array("administration"),
1002 "plRequirements"=> array(
1003 'ldapSchema' => array('gotoPrinter' => '>=2.7'),
1004 'onFailureDisablePlugin' => array(get_class())
1005 ),
1006 "plCategory" => array("printer" => array("description" => _("Printer"),
1007 "objectClass" => "gotoPrinter"),"workstation","terminal"),
1008 "plProperties" =>
1009 array(
1010 array(
1011 "name" => "printerRDN",
1012 "type" => "rdn",
1013 "default" => "ou=printers,ou=systems,",
1014 "description" => _("RDN for printer storage."),
1015 "check" => "gosaProperty::isRdn",
1016 "migrate" => "migrate_printerRDN",
1017 "group" => "plugin",
1018 "mandatory" => FALSE
1019 )
1020 ),
1022 "plProvidedAcls"=> array(
1023 "cn" => _("Name"),
1024 "base" => _("Base") ,
1025 "description" => _("Description"),
1026 "l" => _("Location"),
1027 "labeledURI" => _("Labeled URI"),
1028 "macAddress" => _("Mac address"),
1029 "gotoPrinterPPD" => _("Printer PPD"),
1030 "gotoUserPrinter" => _("Permissions"))
1031 ));
1032 }
1035 /* Delete member */
1036 function DelMember($type,$id)
1037 {
1038 /* Check if there was a printer "dn" given, or the "cn" */
1039 foreach($this->member[$type] as $key => $printer){
1040 if($printer['dn'] == $id) {
1041 $id = $key;
1042 }
1043 }
1045 if(!$this->acl_is_writeable("gotoUserPrinter")){
1046 msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
1047 return(FALSE);
1048 }
1050 if(isset($this->member[$type][$id])){
1051 unset($this->member[$type][$id]);
1052 return(TRUE);
1053 }
1054 return(FALSE);
1055 }
1058 /* Add given obejct to members */
1059 function AddMember($type,$dn)
1060 {
1061 $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
1062 if(!in_array_ics($type, $types)){
1063 msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
1064 return(FALSE);
1065 }
1067 if(!$this->acl_is_writeable("gotoUserPrinter")){
1068 msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
1069 return(FALSE);
1070 }
1072 /* Get name of index attributes */
1073 if(preg_match("/user/i",$type)){
1074 $var = "uid";
1075 }else{
1076 $var = "cn";
1077 }
1079 $ldap = $this->config->get_ldap_link();
1080 $ldap->cd($dn);
1081 $ldap->cat($dn,array($var,"cn"));
1082 if($ldap->count()){
1084 $attrs = $ldap->fetch();
1086 if(isset($attrs[$var][0])){
1087 $name = $attrs[$var][0];
1089 /* Check if this uid/cn is already assigned to any permission */
1090 foreach($types as $ctype){
1092 /* If we want to add a user, only check user/userAdmin members */
1093 if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
1094 continue;
1095 }
1097 /* If we want to add a group, only check groups/adminGroups .. */
1098 if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1099 continue;
1100 }
1102 if(isset( $this->member[$ctype][$name])){
1103 msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1104 return(FALSE);
1105 }
1106 }
1108 /* Everything is fine. So add the given object to members */
1109 $this->member[$type][$attrs[$var][0]] = $attrs ;
1110 }else{
1111 print_a($attrs);
1112 }
1113 }else{
1114 msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1115 return(FALSE);
1116 }
1117 return(TRUE);
1118 }
1121 /* Display generic part for server copy & paste */
1122 function getCopyDialog()
1123 {
1124 $vars = array("cn");
1125 $smarty = get_smarty();
1126 $smarty->assign("cn" ,$this->cn);
1127 $smarty->assign("object","printer");
1128 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1129 $ret = array();
1130 $ret['string'] = $str;
1131 $ret['status'] = "";
1132 return($ret);
1133 }
1136 function saveCopyDialog()
1137 {
1138 if(isset($_POST['cn'])){
1139 $this->cn = get_post('cn');
1140 }
1141 }
1143 function PrepareForCopyPaste($source)
1144 {
1145 plugin::PrepareForCopyPaste($source);
1146 if(isset($source['macAddress'][0])){
1147 $this->netConfigDNS->macAddress = $source['macAddress'][0];
1148 }
1149 if(isset($source['ipHostNumber'][0])){
1150 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1151 }
1153 $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1154 foreach($this->attributes as $attr){
1155 $this->$attr = $source_o->$attr;
1156 }
1157 $this->member = $source_o -> member;
1159 $this->gotoPrinterPPD = "";
1160 }
1163 function is_modal_dialog()
1164 {
1165 return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
1166 }
1168 }
1170 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1171 ?>