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'), '/')."/",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'), '/')."/",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'), '/')."/",$this->dn)){
102 $this->base= preg_replace("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/","",dn2base($this->dn));
103 }else{
104 $this->base= preg_replace("/".preg_quote(get_ou('printerRDN'), '/')."/","",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'), '/')."/",get_ou('printerRDN'),$this->dn);
248 }
250 if(preg_match("/workstation/i",$this->BelongsTo)){
251 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",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 if(isset($_POST['AddUser'])){
419 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser",$this->member);
420 }
421 if(isset($_POST['AddGroup'])){
422 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup",$this->member);
423 }
424 if(isset($_POST['AddAdminUser'])){
425 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser",$this->member);
426 }
427 if(isset($_POST['AddAdminGroup'])){
428 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup",$this->member);
429 }
431 /* Display ppd configure/select dialog */
432 if(isset($_POST['EditDriver'])){
433 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
434 $this->dialog = $this->PPDdialogToSave;
435 }else{
437 if(is_array($this->gotoPrinterPPD)){
438 $this->dialog = new printerPPDDialog($this->config, $this->dn,"");
439 }else{
440 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
441 }
442 $this->dialog->cn= $this->cn;
443 }
444 }
446 /* remove ppd */
447 if(isset($_POST['RemoveDriver'])){
448 /* Detect PPD server part */
449 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
450 $method="https://";
451 }else{
452 $method="http://";
453 }
455 /* Get servername */
456 $server = $_SERVER['SERVER_NAME'];
457 if(tests::is_ip($server)){
458 $server_name = @gethostbyaddr($server);
459 }else{
460 $server_name = @gethostbyaddr(gethostbyname($server));
461 }
462 $this->ppdServerPart = $method.str_replace("//","/",$server_name."/ppd");
463 $this->gotoPrinterPPD = array();
464 $this->PPDdialogToSave = NULL;
465 }
467 /* Close ppd dialog */
468 if(isset($_POST['ClosePPD'])){
469 unset($this->dialog);
470 $this->dialog=FALSE;
471 }
473 /* Save selected ppd */
474 if(isset($_POST['SavePPD'])){
475 $this->dialog->save_object();
476 if(count($this->dialog->check())){
477 foreach($this->dialog->check() as $msg){
478 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
479 }
480 }else{
481 $this->gotoPrinterPPD = array();
482 $this->gotoPrinterPPD = $this->dialog->save();
483 $this->PPDdialogToSave = $this->dialog;
484 unset($this->dialog);
485 $this->dialog=FALSE;
486 }
487 }
489 /* Member management, delete user / group / admin ..*/
490 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
491 foreach($_POST['UserMember'] as $mem){
492 $this->DelMember('AddUser',$mem);
493 }
494 }
496 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
497 foreach($_POST['UserMember'] as $mem){
498 $this->DelMember('AddGroup',$mem);
499 }
500 }
502 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
503 foreach($_POST['AdminMember'] as $mem){
504 $this->DelMember('AddAdminUser',$mem);
505 }
506 }
508 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
509 foreach($_POST['AdminMember'] as $mem){
510 $this->DelMember('AddAdminGroup',$mem);
511 }
512 }
514 /* Abort user / group adding dialog */
515 if(isset($_POST['PrinterCancel'])){
516 unset($this->dialog);
517 $this->dialog= FALSE;
518 }
520 /* Save selected users / groups */
521 if(isset($_POST['PrinterSave'])){
522 $this->dialog->save_object();
523 if(count($this->dialog->check())){
524 foreach($this->dialog->check() as $msg){
525 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
526 }
527 }else{
528 $data= $new = $this->dialog->save();
529 unset($data['type']);
530 foreach($data as $mem){
531 $this->AddMember($new['type'], $mem['dn']);
532 }
533 unset($this->dialog);
534 $this->dialog=FALSE;
535 }
536 }
538 /* Display dialog, if there is currently one open*/
539 if(is_object($this->dialog)){
540 $this->dialog->save_object();
541 $display = $this->dialog->execute();
542 return $display;
543 }
545 /* Parse selected ppd file */
546 $config = session::get('config');
547 if ($config->get_cfg_value("ppdPath") != ""){
548 $path = $config->get_cfg_value("ppdPath");
549 if(!preg_match("/\/$/",$path)){
550 $path = $path."/";
551 }
553 $ppdManager= new ppdManager($path);
554 $smarty->assign("displayServerPath",true);
555 if(!empty($this->gotoPrinterPPD)){
556 if((!file_exists($path.$this->gotoPrinterPPD))){
557 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
558 }else{
559 $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD);
560 $smarty->assign("driverInfo", $ppdDesc['name']);
561 }
562 }else{
563 $smarty->assign("driverInfo", _("Not defined"));
564 $smarty->assign("displayServerPath",false);
565 }
566 }else{
567 $smarty->assign("driverInfo",_("Can't get ppd informations."));
568 $smarty->assign("displayServerPath",true);
569 }
570 $smarty->assign("ppdServerPart",$this->ppdServerPart);
573 /* Create user & admin user list */
574 $list=$this->generateList();
575 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
576 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
578 asort($userlist);
579 asort($adminlist);
581 if(!preg_match("/Printer/i",$this->BelongsTo)){
582 if(preg_match("/Terminal/i",$this->BelongsTo)){
583 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
584 }else{
585 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
586 }
587 $smarty->assign("cnACL" , $this->getacl("cn",true));
588 }else{
589 $smarty->assign("desc" ,"");
590 }
591 $smarty->assign("UserMember" ,$this->UserMember);
592 $smarty->assign("UserMembers" ,$userlist);
593 $smarty->assign("UserMemberKeys",array_flip($userlist));
595 $smarty->assign("AdminMember" ,$this->AdminMember);
596 $smarty->assign("AdminMembers" ,$adminlist);
597 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
598 if(preg_match("/Printer/i",$this->BelongsTo)){
600 /* Show main page */
601 $str = $this->netConfigDNS->execute();
602 if(is_object($this->netConfigDNS->dialog)){
603 return($str);
604 }
605 $smarty->assign("netconfig", $str);
606 } else {
607 $smarty->assign("netconfig", "");
608 }
610 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
611 }
613 function remove_from_parent()
614 {
615 /* Only remove if there was initially an account */
616 if($this->initially_was_account){
618 /* Update dn, to ensure storing as printer instead of WS / terminal
619 */
620 if(preg_match("/terminal/i",$this->BelongsTo)){
621 $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/').",/",get_ou('printerRDN'),$this->dn);
622 }
624 if(preg_match("/workstation/i",$this->BelongsTo)){
625 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
626 }
628 /* Check if this dn points to a printer, to avoid deleting something else */
629 $ldap= $this->config->get_ldap_link();
630 $ldap->cat($this->dn, array('dn',"objectClass"));
631 if(!$ldap->count()){
632 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
633 return;
634 }
636 /* Check if obejct is a printer */
637 $CheckPrinter = $ldap->fetch();
638 if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
639 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
640 return;
641 }
643 /* Remove account & dns extension */
644 $this->netConfigDNS->remove_from_parent();
645 $ldap->rmdir($this->dn);
647 new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
649 if (!$ldap->success()){
650 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
651 }
652 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
654 /* Delete references to object groups */
655 $ldap->cd ($this->config->current['BASE']);
656 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
657 while ($ldap->fetch()){
658 $og= new ogroup($this->config, $ldap->getDN());
659 unset($og->member[$this->dn]);
660 $og->save ();
661 }
663 /* Remove previously selected ppd file.*/
664 if(!empty($this->initial_PPD)){
665 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
666 $tmp->removeModifiedPPD();
667 }
668 }
669 }
672 /* Save data to object */
673 function save_object()
674 {
675 /* Create a base backup and reset the
676 base directly after calling plugin::save_object();
677 Base will be set seperatly a few lines below */
678 $base_tmp = $this->base;
679 plugin::save_object();
680 $this->base = $base_tmp;
682 if(isset($_POST['ppdServerPart'])){
683 $this->ppdServerPart = get_post('ppdServerPart');
684 }
686 if(is_object($this->netConfigDNS)){
687 $this->netConfigDNS->save_object();
688 }
690 /* Set new base if allowed */
691 $tmp = $this->get_allowed_bases();
692 if(isset($_POST['base'])){
693 if(isset($tmp[$_POST['base']])){
694 $this->base= $_POST['base'];
695 }
696 }
697 }
699 /* Check supplied data */
700 function check()
701 {
702 /* Call common method to give check the hook */
703 $message= plugin::check();
704 if (preg_match("/printer/i",$this->BelongsTo)){
705 $message= array_merge($message, $this->netConfigDNS->check());
706 }
708 /* Don't display check messages if this is a template object */
709 if(isset($this->parent->by_object['workgeneric'])){
710 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
711 return $message;
712 }
713 }elseif(isset($this->parent->by_object['termgeneric'])){
714 if($this->parent->by_object['termgeneric']->cn == "default"){
715 return $message;
716 }
717 }
719 $dn= "cn=".$this->cn.get_ou('printerRDN').",".$this->base;
721 /* must: cn */
722 if(($this->BelongsTo == "Printer") && $this->cn == ""){
723 $message[]= msgPool::required(_("Name"));
724 }
726 /* must: cn */
727 if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
728 $message[]= msgPool::invalid(_("Name"));
729 }
731 /* must: labeledURI */
732 if(empty($this->labeledURI)){
733 $message[]= msgPool::required(_("Printer URL"));
734 }
736 /* Check if there is already an entry with this cn*/
737 if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
738 $ldap= $this->config->get_ldap_link();
739 $ldap->cd ($this->base);
740 $ldap->ls("(cn=".$this->cn.")",get_ou('printerRDN').$this->base, array("cn"));
741 if ($ldap->count() != 0){
742 while ($attrs= $ldap->fetch()){
743 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
744 continue;
745 }
746 if ($attrs['dn'] != $this->orig_dn){
747 $message[]= msgPool::duplicated(_("Name"));
748 break;
749 }
750 }
751 }
752 }
754 /* Check if we are allowed to create or move this object
755 */
756 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
757 $message[] = msgPool::permCreate();
758 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
759 $message[] = msgPool::permMove();
760 }
762 return ($message);
763 }
766 /* Save to LDAP */
767 function save()
768 {
769 /* Update dn, to ensure storing as printer instead of WS / terminal
770 */
771 if(preg_match("/terminal/i",$this->BelongsTo)){
772 $this->dn= preg_replace("/".preg_quote(get_ou('terminalRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
773 }
775 if(preg_match("/workstation/i",$this->BelongsTo)){
776 $this->dn= preg_replace("/".preg_quote(get_ou('workstationRDN'), '/')."/",get_ou('printerRDN'),$this->dn);
777 }
779 if(!$this->is_account) return;
780 if(isset($this->parent->by_object['workgeneric'])){
781 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
782 return;
783 }
785 /* Adapt IP & mac from parent object */
786 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
787 $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
789 }elseif(isset($this->parent->by_object['termgeneric'])){
790 if($this->parent->by_object['termgeneric']->cn == "default"){
791 return;
792 }
794 /* Adapt IP & mac from parent object */
795 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
796 $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
797 }
799 /* If type is still unknown, the initialisation of this printer failed, abort. */
800 if(preg_match("/unknown/i",$this->BelongsTo)){
801 return;
802 }
804 /* save ppd configuration */
805 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
806 $this->PPDdialogToSave->save_ppd();
808 /* Rename the generated ppd to match the gzip ending '.gz', if necessary.
809 */
810 $path = $this->config->get_cfg_value("ppdPath");
811 if(!preg_match("/\/$/",$path)){
812 $path = $path."/";
813 }
814 $ppdManager= new ppdManager($path);
815 if($ppdManager->useGzip && !preg_match('/\.gz$/',$this->gotoPrinterPPD)){
816 if(rename($path.$this->gotoPrinterPPD,$path.$this->gotoPrinterPPD.'.gz')){
817 $this->gotoPrinterPPD .= '.gz';
818 }
819 }elseif(!$ppdManager->useGzip && preg_match('/\.gz$/',$this->gotoPrinterPPD)){
820 $new_ppd = preg_replace('/\.gz$/','',$this->gotoPrinterPPD);
821 if(rename($path.$this->gotoPrinterPPD,$path.$new_ppd)){
822 $this->gotoPrinterPPD = $new_ppd;
823 }
824 }
825 }
826 if($this->orig_dn != $this->dn){
827 if(!empty($this->gotoPrinterPPD)) {
828 $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
829 $this->PPDdialogToSave->cn = $this->cn;
830 $this->PPDdialogToSave->generateProperties();
831 $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
832 }
833 }
835 /* Remove previously selected ppd file.*/
836 if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
837 if(!empty($this->initial_PPD)){
838 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
839 $tmp->removeModifiedPPD();
840 }
841 }
843 /* If no ppd is selected, remove this attribute */
844 if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
845 $this->gotoPrinterPPD = $this->ppdServerPart.'/'.$this->gotoPrinterPPD;
846 }else{
847 $this->gotoPrinterPPD = array();
848 }
850 $dn= $this->dn;
852 /* reduce objectClasses to minimun */
853 $this->attrs['objectClass']= $this->objectclasses;
855 plugin::save();
856 $ldap= $this->config->get_ldap_link();
858 /* Remove all empty values */
859 if ($this->orig_dn == 'new'){
860 $attrs= array();
861 foreach ($this->attrs as $key => $val){
862 if (is_array($val) && count($val) == 0){
863 continue;
864 }
865 $attrs[$key]= $val;
866 }
867 $this->attrs= $attrs;
868 }
870 /* Append printer user
871 */
872 $this->attrs['gotoUserPrinter']=array();
873 foreach($this->member['AddUser'] as $mem){
874 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
875 }
877 /* Append printer group
878 */
879 $this->attrs['gotoGroupPrinter'] = array();
880 foreach($this->member['AddGroup'] as $mem){
881 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
882 }
884 /* Append printer admin user
885 */
886 $this->attrs['gotoUserAdminPrinter'] = array();
887 foreach($this->member['AddAdminUser'] as $mem){
888 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
889 }
891 /* Append printer admin group
892 */
893 $this->attrs['gotoGroupAdminPrinter']= array();
894 foreach($this->member['AddAdminGroup'] as $mem){
895 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
896 }
898 if($this->orig_dn == 'new'){
899 foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
900 if(count($this->attrs[$checkVar]) == 0 || empty($this->attrs[$checkVar])){
901 unset($this->attrs[$checkVar]);
902 }
903 }
904 }
906 /* Ensure to create a new object */
907 if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'), '/')."/",$this->orig_dn)){
908 $this->orig_dn = "new";
909 }
911 /* Move object in necessary*/
912 if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
913 $this->move($this->orig_dn, $this->dn);
914 }
916 /* Write back to ldap */
917 $ldap= $this->config->get_ldap_link();
918 $ldap->cat($this->dn);
919 if(!$ldap->count()){
920 $ldap->cd($this->config->current['BASE']);
921 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
922 $ldap->cd($this->dn);
924 /* Remove empty values */
925 foreach($this->attrs as $name => $value){
926 if(empty($value)){
927 unset($this->attrs[$name]);
928 }
929 }
931 $ldap->add($this->attrs);
932 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
933 new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
934 } else {
935 $ldap->cd($this->dn);
936 $this->cleanup();
937 $ldap->modify ($this->attrs);
938 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
939 new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
940 }
941 if (!$ldap->success()){
942 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
943 }
945 #if(preg_match("/printer/i",$this->BelongsTo)){
946 $this->netConfigDNS->cn = $this->cn;
947 $this->netConfigDNS->dn = $this->dn;
948 $this->netConfigDNS->save();
949 #}
950 }
952 function generateList(){
953 $a_return=array();
955 foreach($this->member as $type => $values){
956 $a_return[$type]=array();
957 foreach($values as $value){
958 if((preg_match("/Group/i",$type))){
959 if(!isset($value['description'])){
960 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
961 }else{
962 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
963 }
964 }else{
965 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
966 }
967 }
968 }
969 return($a_return);
970 }
972 /* Return plugin informations for acl handling
973 #FIXME FAIscript seams to ununsed within this class... */
974 static function plInfo()
975 {
976 return (array(
977 "plShortName" => _("Generic"),
978 "plDescription" => _("Print generic"),
979 "plSelfModify" => FALSE,
980 "plDepends" => array(),
981 "plPriority" => 4,
982 "plSection" => array("administration"),
983 "plCategory" => array("printer" => array("description" => _("Printer"),
984 "objectClass" => "gotoPrinter"),"workstation","terminal"),
985 "plProvidedAcls"=> array(
986 "cn" => _("Name"),
987 "base" => _("Base") ,
988 "description" => _("Description"),
989 "l" => _("Location"),
990 "labeledURI" => _("LabeledURL"),
991 "gotoPrinterPPD" => _("Printer PPD"),
992 "gotoUserPrinter" => _("Permissions"))
993 ));
994 }
997 /* Delete member */
998 function DelMember($type,$id)
999 {
1000 /* Check if there was a printer "dn" given, or the "cn" */
1001 foreach($this->member[$type] as $key => $printer){
1002 if($printer['dn'] == $id) {
1003 $id = $key;
1004 }
1005 }
1007 if(!$this->acl_is_writeable("gotoUserPrinter")){
1008 msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
1009 return(FALSE);
1010 }
1012 if(isset($this->member[$type][$id])){
1013 unset($this->member[$type][$id]);
1014 return(TRUE);
1015 }
1016 return(FALSE);
1017 }
1020 /* Add given obejct to members */
1021 function AddMember($type,$dn)
1022 {
1023 $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
1024 if(!in_array_ics($type, $types)){
1025 msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
1026 return(FALSE);
1027 }
1029 if(!$this->acl_is_writeable("gotoUserPrinter")){
1030 msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
1031 return(FALSE);
1032 }
1034 /* Get name of index attributes */
1035 if(preg_match("/user/i",$type)){
1036 $var = "uid";
1037 }else{
1038 $var = "cn";
1039 }
1041 $ldap = $this->config->get_ldap_link();
1042 $ldap->cd($dn);
1043 $ldap->cat($dn,array($var,"cn"));
1044 if($ldap->count()){
1046 $attrs = $ldap->fetch();
1048 if(isset($attrs[$var][0])){
1049 $name = $attrs[$var][0];
1051 /* Check if this uid/cn is already assigned to any permission */
1052 foreach($types as $ctype){
1054 /* If we want to add a user, only check user/userAdmin members */
1055 if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
1056 continue;
1057 }
1059 /* If we want to add a group, only check groups/adminGroups .. */
1060 if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
1061 continue;
1062 }
1064 if(isset( $this->member[$ctype][$name])){
1065 msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
1066 return(FALSE);
1067 }
1068 }
1070 /* Everything is fine. So add the given object to members */
1071 $this->member[$type][$attrs[$var][0]] = $attrs ;
1072 }else{
1073 print_a($attrs);
1074 }
1075 }else{
1076 msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1077 return(FALSE);
1078 }
1079 return(TRUE);
1080 }
1083 /* Display generic part for server copy & paste */
1084 function getCopyDialog()
1085 {
1086 $vars = array("cn");
1087 $smarty = get_smarty();
1088 $smarty->assign("cn" ,$this->cn);
1089 $smarty->assign("object","printer");
1090 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1091 $ret = array();
1092 $ret['string'] = $str;
1093 $ret['status'] = "";
1094 return($ret);
1095 }
1098 function saveCopyDialog()
1099 {
1100 if(isset($_POST['cn'])){
1101 $this->cn = $_POST['cn'];
1102 }
1103 }
1105 function PrepareForCopyPaste($source)
1106 {
1107 plugin::PrepareForCopyPaste($source);
1108 if(isset($source['macAddress'][0])){
1109 $this->netConfigDNS->macAddress = $source['macAddress'][0];
1110 }
1111 if(isset($source['ipHostNumber'][0])){
1112 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1113 }
1115 $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1116 foreach($this->attributes as $attr){
1117 $this->$attr = $source_o->$attr;
1118 }
1119 $this->member = $source_o -> member;
1121 $this->gotoPrinterPPD = "";
1122 }
1123 }
1125 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1126 ?>