e296759f613fdfc2f33251d92ef93fdd7dc5f5b0
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 ;
45 /* attribute list for save action */
46 var $attributes = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress",
47 "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter","gosaUnitTag");
48 var $objectclasses = array("top", "gotoPrinter");
49 var $view_logged = FALSE;
50 var $parent;
52 function printgeneric (&$config, $dn,$parent_init,$parent)
53 {
54 $this->config = &$config;
55 $this->dn = $dn;
57 /* If parent was posted(the tabs object) we can detect the printer type. */
58 if($parent){
59 $this->parent = $parent;
60 $this->getTypeOfPrinter();
61 }else{
62 $this->BelongsTo = "unknown";
63 return;
64 }
66 /* Update dn, to ensure storing as printer instead of WS / terminal */
67 if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
68 $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
69 }
71 if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
72 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
73 }
75 $this->orig_dn = $this->dn;
77 /* Get printer settings, possibly dn has changed */
78 plugin::plugin ($config, $this->dn);
80 /* Get is_account initially_was_account status */
81 $this->getTypeOfPrinter(true);
83 /* set orig dn to new if object is new */
84 $ldap= $this->config->get_ldap_link();
85 $ldap->cat($this->dn, array('dn'));
86 if(!$ldap->count()){
87 $this->orig_dn = "new";
88 }
90 /* create dns object */
91 $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
93 /* Set base */
94 if ($this->dn == "new"){
95 $ui= get_userinfo();
96 $this->base= dn2base($ui->dn);
97 $this->cn= "";
98 } else {
100 /* Set base and check if the extracted base exists */
101 if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i",$this->dn)){
102 $this->base= preg_replace("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/i","",dn2base($this->dn));
103 }else{
104 $this->base= preg_replace("/".preg_quote(get_ou('printerRDN'), '/')."/i","",dn2base($this->dn));
105 }
107 if(!isset($this->config->idepartments[$this->base])){
108 msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
109 $this->base = session::get('CurrentMainBase');
110 }
111 }
113 /* If no ppd is selected, remove this attribute */
114 if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
115 $this->ppdServerPart = preg_replace("/^(http.*ppd)\/.*$/i","\\1",$this->gotoPrinterPPD);
116 }else{
118 /* Detect PPD server part */
119 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
120 $method="https://";
121 }else{
122 $method="http://";
123 }
125 /* Get servername */
126 $server = $_SERVER['SERVER_NAME'];
127 if(tests::is_ip($server)){
128 $server_name = @gethostbyaddr($server);
129 }else{
130 $server_name = @gethostbyaddr(gethostbyname($server));
131 }
132 $this->ppdServerPart = $method.str_replace("//","/",$server_name."/ppd");
133 }
135 /* Extract selected ppd */
136 if(isset($this->gotoPrinterPPD)){
137 $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
138 }
140 $this->initial_PPD = $this->gotoPrinterPPD;
142 /* Prepare different member types */
143 foreach(array("AddUser" =>"gotoUserPrinter",
144 "AddGroup" =>"gotoGroupPrinter",
145 "AddAdminUser" =>"gotoUserAdminPrinter",
146 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
148 /* $this->members contains all members */
149 $this->member[$type]=array();
151 if(isset($this->attrs[$attr])){
152 $ldap->cd($this->config->current['BASE']) ;
153 for($i = 0 ; $i < $this->attrs[$attr]['count']; $i++){
155 $mem = $this->attrs[$attr][$i];
156 if(preg_match("/Group/",$type)){
157 $ldap->search("(&(|(objectClass=posixGroup)(objectClass=gosaGroupOfNames))(cn=".$mem."))",array("cn","description"));
158 if($ldap->count()){
159 $entry = $ldap->fetch();
160 $this->member[$type][$entry['cn'][0]]=$entry;
161 }
162 }else{
163 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
164 if($ldap->count()){
165 $entry = $ldap->fetch();
166 $this->member[$type][$entry['uid'][0]]=$entry;
167 }
168 }
169 }
170 }
171 }
172 $this->orig_cn = $this->cn;
173 $this->orig_base = $this->base;
174 }
176 function set_acl_base($base)
177 {
178 plugin::set_acl_base($base);
179 if(is_object($this->netConfigDNS)){
180 $this->netConfigDNS->set_acl_base($base);
181 }
182 }
184 function set_acl_category($cat)
185 {
186 plugin::set_acl_category($cat);
187 if(is_object($this->netConfigDNS)){
188 $this->netConfigDNS->set_acl_category($cat);
189 }
190 }
192 /* Detect type of printer.
193 * Printer can be stand alone, belong to a workstation or belong to a terminal.
194 * We can detect the type printer type when comparing the tabs objects
195 */
196 function getTypeOfPrinter($UpdateAccountStatus = false)
197 {
198 /* Disable account as default
199 */
200 $this->is_account = $this->initially_was_account = false;
202 /* Detect type of printer via parent tabs.
203 */
205 $class = get_class($this->parent);
206 if(isset($this->parent->by_object['workgeneric'])){
208 /* Exclude templates
209 */
210 $this->cn = $this->parent->by_object['workgeneric']->cn;
211 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
212 $this->BelongsTo = "WorkstationTemplate";
213 }else{
214 $this->BelongsTo = "Workstation";
215 }
216 }elseif(isset($this->parent->by_object['termgeneric'])){
218 /* Exclude templates
219 */
220 $this->cn = $this->parent->by_object['termgeneric']->cn;
221 if($this->parent->by_object['termgeneric']->cn == "default"){
222 $this->BelongsTo = "TerminalTemplate";
223 }else{
224 $this->BelongsTo = "Terminal";
225 }
226 }elseif(isset($this->parent->by_name['printgeneric'])){
227 $this->BelongsTo = "Printer";
228 }
230 if($UpdateAccountStatus){
232 /* Set is_account / was account
233 */
234 if($this->dn == "new"){
235 $this->initially_was_account = false;
236 }
238 /* If is printer it must be a true account.
239 */
240 if(preg_match("/printer/i",$this->BelongsTo)){
241 $this->is_account = true;
242 }
244 /* Update dn, to ensure storing as printer instead of WS / terminal
245 */
246 if(preg_match("/terminal/i",$this->BelongsTo)){
247 $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
248 }
250 if(preg_match("/workstation/i",$this->BelongsTo)){
251 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
252 }
254 /* Detect if this is a valid printer account;
255 */
256 $ldap = $this->config->get_ldap_link();
257 $ldap->cat($this->dn, array('objectClass'));
259 if($ldap->count()){
260 $attrs = $ldap->fetch();
261 if(in_array("gotoPrinter",$attrs['objectClass'])){
262 $this->initially_was_account = true;
263 $this->is_account = true;
264 }else{
265 $this->is_account = false;
266 }
267 }
268 }
269 }
271 function execute()
272 {
273 /* Call parent execute */
274 plugin::execute();
276 if($this->is_account && !$this->view_logged){
277 $this->view_logged = TRUE;
278 new log("view","printer/".get_class($this),$this->dn);
279 }
281 /* If type of printer couldn't be detected (because of missing parent object in construction)
282 * hide this tab.
283 */
284 if(preg_match("/unknown/i",$this->BelongsTo)){
285 $display= $this->show_enable_header(_("Add printer extension"),
286 _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
287 return($display);
288 }
290 /* Templates can't have printer extensions
291 */
292 if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
293 $display= $this->show_enable_header(_("Add printer extension"),
294 _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
295 return($display);
296 }
297 if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
298 $display= $this->show_enable_header(_("Add printer extension"),
299 _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
300 return($display);
301 }
303 /* Get cn from base object */
304 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
305 $this->cn = $this->parent->by_object['workgeneric']->cn;
306 }
307 if(preg_match("/^Terminal$/i",$this->BelongsTo)){
308 $this->cn = $this->parent->by_object['termgeneric']->cn;
309 }
311 $smarty= get_smarty();
313 /* Assign acls */
314 $tmp = $this->plInfo();
315 foreach($tmp['plProvidedAcls'] as $name => $translation){
316 $smarty->assign($name."ACL", $this->getacl($name));
317 }
319 $display="";
321 /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
322 if(preg_match("/^Printer$/i",$this->BelongsTo)){
323 $smarty->assign("StandAlone",true);
324 }else{
325 $smarty->assign("StandAlone",false);
326 }
328 /* Do we need to flip is_account state? */
329 if(isset($_POST['modify_state'])){
330 if($this->is_account && $this->acl_is_removeable()){
331 $this->is_account= FALSE;
332 }elseif(!$this->is_account && $this->acl_is_createable()){
333 $this->is_account= TRUE;
334 }
335 }
337 /* Do we represent a valid printer? */
338 if (!$this->is_account && $this->parent === NULL){
339 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
340 msgPool::noValidExtension(_("printer"))."</b>";
341 return($display);
342 }
344 /* If this is a WS / Terminal depending printer, display account state button */
345 if(!preg_match("/^Printer$/i",$this->BelongsTo)){
346 if($this->cn == "" && ($this->dn != "new")){
347 $display= $this->show_enable_header(_("Add printer extension"),
348 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);
349 $this->is_account= false;
350 return $display;
351 }
353 if (($this->is_account)){
354 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
355 $display= $this->show_disable_header(_("Remove printer extension"),
356 msgPool::featuresEnabled(_("printer")));
357 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
358 $display= $this->show_disable_header(_("Remove printer extension"),
359 msgPool::featuresDisabled(_("printer")));
360 }
361 }else{
362 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
363 $display= $this->show_enable_header(_("Add printer extension"),
364 msgPool::featuresEnabled(_("printer")));
365 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
366 $display= $this->show_enable_header(_("Add printer extension"),
367 msgPool::featuresDisabled(_("printer")));
368 }
369 return ($display);
370 }
371 }
373 /* Base select dialog */
374 $once = true;
375 foreach($_POST as $name => $value){
376 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
377 $once = false;
378 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
379 $this->dialog->setCurrentBase($this->base);
380 $this->baseSelection = true;
381 }
382 }
384 /* Dialog handling for base select dialog
385 * Check if base was selected, dialog aborted etc */
386 if(is_object($this->dialog)){
388 $this->dialog->save_object();
389 if($this->baseSelection){
390 if($this->dialog->isClosed()){
391 $this->dialog = false;
392 $this->baseSelection = false;
393 }elseif($this->dialog->isSelected()){
395 /* A new base was selected, check if it is a valid one */
396 $tmp = $this->get_allowed_bases();
397 if(isset($tmp[$this->dialog->isSelected()])){
398 $this->base = $this->dialog->isSelected();
399 }
401 $this->dialog= false;
402 $this->baseSelection = false;
403 }else{
404 return($this->dialog->execute());
405 }
406 }
407 }
409 /* Fill templating stuff */
410 $smarty->assign("bases", $this->get_allowed_bases());
411 $smarty->assign("base_select", $this->base);
413 /* Assign attributes */
414 foreach ($this->attributes as $attr){
415 $smarty->assign("$attr", $this->$attr);
416 }
418 // Act on add user/grouo requests
419 if(isset($_POST['AddUser'])){
420 $this->userSelect = new userGroupSelect($this->config, get_userinfo());
421 $this->dialog = TRUE;
422 }
423 if(isset($_POST['AddAdminUser'])){
424 $this->adminUserSelect = new userGroupSelect($this->config, get_userinfo());
425 $this->dialog = TRUE;
426 }
428 /* Display ppd configure/select dialog */
429 if(isset($_POST['EditDriver'])){
430 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
431 $this->dialog = $this->PPDdialogToSave;
432 }else{
434 if(is_array($this->gotoPrinterPPD)){
435 $this->dialog = new printerPPDDialog($this->config, $this->dn,"");
436 }else{
437 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
438 }
439 $this->dialog->cn= $this->cn;
440 }
442 // Detect edit acl base
443 $ldap = $this->config->get_ldap_link();
444 if($ldap->dn_exists($this->dn)){
445 $acl_base = $this->dn;
446 }else{
447 $acl_base = $this->base;
448 }
450 $this->dialog->set_acl_base($acl_base);
451 }
453 /* remove ppd */
454 if(isset($_POST['RemoveDriver'])){
455 /* Detect PPD server part */
456 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
457 $method="https://";
458 }else{
459 $method="http://";
460 }
462 /* Get servername */
463 $server = $_SERVER['SERVER_NAME'];
464 if(tests::is_ip($server)){
465 $server_name = @gethostbyaddr($server);
466 }else{
467 $server_name = @gethostbyaddr(gethostbyname($server));
468 }
469 $this->ppdServerPart = $method.str_replace("//","/",$server_name."/ppd");
470 $this->gotoPrinterPPD = array();
471 $this->PPDdialogToSave = NULL;
472 }
474 /* Close ppd dialog */
475 if(isset($_POST['ClosePPD'])){
476 unset($this->dialog);
477 $this->dialog=FALSE;
478 }
480 /* Save selected ppd */
481 if(isset($_POST['SavePPD'])){
482 $this->dialog->save_object();
483 if(count($this->dialog->check())){
484 foreach($this->dialog->check() as $msg){
485 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
486 }
487 }else{
488 $this->gotoPrinterPPD = array();
489 $this->gotoPrinterPPD = $this->dialog->save();
490 $this->PPDdialogToSave = $this->dialog;
491 unset($this->dialog);
492 $this->dialog=FALSE;
493 }
494 }
496 /* Member management, delete user / group / admin ..*/
497 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
498 foreach($_POST['UserMember'] as $mem){
499 $this->DelMember('AddUser',$mem);
500 }
501 }
503 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
504 foreach($_POST['UserMember'] as $mem){
505 $this->DelMember('AddGroup',$mem);
506 }
507 }
509 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
510 foreach($_POST['AdminMember'] as $mem){
511 $this->DelMember('AddAdminUser',$mem);
512 }
513 }
515 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
516 foreach($_POST['AdminMember'] as $mem){
517 $this->DelMember('AddAdminGroup',$mem);
518 }
519 }
521 // Abort user / group adding dialog
522 if(isset($_POST['userGroupSelect_cancel'])){
523 $this->dialog=FALSE;
524 $this->userSelect = NULL;
525 $this->adminUserSelect = NULL;
526 }
528 // Save selected users / groups
529 if(isset($_POST['userGroupSelect_save'])){
530 $users = array();
531 if($this->userSelect instanceOf userGroupSelect){
532 $users = $this->userSelect->save();
533 }elseif($this->adminUserSelect instanceOf userGroupSelect){
534 $users = $this->adminUserSelect->save();
535 }
536 foreach($users as $user){
537 print_a($user);
538 # foreach($data as $mem){
539 # $this->AddMember($new['type'], $mem['dn']);
540 # }
541 }
542 $this->dialog=FALSE;
543 $this->userSelect = NULL;
544 $this->adminUserSelect = NULL;
545 }
547 if($this->userSelect instanceOf userGroupSelect){
548 return($this->userSelect->execute());
549 }elseif($this->adminUserSelect instanceOf userGroupSelect){
550 return($this->adminUserSelect->execute());
551 }
554 /* Display dialog, if there is currently one open*/
555 if(is_object($this->dialog)){
556 $this->dialog->save_object();
557 $display = $this->dialog->execute();
558 return $display;
559 }
561 /* Parse selected ppd file */
562 $config = session::get('config');
563 if ($config->get_cfg_value("ppdPath") != ""){
564 $path = $config->get_cfg_value("ppdPath");
565 if(!preg_match("/\/$/",$path)){
566 $path = $path."/";
567 }
569 $ppdManager= new ppdManager($path);
570 $smarty->assign("displayServerPath",true);
571 if(!empty($this->gotoPrinterPPD)){
572 if((!file_exists($path.$this->gotoPrinterPPD))){
573 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
574 }else{
575 $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD);
576 $smarty->assign("driverInfo", $ppdDesc['name']);
577 }
578 }else{
579 $smarty->assign("driverInfo", _("Not defined"));
580 $smarty->assign("displayServerPath",false);
581 }
582 }else{
583 $smarty->assign("driverInfo",_("Can't get ppd informations."));
584 $smarty->assign("displayServerPath",true);
585 }
586 $smarty->assign("ppdServerPart",$this->ppdServerPart);
589 /* Create user & admin user list */
590 $list=$this->generateList();
591 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
592 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
594 asort($userlist);
595 asort($adminlist);
597 if(!preg_match("/Printer/i",$this->BelongsTo)){
598 if(preg_match("/Terminal/i",$this->BelongsTo)){
599 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
600 }else{
601 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
602 }
603 $smarty->assign("cnACL" , $this->getacl("cn",true));
604 }else{
605 $smarty->assign("desc" ,"");
606 }
607 $smarty->assign("UserMember" ,$this->UserMember);
608 $smarty->assign("UserMembers" ,$userlist);
609 $smarty->assign("UserMemberKeys",array_flip($userlist));
611 $smarty->assign("AdminMember" ,$this->AdminMember);
612 $smarty->assign("AdminMembers" ,$adminlist);
613 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
614 if(preg_match("/Printer/i",$this->BelongsTo)){
616 /* Show main page */
617 $str = $this->netConfigDNS->execute();
618 if(is_object($this->netConfigDNS->dialog)){
619 return($str);
620 }
621 $smarty->assign("netconfig", $str);
622 } else {
623 $smarty->assign("netconfig", "");
624 }
626 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
627 }
629 function remove_from_parent()
630 {
631 /* Only remove if there was initially an account */
632 if($this->initially_was_account){
634 /* Update dn, to ensure storing as printer instead of WS / terminal
635 */
636 if(preg_match("/terminal/i",$this->BelongsTo)){
637 $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/').",/i",get_ou('printerRDN'),$this->dn);
638 }
640 if(preg_match("/workstation/i",$this->BelongsTo)){
641 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/i",get_ou('printerRDN'),$this->dn);
642 }
644 /* Check if this dn points to a printer, to avoid deleting something else */
645 $ldap= $this->config->get_ldap_link();
646 $ldap->cat($this->dn, array('dn',"objectClass"));
647 if(!$ldap->count()){
648 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
649 return;
650 }
652 /* Check if obejct is a printer */
653 $CheckPrinter = $ldap->fetch();
654 if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
655 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
656 return;
657 }
659 /* Remove account & dns extension */
660 $this->netConfigDNS->remove_from_parent();
661 $ldap->rmdir($this->dn);
663 new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
665 if (!$ldap->success()){
666 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
667 }
668 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
670 /* Delete references to object groups */
671 $ldap->cd ($this->config->current['BASE']);
672 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
673 while ($ldap->fetch()){
674 $og= new ogroup($this->config, $ldap->getDN());
675 unset($og->member[$this->dn]);
676 $og->save ();
677 }
679 /* Remove previously selected ppd file.*/
680 if(!empty($this->initial_PPD)){
681 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
682 $tmp->removeModifiedPPD();
683 }
684 }
685 }
688 /* Save data to object */
689 function save_object()
690 {
691 /* Create a base backup and reset the
692 base directly after calling plugin::save_object();
693 Base will be set seperatly a few lines below */
694 $base_tmp = $this->base;
695 plugin::save_object();
696 $this->base = $base_tmp;
698 if(isset($_POST['ppdServerPart'])){
699 $this->ppdServerPart = get_post('ppdServerPart');
700 }
702 if(is_object($this->netConfigDNS)){
703 $this->netConfigDNS->save_object();
704 }
706 /* Set new base if allowed */
707 $tmp = $this->get_allowed_bases();
708 if(isset($_POST['base'])){
709 if(isset($tmp[$_POST['base']])){
710 $this->base= $_POST['base'];
711 }
712 }
713 }
715 /* Check supplied data */
716 function check()
717 {
718 /* Call common method to give check the hook */
719 $message= plugin::check();
720 if (preg_match("/printer/i",$this->BelongsTo)){
721 $message= array_merge($message, $this->netConfigDNS->check());
722 }
724 /* Don't display check messages if this is a template object */
725 if(isset($this->parent->by_object['workgeneric'])){
726 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
727 return $message;
728 }
729 }elseif(isset($this->parent->by_object['termgeneric'])){
730 if($this->parent->by_object['termgeneric']->cn == "default"){
731 return $message;
732 }
733 }
735 $dn= "cn=".$this->cn.get_ou('printerRDN').",".$this->base;
737 /* must: cn */
738 if(($this->BelongsTo == "Printer") && $this->cn == ""){
739 $message[]= msgPool::required(_("Name"));
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 "gotoPrinterPPD" => _("Printer PPD"),
1009 "gotoUserPrinter" => _("Permissions"))
1010 ));
1011 }
1014 /* Delete member */
1015 function DelMember($type,$id)
1016 {
1017 /* Check if there was a printer "dn" given, or the "cn" */
1018 foreach($this->member[$type] as $key => $printer){
1019 if($printer['dn'] == $id) {
1020 $id = $key;
1021 }
1022 }
1024 if(!$this->acl_is_writeable("gotoUserPrinter")){
1025 msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
1026 return(FALSE);
1027 }
1029 if(isset($this->member[$type][$id])){
1030 unset($this->member[$type][$id]);
1031 return(TRUE);
1032 }
1033 return(FALSE);
1034 }
1037 /* Add given obejct to members */
1038 function AddMember($type,$dn)
1039 {
1040 $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
1041 if(!in_array_ics($type, $types)){
1042 msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
1043 return(FALSE);
1044 }
1046 if(!$this->acl_is_writeable("gotoUserPrinter")){
1047 msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
1048 return(FALSE);
1049 }
1051 /* Get name of index attributes */
1052 if(preg_match("/user/i",$type)){
1053 $var = "uid";
1054 }else{
1055 $var = "cn";
1056 }
1058 $ldap = $this->config->get_ldap_link();
1059 $ldap->cd($dn);
1060 $ldap->cat($dn,array($var,"cn"));
1061 if($ldap->count()){
1063 $attrs = $ldap->fetch();
1065 if(isset($attrs[$var][0])){
1066 $name = $attrs[$var][0];
1068 /* Check if this uid/cn is already assigned to any permission */
1069 foreach($types as $ctype){
1071 /* If we want to add a user, only check user/userAdmin members */
1072 if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
1073 continue;
1074 }
1076 /* If we want to add a group, only check groups/adminGroups .. */
1077 if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1078 continue;
1079 }
1081 if(isset( $this->member[$ctype][$name])){
1082 msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1083 return(FALSE);
1084 }
1085 }
1087 /* Everything is fine. So add the given object to members */
1088 $this->member[$type][$attrs[$var][0]] = $attrs ;
1089 }else{
1090 print_a($attrs);
1091 }
1092 }else{
1093 msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1094 return(FALSE);
1095 }
1096 return(TRUE);
1097 }
1100 /* Display generic part for server copy & paste */
1101 function getCopyDialog()
1102 {
1103 $vars = array("cn");
1104 $smarty = get_smarty();
1105 $smarty->assign("cn" ,$this->cn);
1106 $smarty->assign("object","printer");
1107 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1108 $ret = array();
1109 $ret['string'] = $str;
1110 $ret['status'] = "";
1111 return($ret);
1112 }
1115 function saveCopyDialog()
1116 {
1117 if(isset($_POST['cn'])){
1118 $this->cn = $_POST['cn'];
1119 }
1120 }
1122 function PrepareForCopyPaste($source)
1123 {
1124 plugin::PrepareForCopyPaste($source);
1125 if(isset($source['macAddress'][0])){
1126 $this->netConfigDNS->macAddress = $source['macAddress'][0];
1127 }
1128 if(isset($source['ipHostNumber'][0])){
1129 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1130 }
1132 $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1133 foreach($this->attributes as $attr){
1134 $this->$attr = $source_o->$attr;
1135 }
1136 $this->member = $source_o -> member;
1138 $this->gotoPrinterPPD = "";
1139 }
1142 function is_modal_dialog()
1143 {
1144 return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
1145 }
1147 }
1149 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1150 ?>