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 = "";
19 var $UserMember ="";
20 var $UserMembers =array();
21 var $UserMemberKeys =array();
23 var $AdminMember ="";
24 var $AdminMembers =array();
25 var $AdminMemberKeys =array();
27 var $PPDdialogToSave = NULL;
28 var $BelongsTo = "unknown"; // Specifies if this is a standalone printer, or belongs to a terminal / WS
30 var $member =array();
31 var $strings = "";
32 var $netConfigDNS;
33 var $baseSelection = false;
34 var $macAddress = "";
36 var $gotoUserAdminPrinter;
37 var $gotoGroupAdminPrinter ;
38 var $gotoGroupPrinter;
39 var $gotoUserPrinter ;
41 /* attribute list for save action */
42 var $attributes = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress",
43 "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter","gosaUnitTag");
44 var $objectclasses = array("top", "gotoPrinter");
45 var $view_logged = FALSE;
46 var $parent;
48 function printgeneric (&$config, $dn,$parent_init,$parent)
49 {
50 $this->config = &$config;
51 $this->dn = $dn;
53 /* If parent was posted(the tabs object) we can detect the printer type. */
54 if($parent){
55 $this->parent = $parent;
56 $this->getTypeOfPrinter();
57 }else{
58 $this->BelongsTo = "unknown";
59 return;
60 }
62 /* Update dn, to ensure storing as printer instead of WS / terminal */
63 if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
64 $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou'))."/",get_ou('printerou'),$this->dn);
65 }
67 if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
68 $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
69 }
71 $this->orig_dn = $this->dn;
73 /* Get printer settings, possibly dn has changed */
74 plugin::plugin ($config, $this->dn);
76 /* Get is_account initially_was_account status */
77 $this->getTypeOfPrinter(true);
79 /* set orig dn to new if object is new */
80 $ldap= $this->config->get_ldap_link();
81 $ldap->cat($this->dn, array('dn'));
82 if(!$ldap->count()){
83 $this->orig_dn = "new";
84 }
86 /* create dns object */
87 $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
89 /* Set base */
90 if ($this->dn == "new"){
91 $ui= get_userinfo();
92 $this->base= dn2base($ui->dn);
93 $this->cn= "";
94 } else {
96 /* Set base and check if the extracted base exists */
97 if(preg_match("/".normalizePreg(get_ou('incomingou'))."/",$this->dn)){
98 $this->base= preg_replace("/".normalizePreg(get_ou('incomingou'))."/","",dn2base($this->dn));
99 }else{
100 $this->base= preg_replace("/".normalizePreg(get_ou('printerou'))."/","",dn2base($this->dn));
101 }
103 if(!isset($this->config->idepartments[$this->base])){
104 msg_dialog::display(_("Internal error"), sprintf(_("Cannot determine a valid department for this object. Setting base to '%s'!"), session::get('CurrentMainBase')) , WARNING_DIALOG);
105 $this->base = session::get('CurrentMainBase');
106 }
107 }
109 /* Extract selected ppd */
110 if(isset($this->gotoPrinterPPD)){
111 $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
112 }
114 $this->initial_PPD = $this->gotoPrinterPPD;
116 /* Prepare different member types */
117 foreach(array("AddUser" =>"gotoUserPrinter",
118 "AddGroup" =>"gotoGroupPrinter",
119 "AddAdminUser" =>"gotoUserAdminPrinter",
120 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
122 /* $this->members contains all members */
123 $this->member[$type]=array();
125 if (isset($this->attrs[$attr]['count'])) {
126 unset($this->attrs[$attr]['count']);
127 }
129 if(isset($this->attrs[$attr])){
130 foreach($this->attrs[$attr] as $mem){
131 if(preg_match("/Group/",$type)){
132 $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
133 if($ldap->count()){
134 $entry = $ldap->fetch();
135 $this->member[$type][$entry['cn'][0]]=$entry;
136 }
137 }else{
138 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
139 if($ldap->count()){
140 $entry = $ldap->fetch();
141 $this->member[$type][$entry['uid'][0]]=$entry;
142 }
143 }
144 }
145 }
146 }
147 }
149 function set_acl_base($base)
150 {
151 plugin::set_acl_base($base);
152 if(is_object($this->netConfigDNS)){
153 $this->netConfigDNS->set_acl_base($base);
154 }
155 }
157 function set_acl_category($cat)
158 {
159 plugin::set_acl_category($cat);
160 if(is_object($this->netConfigDNS)){
161 $this->netConfigDNS->set_acl_category($cat);
162 }
163 }
165 /* Detect type of printer.
166 * Printer can be stand alone, belong to a workstation or belong to a terminal.
167 * We can detect the type printer type when comparing the tabs objects
168 */
169 function getTypeOfPrinter($UpdateAccountStatus = false)
170 {
171 /* Disable account as default
172 */
173 $this->is_account = $this->initially_was_account = false;
175 /* Detect type of printer via parent tabs.
176 */
178 $class = get_class($this->parent);
179 if(isset($this->parent->by_object['workgeneric'])){
181 /* Exclude templates
182 */
183 $this->cn = $this->parent->by_object['workgeneric']->cn;
184 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
185 $this->BelongsTo = "WorkstationTemplate";
186 }else{
187 $this->BelongsTo = "Workstation";
188 }
189 }elseif(isset($this->parent->by_object['termgeneric'])){
191 /* Exclude templates
192 */
193 $this->cn = $this->parent->by_object['termgeneric']->cn;
194 if($this->parent->by_object['termgeneric']->cn == "default"){
195 $this->BelongsTo = "TerminalTemplate";
196 }else{
197 $this->BelongsTo = "Terminal";
198 }
199 }elseif(isset($this->parent->by_name['printgeneric'])){
200 $this->BelongsTo = "Printer";
201 }
203 if($UpdateAccountStatus){
205 /* Set is_account / was account
206 */
207 if($this->dn == "new"){
208 $this->initially_was_account = false;
209 }
211 /* If is printer it must be a true account.
212 */
213 if(preg_match("/printer/i",$this->BelongsTo)){
214 $this->is_account = true;
215 }
217 /* Update dn, to ensure storing as printer instead of WS / terminal
218 */
219 if(preg_match("/terminal/i",$this->BelongsTo)){
220 $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou'))."/",get_ou('printerou'),$this->dn);
221 }
223 if(preg_match("/workstation/i",$this->BelongsTo)){
224 $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
225 }
227 /* Detect if this is a valid printer account;
228 */
229 $ldap = $this->config->get_ldap_link();
230 $ldap->cat($this->dn, array('objectClass'));
232 if($ldap->count()){
233 $attrs = $ldap->fetch();
234 if(in_array("gotoPrinter",$attrs['objectClass'])){
235 $this->initially_was_account = true;
236 $this->is_account = true;
237 }else{
238 $this->is_account = false;
239 }
240 }
241 }
242 }
244 function execute()
245 {
246 /* Call parent execute */
247 plugin::execute();
249 if($this->is_account && !$this->view_logged){
250 $this->view_logged = TRUE;
251 new log("view","printer/".get_class($this),$this->dn);
252 }
255 /* If type of printer couldn't be detected (because of missing parent object in construction)
256 * hide this tab.
257 */
258 if(preg_match("/unknown/i",$this->BelongsTo)){
259 $display= $this->show_enable_header(_("Add printer extension"),
260 _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
261 return($display);
262 }
264 /* Templates can't have printer extensions
265 */
266 if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
267 $display= $this->show_enable_header(_("Add printer extension"),
268 _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
269 return($display);
270 }
271 if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
272 $display= $this->show_enable_header(_("Add printer extension"),
273 _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
274 return($display);
275 }
277 /* Get cn from base object */
278 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
279 $this->cn = $this->parent->by_object['workgeneric']->cn;
280 }
281 if(preg_match("/^Terminal$/i",$this->BelongsTo)){
282 $this->cn = $this->parent->by_object['termgeneric']->cn;
283 }
285 $smarty= get_smarty();
287 /* Assign acls */
288 $tmp = $this->plInfo();
289 foreach($tmp['plProvidedAcls'] as $name => $translation){
290 $smarty->assign($name."ACL", $this->getacl($name));
291 }
293 $display="";
295 /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
296 if(preg_match("/^Printer$/i",$this->BelongsTo)){
297 $smarty->assign("StandAlone",true);
298 }else{
299 $smarty->assign("StandAlone",false);
300 }
302 /* Do we need to flip is_account state? */
303 if(isset($_POST['modify_state'])){
304 if($this->is_account && $this->acl_is_removeable()){
305 $this->is_account= FALSE;
306 }elseif(!$this->is_account && $this->acl_is_createable()){
307 $this->is_account= TRUE;
308 }
309 }
311 /* Do we represent a valid printer? */
312 if (!$this->is_account && $this->parent === NULL){
313 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
314 msgPool::noValidExtension(_("printer"))."</b>";
315 return($display);
316 }
318 /* If this is a WS / Terminal depending printer, display account state button */
319 if(!preg_match("/^Printer$/i",$this->BelongsTo)){
320 if((empty($this->cn)) && ($this->dn != "new")){
321 $display= $this->show_enable_header(_("Add printer extension"),
322 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);
323 $this->is_account= false;
324 return $display;
325 }
327 if (($this->is_account)){
328 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
329 $display= $this->show_disable_header(_("Remove printer extension"),
330 msgPool::featuresEnabled(_("printer")));
331 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
332 $display= $this->show_disable_header(_("Remove printer extension"),
333 msgPool::featuresDisabled(_("printer")));
334 }
335 }else{
336 if(preg_match("/^Workstation$/i",$this->BelongsTo)){
337 $display= $this->show_disable_header(_("Add printer extension"),
338 msgPool::featuresEnabled(_("printer")));
339 }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
340 $display= $this->show_enable_header(_("Add printer extension"),
341 msgPool::featuresDisabled(_("printer")));
342 }
343 return ($display);
344 }
345 }
347 /* Base select dialog */
348 $once = true;
349 foreach($_POST as $name => $value){
350 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
351 $once = false;
352 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
353 $this->dialog->setCurrentBase($this->base);
354 $this->baseSelection = true;
355 }
356 }
358 /* Dialog handling for base select dialog
359 * Check if base was selected, dialog aborted etc */
360 if(is_object($this->dialog)){
362 $this->dialog->save_object();
363 if($this->baseSelection){
364 if($this->dialog->isClosed()){
365 $this->dialog = false;
366 $this->baseSelection = false;
367 }elseif($this->dialog->isSelected()){
369 /* A new base was selected, check if it is a valid one */
370 $tmp = $this->get_allowed_bases();
371 if(isset($tmp[$this->dialog->isSelected()])){
372 $this->base = $this->dialog->isSelected();
373 }
375 $this->dialog= false;
376 $this->baseSelection = false;
377 }else{
378 return($this->dialog->execute());
379 }
380 }
381 }
383 /* Fill templating stuff */
384 $smarty->assign("bases", $this->get_allowed_bases());
385 $smarty->assign("base_select", $this->base);
387 /* Assign attributes */
388 foreach ($this->attributes as $attr){
389 $smarty->assign("$attr", $this->$attr);
390 }
392 if(isset($_POST['AddUser'])){
393 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
394 }
395 if(isset($_POST['AddGroup'])){
396 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
397 }
398 if(isset($_POST['AddAdminUser'])){
399 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
400 }
401 if(isset($_POST['AddAdminGroup'])){
402 $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
403 }
405 /* Display ppd configure/select dialog */
406 if(isset($_POST['EditDriver'])){
407 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
408 $this->dialog = $this->PPDdialogToSave;
409 }else{
410 $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
411 $this->dialog->cn= $this->cn;
412 }
413 }
415 /* remove ppd */
416 if(isset($_POST['RemoveDriver'])){
417 $this->gotoPrinterPPD = array();
418 $this->PPDdialogToSave = NULL;
419 }
421 /* Close ppd dialog */
422 if(isset($_POST['ClosePPD'])){
423 unset($this->dialog);
424 $this->dialog=FALSE;
425 }
427 /* Save selected ppd */
428 if(isset($_POST['SavePPD'])){
429 $this->dialog->save_object();
430 if(count($this->dialog->check())){
431 foreach($this->dialog->check() as $msg){
432 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
433 }
434 }else{
435 $this->gotoPrinterPPD = array();
436 $this->gotoPrinterPPD = $this->dialog->save();
437 $this->PPDdialogToSave = $this->dialog;
438 unset($this->dialog);
439 $this->dialog=FALSE;
440 }
441 }
443 /* Member management, delete user / group / admin ..*/
444 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
445 foreach($_POST['UserMember'] as $mem){
446 $this->DelMember('AddUser',$mem);
447 }
448 }
450 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
451 foreach($_POST['UserMember'] as $mem){
452 $this->DelMember('AddGroup',$mem);
453 }
454 }
456 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
457 foreach($_POST['AdminMember'] as $mem){
458 $this->DelMember('AddAdminUser',$mem);
459 }
460 }
462 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
463 foreach($_POST['AdminMember'] as $mem){
464 $this->DelMember('AddAdminGroup',$mem);
465 }
466 }
468 /* Abort user / group adding dialog */
469 if(isset($_POST['PrinterCancel'])){
470 unset($this->dialog);
471 $this->dialog= FALSE;
472 }
474 /* Save selected users / groups */
475 if(isset($_POST['PrinterSave'])){
476 $this->dialog->save_object();
477 if(count($this->dialog->check())){
478 foreach($this->dialog->check() as $msg){
479 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
480 }
481 }else{
482 $data= $new = $this->dialog->save();
483 unset($data['type']);
484 foreach($data as $mem){
485 $this->AddMember($new['type'], $mem['dn']);
486 }
487 unset($this->dialog);
488 $this->dialog=FALSE;
489 }
490 }
492 /* Display dialog, if there is currently one open*/
493 if(is_object($this->dialog)){
494 $this->dialog->save_object();
495 $display = $this->dialog->execute();
496 return $display;
497 }
499 /* Parse selected ppd file */
500 $config = session::get('config');
501 if((isset($config->data['MAIN']['PPD_PATH']))&&(is_dir($config->data['MAIN']['PPD_PATH']))){
503 $path = $config->data['MAIN']['PPD_PATH'];
504 if(!preg_match("/\/$/",$path)){
505 $path = $path."/";
506 }
508 $ppdManager= new ppdManager($path);
509 if(!empty($this->gotoPrinterPPD)){
510 if((!file_exists($path.$this->gotoPrinterPPD))){
511 $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
512 }else{
513 $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
514 }
515 }else{
516 $smarty->assign("driverInfo", _("Not defined"));
517 }
518 }else{
519 $smarty->assign("driverInfo",_("Can't get ppd informations."));
520 }
522 /* Create user & admin user list */
523 $list=$this->generateList();
524 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
525 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
527 asort($userlist);
528 asort($adminlist);
530 if(!preg_match("/Printer/i",$this->BelongsTo)){
531 if(preg_match("/Terminal/i",$this->BelongsTo)){
532 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("terminal"),"<b>".$this->cn."</b>"));
533 }else{
534 $smarty->assign("desc" ,sprintf(_("This printer belongs to %s. You can't rename this printer."),_("workstation"),"<b>".$this->cn."</b>"));
535 }
536 $smarty->assign("cnACL" , $this->getacl("cn",true));
537 }else{
538 $smarty->assign("desc" ,"");
539 }
540 $smarty->assign("UserMember" ,$this->UserMember);
541 $smarty->assign("UserMembers" ,$userlist);
542 $smarty->assign("UserMemberKeys",array_flip($userlist));
544 $smarty->assign("AdminMember" ,$this->AdminMember);
545 $smarty->assign("AdminMembers" ,$adminlist);
546 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
547 if(preg_match("/Printer/i",$this->BelongsTo)){
549 /* Show main page */
550 $str = $this->netConfigDNS->execute();
551 if(is_object($this->netConfigDNS->dialog)){
552 return($str);
553 }
554 $smarty->assign("netconfig", $str);
555 } else {
556 $smarty->assign("netconfig", "");
557 }
559 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
560 }
562 function remove_from_parent()
563 {
564 /* Only remove if there was initially an account */
565 if($this->initially_was_account){
567 /* Update dn, to ensure storing as printer instead of WS / terminal
568 */
569 if(preg_match("/terminal/i",$this->BelongsTo)){
570 $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou')).",/",get_ou('printerou'),$this->dn);
571 }
573 if(preg_match("/workstation/i",$this->BelongsTo)){
574 $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
575 }
577 /* Check if this dn points to a printer, to avoid deleting something else */
578 $ldap= $this->config->get_ldap_link();
579 $ldap->cat($this->dn, array('dn',"objectClass"));
580 if(!$ldap->count()){
581 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
582 return;
583 }
585 /* Check if obejct is a printer */
586 $CheckPrinter = $ldap->fetch();
587 if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
588 msg_dialog::display(_("Error"), _("Object is no printer!"), ERROR_DIALOG);
589 return;
590 }
592 /* Remove account & dns extension */
593 $this->netConfigDNS->remove_from_parent();
594 $ldap->rmdir($this->dn);
596 new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
598 if (!$ldap->success()){
599 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
600 }
601 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
603 /* Delete references to object groups */
604 $ldap->cd ($this->config->current['BASE']);
605 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
606 while ($ldap->fetch()){
607 $og= new ogroup($this->config, $ldap->getDN());
608 unset($og->member[$this->dn]);
609 $og->save ();
610 }
612 /* Remove previously selected ppd file.*/
613 if(!empty($this->initial_PPD)){
614 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
615 $tmp->removeModifiedPPD();
616 }
617 }
618 }
621 /* Save data to object */
622 function save_object()
623 {
624 /* Create a base backup and reset the
625 base directly after calling plugin::save_object();
626 Base will be set seperatly a few lines below */
627 $base_tmp = $this->base;
628 plugin::save_object();
629 $this->base = $base_tmp;
631 if(is_object($this->netConfigDNS)){
632 $this->netConfigDNS->save_object();
633 }
635 /* Set new base if allowed */
636 $tmp = $this->get_allowed_bases();
637 if(isset($_POST['base'])){
638 if(isset($tmp[$_POST['base']])){
639 $this->base= $_POST['base'];
640 }
641 }
642 }
644 /* Check supplied data */
645 function check()
646 {
647 /* Call common method to give check the hook */
648 $message= plugin::check();
649 if (preg_match("/printer/i",$this->BelongsTo)){
650 $message= array_merge($message, $this->netConfigDNS->check());
651 }
653 /* Don't display check messages if this is a template object */
654 if(isset($this->parent->by_object['workgeneric'])){
655 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
656 return $message;
657 }
658 }elseif(isset($this->parent->by_object['termgeneric'])){
659 if($this->parent->by_object['termgeneric']->cn == "default"){
660 return $message;
661 }
662 }
664 $dn= "cn=".$this->cn.get_ou('printerou').",".$this->base;
666 /* must: cn */
667 if(($this->BelongsTo == "Printer") && (empty($this->cn))){
668 $message[]= msgPool::required(_("Name"));
669 }
671 /* must: cn */
672 if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
673 $message[]= msgPool::invalid(_("Name"));
674 }
676 /* must: labeledURI */
677 if(empty($this->labeledURI)){
678 $message[]= msgPool::required(_("Printer URL"));
679 }
681 /* Check if there is already an entry with this cn*/
682 if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
683 $ldap= $this->config->get_ldap_link();
684 $ldap->cd ($this->base);
685 $ldap->ls("(cn=".$this->cn.")",get_ou('printerou').$this->base, array("cn"));
686 if ($ldap->count() != 0){
687 while ($attrs= $ldap->fetch()){
688 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
689 continue;
690 }
691 if ($attrs['dn'] != $this->orig_dn){
692 $message[]= msgPool::duplicated(_("Name"));
693 break;
694 }
695 }
696 }
697 }
699 return ($message);
700 }
703 /* Save to LDAP */
704 function save()
705 {
706 /* Update dn, to ensure storing as printer instead of WS / terminal
707 */
708 if(preg_match("/terminal/i",$this->BelongsTo)){
709 $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou')).",/",get_ou('printerou'),$this->dn);
710 }
712 if(preg_match("/workstation/i",$this->BelongsTo)){
713 $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
714 }
716 if(!$this->is_account) return;
717 if(isset($this->parent->by_object['workgeneric'])){
718 if($this->parent->by_object['workgeneric']->cn == "wdefault"){
719 return;
720 }
722 /* Adapt IP & mac from parent object */
723 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['workgeneric']->netConfigDNS->ipHostNumber;
724 $this->netConfigDNS->macAddress = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
726 }elseif(isset($this->parent->by_object['termgeneric'])){
727 if($this->parent->by_object['termgeneric']->cn == "default"){
728 return;
729 }
731 /* Adapt IP & mac from parent object */
732 $this->netConfigDNS->ipHostNumber = $this->parent->by_object['termgeneric']->netConfigDNS->ipHostNumber;
733 $this->netConfigDNS->macAddress = $this->parent->by_object['termgeneric']->netConfigDNS->macAddress;
734 }
736 /* If type is still unknown, the initialisation of this printer failed, abort. */
737 if(preg_match("/unknown/i",$this->BelongsTo)){
738 return;
739 }
741 /* save ppd configuration */
742 if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
743 $this->PPDdialogToSave->save_ppd();
744 }
745 if($this->orig_dn != $this->dn){
746 if(!empty($this->gotoPrinterPPD)) {
747 $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
748 $this->PPDdialogToSave->cn = $this->cn;
749 $this->PPDdialogToSave->generateProperties();
750 $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
751 }
752 }
754 /* Remove previously selected ppd file.*/
755 if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
756 if(!empty($this->initial_PPD)){
757 $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
758 $tmp->removeModifiedPPD();
759 }
760 }
762 if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
763 $method="https://";
764 }else{
765 $method="http://";
766 }
768 /* Get servername */
769 $server = $_SERVER['SERVER_NAME'];
770 if(TESTS::is_ip($server)){
771 $server_name = gethostbyaddr($server);
772 }else{
773 $server_name = gethostbyaddr(gethostbyname($server));
774 }
776 /* If no ppd is selected, remove this attribute */
777 if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
778 $this->gotoPrinterPPD = $method.str_replace("//","/",$server_name."/ppd/".$this->gotoPrinterPPD);
779 }else{
780 $this->gotoPrinterPPD = array();
781 }
783 $dn= $this->dn;
785 /* reduce objectClasses to minimun */
786 $this->attrs['objectClass']= $this->objectclasses;
788 plugin::save();
789 $ldap= $this->config->get_ldap_link();
791 /* Remove all empty values */
792 if ($this->orig_dn == 'new'){
793 $attrs= array();
794 foreach ($this->attrs as $key => $val){
795 if (is_array($val) && count($val) == 0){
796 continue;
797 }
798 $attrs[$key]= $val;
799 }
800 $this->attrs= $attrs;
801 }
803 /* Append printer user
804 */
805 $this->attrs['gotoUserPrinter']=array();
806 foreach($this->member['AddUser'] as $mem){
807 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
808 }
810 /* Append printer group
811 */
812 $this->attrs['gotoGroupPrinter'] = array();
813 foreach($this->member['AddGroup'] as $mem){
814 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
815 }
817 /* Append printer admin user
818 */
819 $this->attrs['gotoUserAdminPrinter'] = array();
820 foreach($this->member['AddAdminUser'] as $mem){
821 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
822 }
824 /* Append printer admin group
825 */
826 $this->attrs['gotoGroupAdminPrinter']= array();
827 foreach($this->member['AddAdminGroup'] as $mem){
828 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
829 }
831 if($this->orig_dn == 'new'){
832 foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
833 if(count($this->attrs[$checkVar]) == 0 || empty($this->attrs[$checkVar])){
834 unset($this->attrs[$checkVar]);
835 }
836 }
837 }
839 /* Ensure to create a new object */
840 if(preg_match("/".normalizePreg(get_ou('incomingou'))."/",$this->orig_dn)){
841 $this->orig_dn = "new";
842 }
844 /* Move object in necessary*/
845 if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
846 $this->move($this->orig_dn, $this->dn);
847 }
849 /* Write back to ldap */
850 $ldap= $this->config->get_ldap_link();
851 $ldap->cat($this->dn);
852 if(!$ldap->count()){
853 $ldap->cd($this->config->current['BASE']);
854 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
855 $ldap->cd($this->dn);
857 /* Remove empty values */
858 foreach($this->attrs as $name => $value){
859 if(empty($value)){
860 unset($this->attrs[$name]);
861 }
862 }
864 $ldap->add($this->attrs);
865 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
866 new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
867 } else {
868 $ldap->cd($this->dn);
869 $this->cleanup();
870 $ldap->modify ($this->attrs);
871 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
872 new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
873 }
874 if (!$ldap->success()){
875 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
876 }
878 #if(preg_match("/printer/i",$this->BelongsTo)){
879 $this->netConfigDNS->cn = $this->cn;
880 $this->netConfigDNS->dn = $this->dn;
881 $this->netConfigDNS->save();
882 #}
883 }
885 function generateList(){
886 $a_return=array();
888 foreach($this->member as $type => $values){
889 $a_return[$type]=array();
890 foreach($values as $value){
891 if((preg_match("/Group/i",$type))){
892 if(!isset($value['description'])){
893 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
894 }else{
895 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
896 }
897 }else{
898 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
899 }
900 }
901 }
902 return($a_return);
903 }
905 /* Return plugin informations for acl handling
906 #FIXME FAIscript seams to ununsed within this class... */
907 static function plInfo()
908 {
909 return (array(
910 "plShortName" => _("Generic"),
911 "plDescription" => _("Print generic"),
912 "plSelfModify" => FALSE,
913 "plDepends" => array(),
914 "plPriority" => 4,
915 "plSection" => array("administration"),
916 "plCategory" => array("printer" => array("description" => _("Printer"),
917 "objectClass" => "gotoPrinter"),"workstation","terminal"),
918 "plProvidedAcls"=> array(
919 "cn" => _("Name"),
920 "base" => _("Base") ,
921 "description" => _("Description"),
922 "l" => _("Location"),
923 "labeledURI" => _("LabeledURL"),
924 "gotoPrinterPPD" => _("Printer PPD"),
925 "gotoUserPrinter" => _("Permissions"))
926 ));
927 }
930 /* Delete member */
931 function DelMember($type,$id)
932 {
933 /* Check if there was a printer "dn" given, or the "cn" */
934 foreach($this->member[$type] as $key => $printer){
935 if($printer['dn'] == $id) {
936 $id = $key;
937 }
938 }
940 if(!$this->acl_is_writeable("gotoUserPrinter")){
941 msg_dialog::display(_("Permission error"), msgPool::permDelete(_("printer user"), $id), INFO_DIALOG);
942 return(FALSE);
943 }
945 if(isset($this->member[$type][$id])){
946 unset($this->member[$type][$id]);
947 return(TRUE);
948 }
949 return(FALSE);
950 }
953 /* Add given obejct to members */
954 function AddMember($type,$dn)
955 {
956 $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
957 if(!in_array_ics($type, $types)){
958 msg_dialog::display(_("Internal error"), sprintf(_("Illegal member type '%s'!"), $type), ERROR_DIALOG);
959 return(FALSE);
960 }
962 if(!$this->acl_is_writeable("gotoUserPrinter")){
963 msg_dialog::display(_("Permission error"), msgPool::permModify(_("printer user"), $this->dn), INFO_DIALOG);
964 return(FALSE);
965 }
967 /* Get name of index attributes */
968 if(preg_match("/user/i",$type)){
969 $var = "uid";
970 }else{
971 $var = "cn";
972 }
974 $ldap = $this->config->get_ldap_link();
975 $ldap->cd($dn);
976 $ldap->cat($dn,array($var,"cn"));
977 if($ldap->count()){
979 $attrs = $ldap->fetch();
981 if(isset($attrs[$var][0])){
982 $name = $attrs[$var][0];
984 /* Check if this uid/cn is already assigned to any permission */
985 foreach($types as $ctype){
987 /* If we want to add a user, only check user/userAdmin members */
988 if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
989 continue;
990 }
992 /* If we want to add a group, only check groups/adminGroups .. */
993 if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
994 continue;
995 }
997 if(isset( $this->member[$ctype][$name])){
998 msg_dialog::display(_("Error"), sprintf(_("'%s' is already used!"), $attrs[$var][0]), ERROR_DIALOG);
999 return(FALSE);
1000 }
1001 }
1003 /* Everything is fine. So add the given object to members */
1004 $this->member[$type][$attrs[$var][0]] = $attrs ;
1005 }else{
1006 print_a($attrs);
1007 }
1008 }else{
1009 msg_dialog::display(_("Error"), sprintf(_("'%s' does not exist!"), $dn), ERROR_DIALOG);
1010 return(FALSE);
1011 }
1012 return(TRUE);
1013 }
1016 /* Display generic part for server copy & paste */
1017 function getCopyDialog()
1018 {
1019 $vars = array("cn");
1020 $smarty = get_smarty();
1021 $smarty->assign("cn" ,$this->cn);
1022 $smarty->assign("object","printer");
1023 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1024 $ret = array();
1025 $ret['string'] = $str;
1026 $ret['status'] = "";
1027 return($ret);
1028 }
1031 function saveCopyDialog()
1032 {
1033 if(isset($_POST['cn'])){
1034 $this->cn = $_POST['cn'];
1035 }
1036 }
1038 function PrepareForCopyPaste($source)
1039 {
1040 plugin::PrepareForCopyPaste($source);
1041 if(isset($source['macAddress'][0])){
1042 $this->netConfigDNS->macAddress = $source['macAddress'][0];
1043 }
1044 if(isset($source['ipHostNumber'][0])){
1045 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1046 }
1048 $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1049 foreach($this->attributes as $attr){
1050 $this->$attr = $source_o->$attr;
1051 }
1052 $this->member = $source_o -> member;
1054 $this->gotoPrinterPPD = "";
1055 }
1056 }
1058 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1059 ?>