Code

Updated actions
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_printGeneric.inc
1 <?php
3 class printgeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage terminal base objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* Generic terminal attributes */
11   var $interfaces     = array();
12   var $ignore_account = FALSE;
14   /* Needed values and lists */
15   var $base             = "";
16   var $cn               = "";
17   var $l                = "";
18   var $description      = "";
19   var $labeledURI       = "";
20   var $gotoPrinterPPD   = "";
21   var $initial_PPD      = "";
22   var $orig_dn          = "";
24   var $UserMember       ="";
25   var $UserMembers      =array();
26   var $UserMemberKeys   =array();
28   var $AdminMember      ="";
29   var $AdminMembers     =array();
30   var $AdminMemberKeys  =array();
32   var $PPDdialogToSave  = NULL;
33   var $BelongsTo        = "unknown"; //  Specifies if this is a standalone printer, or belongs to a terminal / WS
35   var $member           =array();
36   var $strings          = "";
37   var $netConfigDNS;
38   var $baseSelection    = false;
39   var $macAddress       = "";
41   var $gotoUserAdminPrinter;
42   var $gotoGroupAdminPrinter ;
43   var $gotoGroupPrinter;
44   var $gotoUserPrinter ;
46   /* attribute list for save action */
47   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","gotoUserPrinter", "macAddress", 
48                               "gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter","gotoGroupPrinter");
49   var $objectclasses  = array("top", "gotoPrinter");
50   var $view_logged    = FALSE;
51   var $parent;
53   function printgeneric (&$config, $dn,$parent_init,$parent)
54   {
55     $this->config = &$config;
56     $this->dn = $dn; 
57  
58     /* If parent was posted(the tabs object) we can detect the printer type. */
59     if($parent){
60       $this->parent = $parent;
61       $this->getTypeOfPrinter();
62     }else{
63       $this->BelongsTo = "unknown";
64       return;
65     }
67     /* Update dn, to ensure storing as printer instead of WS / terminal */
68     if(preg_match("/Terminal/i",$this->BelongsTo) || preg_match("/TerminalTemplate/i",$this->BelongsTo)){
69       $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou'))."/",get_ou('printerou'),$this->dn);
70     }
72     if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
73       $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
74     }
76     $this->orig_dn = $this->dn;
78     /* Get printer settings, possibly dn has changed */
79     plugin::plugin ($config, $this->dn);
81     /* Get is_account initially_was_account status */
82     $this->getTypeOfPrinter(true);
84     /* set orig dn to new if object is new */
85     $ldap= $this->config->get_ldap_link();
86     $ldap->cat($this->dn, array('dn'));
87     if(!$ldap->count()){
88       $this->orig_dn = "new";
89     }
90     
91     /* create dns object */
92     $this->netConfigDNS = new termDNS($this->config, $this,$this->objectclasses);
94     /* Set base */
95     if ($this->dn == "new"){
96       $ui= get_userinfo();
97       $this->base= dn2base($ui->dn);
98       $this->cn= "";
99     } else {
100     
101       /* Set base and check if the extracted base exists */
102       if(preg_match("/".normalizePreg(get_ou('incomingou'))."/",$this->dn)){
103         $this->base= preg_replace("/".normalizePreg(get_ou('incomingou'))."/","",dn2base($this->dn));
104       }else{
105         $this->base= preg_replace("/".normalizePreg(get_ou('printerou'))."/","",dn2base($this->dn));
106       }
108       if(!isset($this->config->idepartments[$this->base])){
109         print_red(_("Can't extract a valid base out of object dn, setting base to '%s'."),session::get('CurrentMainBase'));
110         $this->base  = session::get('CurrentMainBase');
111       }
112     }
114     /* Extract selected ppd */
115     if(isset($this->gotoPrinterPPD)){
116       $this->gotoPrinterPPD = preg_replace("/^http.*ppd\//i","",$this->gotoPrinterPPD);
117     }
119     $this->initial_PPD = $this->gotoPrinterPPD;
121     /* Prepare different member types */ 
122     foreach(array("AddUser"       =>"gotoUserPrinter",
123           "AddGroup"      =>"gotoGroupPrinter",
124           "AddAdminUser"  =>"gotoUserAdminPrinter",
125           "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
127       /* $this->members contains all members */
128       $this->member[$type]=array();
130       if (isset($this->attrs[$attr]['count'])) {
131         unset($this->attrs[$attr]['count']);
132       }
134       if(isset($this->attrs[$attr])){
135         foreach($this->attrs[$attr] as $mem){
136           if(preg_match("/Group/",$type)){
137             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
138             if($ldap->count()){
139               $entry = $ldap->fetch();
140               $this->member[$type][$entry['cn'][0]]=$entry;
141             }
142           }else{
143             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
144             if($ldap->count()){
145               $entry = $ldap->fetch();
146               $this->member[$type][$entry['uid'][0]]=$entry;
147             }
148           }
149         }
150       }
151     }
152   }
154   function set_acl_base($base)
155   {
156     plugin::set_acl_base($base);
157     if(is_object($this->netConfigDNS)){
158       $this->netConfigDNS->set_acl_base($base);
159     }
160   }
162   function set_acl_category($cat)
163   {
164     plugin::set_acl_category($cat);
165     if(is_object($this->netConfigDNS)){
166       $this->netConfigDNS->set_acl_category($cat);
167     }
168   }
170   /* Detect type of printer.
171    * Printer can be stand alone, belong to a workstation or belong to a terminal. 
172    * We can detect the type printer type when comparing the tabs objects
173    */
174   function getTypeOfPrinter($UpdateAccountStatus = false)
175   {
176     /* Disable account as default
177      */  
178     $this->is_account = $this->initially_was_account = false;
180     /* Detect type of printer via parent tabs.
181      */
183     $class = get_class($this->parent);
184     if(isset($this->parent->by_object['workgeneric'])){
186       /* Exclude templates 
187        */
188       $this->cn = $this->parent->by_object['workgeneric']->cn;
189       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
190         $this->BelongsTo = "WorkstationTemplate";
191       }else{
192         $this->BelongsTo = "Workstation";
193       }
194     }elseif(isset($this->parent->by_object['termgeneric'])){
196       /* Exclude templates 
197        */
198       $this->cn = $this->parent->by_object['termgeneric']->cn;
199       if($this->parent->by_object['termgeneric']->cn == "default"){
200         $this->BelongsTo = "TerminalTemplate";  
201       }else{
202         $this->BelongsTo = "Terminal";
203       }
204     }elseif(isset($this->parent->by_name['printgeneric'])){
205       $this->BelongsTo  = "Printer";
206     }
208     if($UpdateAccountStatus){
210       /* Set is_account / was account 
211        */
212       if($this->dn == "new"){
213         $this->initially_was_account = false;
214       }
216       /* If is printer it must be a true account.
217        */
218       if(preg_match("/printer/i",$this->BelongsTo)){
219         $this->is_account = true;
220       }
222       /* Update dn, to ensure storing as printer instead of WS / terminal
223        */
224       if(preg_match("/terminal/i",$this->BelongsTo)){
225         $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou'))."/",get_ou('printerou'),$this->dn);
226       }
228       if(preg_match("/workstation/i",$this->BelongsTo)){
229         $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
230       }
232       /* Detect if this is a valid printer account;
233        */
234       $ldap = $this->config->get_ldap_link();
235       $ldap->cat($this->dn, array('objectClass'));
237       if($ldap->count()){
238         $attrs = $ldap->fetch();
239         if(in_array("gotoPrinter",$attrs['objectClass'])){
240           $this->initially_was_account = true;
241           $this->is_account             = true;
242         }else{
243           $this->is_account = false;
244         }
245       }
246     }
247   }
249   function execute()
250   {
251     /* Call parent execute */
252     plugin::execute();
254     if($this->is_account && !$this->view_logged){
255       $this->view_logged = TRUE;
256       new log("view","printer/".get_class($this),$this->dn);
257     }
260     /* If type of printer couldn't be detected (because of missing parent object in construction) 
261      * hide this tab.
262      */
263     if(preg_match("/unknown/i",$this->BelongsTo)){
264       $display= $this->show_enable_header(_("Add printer extension"),
265           _("Could not initialize printer tab, parameter parent was missing while construction."),TRUE,TRUE);
266       return($display);
267     }
269     /* Templates can't have printer extensions 
270      */
271     if(preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
272       $display= $this->show_enable_header(_("Add printer extension"),
273           _("This is a workstation template, printer tab is disabled."),TRUE,TRUE);
274       return($display);
275     }
276     if(preg_match("/TerminalTemplate/i",$this->BelongsTo)){
277       $display= $this->show_enable_header(_("Add printer extension"),
278           _("This is a terminal template, printer tab is disabled."),TRUE,TRUE);
279       return($display);
280     }
282     /* Get cn from base object */
283     if(preg_match("/^Workstation$/i",$this->BelongsTo)){
284       $this->cn = $this->parent->by_object['workgeneric']->cn;
285     }
286     if(preg_match("/^Terminal$/i",$this->BelongsTo)){
287       $this->cn = $this->parent->by_object['termgeneric']->cn;
288     }
290     $smarty= get_smarty();
292     /* Assign acls */
293     $tmp = $this->plInfo();
294     foreach($tmp['plProvidedAcls'] as $name => $translation){
295       $smarty->assign($name."ACL", $this->getacl($name));
296     }
298     $display="";
300     /* Tell smarty if this is a standalone object or a terminal / WS depending printer */
301     if(preg_match("/^Printer$/i",$this->BelongsTo)){    
302       $smarty->assign("StandAlone",true);
303     }else{
304       $smarty->assign("StandAlone",false);
305     }
307     /* Do we need to flip is_account state? */
308     if(isset($_POST['modify_state'])){
309       if($this->is_account && $this->acl_is_removeable()){
310         $this->is_account= FALSE;
311       }elseif(!$this->is_account && $this->acl_is_createable()){
312         $this->is_account= TRUE;
313       }
314     }
316     /* Do we represent a valid printer? */
317     if (!$this->is_account && $this->parent === NULL){
318       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
319         _("This 'dn' has no printer features.")."</b>";
320       return($display);
321     }
323     /* If this is a WS / Terminal depending printer, display account state button */
324     if(!preg_match("/^Printer$/i",$this->BelongsTo)){
325       if((empty($this->cn)) && ($this->dn != "new")){
326         $display= $this->show_enable_header(_("Add printer extension"),
327             _("This object has printer extension disabled. You can't enable it while 'cn' is not present in entry. Possibly you are currently creating a new terminal template"),TRUE,TRUE);
328         $this->is_account= false;
329         return $display;
330       }
332       if (($this->is_account)){
333         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
334           $display= $this->show_disable_header(_("Remove printer extension"),
335               _("This workstation has printer extension enabled.You can disable it by clicking below."));
336         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
337           $display= $this->show_disable_header(_("Remove printer extension"),
338               _("This terminal has printer extension enabled. You can disable it by clicking below."));
339         }
340       }else{
341         if(preg_match("/^Workstation$/i",$this->BelongsTo)){
342           $display= $this->show_disable_header(_("Add printer extension"),
343               _("This workstation has printer extension disabled. You can enable it by clicking below."));
344         }elseif(preg_match("/^Terminal$/i",$this->BelongsTo)){
345           $display= $this->show_enable_header(_("Add printer extension"),
346               _("This terminal has printer extension disabled. You can enable it by clicking below."));
347         }  
348         return ($display);
349       }
350     }
352     /* Base select dialog */
353     $once = true;
354     foreach($_POST as $name => $value){
355       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
356         $once = false;
357         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
358         $this->dialog->setCurrentBase($this->base);
359         $this->baseSelection = true;
360       }
361     }
363     /* Dialog handling for base select dialog
364      * Check if base was selected, dialog aborted etc */
365     if(is_object($this->dialog)){
367       $this->dialog->save_object();
368       if($this->baseSelection){
369         if($this->dialog->isClosed()){
370           $this->dialog = false;
371           $this->baseSelection = false;
372         }elseif($this->dialog->isSelected()){
374           /* A new base was selected, check if it is a valid one */
375           $tmp = $this->get_allowed_bases();
376           if(isset($tmp[$this->dialog->isSelected()])){
377             $this->base = $this->dialog->isSelected();
378           }
380           $this->dialog= false;
381           $this->baseSelection = false;
382         }else{
383           return($this->dialog->execute());
384         }
385       }
386     }
388     /* Fill templating stuff */
389     $smarty->assign("bases", $this->get_allowed_bases());
390     $smarty->assign("base_select", $this->base);
392     /* Assign attributes */
393     foreach ($this->attributes as $attr){
394       $smarty->assign("$attr", $this->$attr);
395     }
397     if(isset($_POST['AddUser'])){
398       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddUser");
399     }
400     if(isset($_POST['AddGroup'])){
401       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddGroup");
402     }
403     if(isset($_POST['AddAdminUser'])){
404       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminUser");
405     }
406     if(isset($_POST['AddAdminGroup'])){
407       $this->dialog = new selectUserToPrinterDialog($this->config, NULL,"AddAdminGroup");
408     }
410     /* Display ppd configure/select dialog      */
411     if(isset($_POST['EditDriver'])){
412       if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
413         $this->dialog = $this->PPDdialogToSave;
414       }else{
415         $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
416         $this->dialog->cn= $this->cn;
417       }
418     }
420     /* remove ppd */
421     if(isset($_POST['RemoveDriver'])){
422       $this->gotoPrinterPPD = array();
423       $this->PPDdialogToSave = NULL;
424     }
426     /* Close ppd dialog */
427     if(isset($_POST['ClosePPD'])){
428       unset($this->dialog);
429       $this->dialog=FALSE;
430     }
432     /* Save selected ppd */
433     if(isset($_POST['SavePPD'])){
434       $this->dialog->save_object();
435       if(count($this->dialog->check())){
436         foreach($this->dialog->check() as $msg){
437           print_red($msg);
438         }
439       }else{
440         $this->gotoPrinterPPD = array();
441         $this->gotoPrinterPPD = $this->dialog->save();
442         $this->PPDdialogToSave = $this->dialog;
443         unset($this->dialog);
444         $this->dialog=FALSE;
445       }
446     }
448      /* Member management, delete user / group / admin ..*/
449     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
450       foreach($_POST['UserMember'] as $mem){
451         $this->DelMember('AddUser',$mem);
452       }
453     }
455     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
456       foreach($_POST['UserMember'] as $mem){
457         $this->DelMember('AddGroup',$mem);
458       }
459     }
461     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
462       foreach($_POST['AdminMember'] as $mem){
463         $this->DelMember('AddAdminUser',$mem);
464       }
465     }
467     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
468       foreach($_POST['AdminMember'] as $mem){
469         $this->DelMember('AddAdminGroup',$mem);
470       }
471     }
473     /* Abort user / group adding dialog */
474     if(isset($_POST['PrinterCancel'])){
475       unset($this->dialog);
476       $this->dialog= FALSE;
477     }
479     /* Save selected users / groups */
480     if(isset($_POST['PrinterSave'])){
481       $this->dialog->save_object();
482       if(count($this->dialog->check())){
483         foreach($this->dialog->check() as $msg){
484           print_red($msg);
485         }
486       }else{
487         $data= $new = $this->dialog->save();
488         unset($data['type']);
489         foreach($data as $mem){
490           $this->AddMember($new['type'], $mem['dn']);
491         }
492         unset($this->dialog);
493         $this->dialog=FALSE;
494       }
495     }
497     /* Display dialog, if there is currently one open*/
498     if(is_object($this->dialog)){
499       $this->dialog->save_object();
500       $display = $this->dialog->execute();
501       return $display;
502     }
504     /* Parse selected ppd file */
505     $config = session::get('config');
506     if((isset($config->data['MAIN']['PPD_PATH']))&&(is_dir($config->data['MAIN']['PPD_PATH']))){
508       $path = $config->data['MAIN']['PPD_PATH'];
509       if(!preg_match("/\/$/",$path)){
510         $path = $path."/";
511       }
513       $ppdManager= new ppdManager($path);
514       if(!empty($this->gotoPrinterPPD)){
515         if((!file_exists($path.$this->gotoPrinterPPD))){
516           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
517         }else{
518           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
519         }
520       }else{
521         $smarty->assign("driverInfo", _("Not defined"));
522       }
523     }else{
524       $smarty->assign("driverInfo",_("Can't get ppd informations."));
525     }
527     /* Create user & admin user list */
528     $list=$this->generateList();
529     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
530     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
532     asort($userlist);
533     asort($adminlist);
535     if(!preg_match("/Printer/i",$this->BelongsTo)){
536       if(preg_match("/Terminal/i",$this->BelongsTo)){
537         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
538       }else{
539         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
540       }
541       $smarty->assign("cnACL"    , $this->getacl("cn",true));
542     }else{
543       $smarty->assign("desc"    ,"");
544     }
545     $smarty->assign("UserMember"    ,$this->UserMember);
546     $smarty->assign("UserMembers"   ,$userlist);
547     $smarty->assign("UserMemberKeys",array_flip($userlist));
549     $smarty->assign("AdminMember"    ,$this->AdminMember);
550     $smarty->assign("AdminMembers"   ,$adminlist);
551     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
552     if(preg_match("/Printer/i",$this->BelongsTo)){
554       /* Show main page */
555       $str = $this->netConfigDNS->execute();
556       if(is_object($this->netConfigDNS->dialog)){
557         return($str);
558       }
559       $smarty->assign("netconfig", $str);
560     } else {
561       $smarty->assign("netconfig", "");
562     }
564     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE, dirname(__FILE__))));
565   }
567   function remove_from_parent()
568   {
569     /* Only remove if there was initially an account */
570     if($this->initially_was_account){
572       /* Update dn, to ensure storing as printer instead of WS / terminal
573        */
574       if(preg_match("/terminal/i",$this->BelongsTo)){
575         $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou')).",/",get_ou('printerou'),$this->dn);
576       }
578       if(preg_match("/workstation/i",$this->BelongsTo)){
579         $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
580       }
582       /* Check if this dn points to a printer, to avoid deleting something else */
583       $ldap= $this->config->get_ldap_link();
584       $ldap->cat($this->dn, array('dn',"objectClass"));
585       if(!$ldap->count()){
586         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
587         return;
588       }
590       /* Check if obejct is a printer */
591       $CheckPrinter = $ldap->fetch();
592       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
593         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
594         return;
595       }
597       /* Remove account & dns extension */ 
598       $this->netConfigDNS->remove_from_parent();
599       $ldap->rmdir($this->dn);
601       new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
602   
603       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system print/generic with dn '%s' failed."),$this->dn));
604       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
606       /* Delete references to object groups */
607       $ldap->cd ($this->config->current['BASE']);
608       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
609       while ($ldap->fetch()){
610         $og= new ogroup($this->config, $ldap->getDN());
611         unset($og->member[$this->dn]);
612         $og->save ();
613       }
615       /* Remove previously selected ppd file.*/
616       if(!empty($this->initial_PPD)){
617         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
618         $tmp->removeModifiedPPD();
619       }
620     }
621   }
624   /* Save data to object */
625   function save_object()
626   {
627     /* Create a base backup and reset the
628        base directly after calling plugin::save_object();
629        Base will be set seperatly a few lines below */
630     $base_tmp = $this->base;
631     plugin::save_object();
632     $this->base = $base_tmp;
634     if(is_object($this->netConfigDNS)){
635       $this->netConfigDNS->save_object();
636     }
637     
638     /* Set new base if allowed */
639     $tmp = $this->get_allowed_bases();
640     if(isset($_POST['base'])){
641       if(isset($tmp[$_POST['base']])){
642         $this->base= $_POST['base'];
643       }
644     }
645   }
647   /* Check supplied data */
648   function check()
649   {
650     /* Call common method to give check the hook */
651     $message= plugin::check();
652     if (preg_match("/printer/i",$this->BelongsTo)){
653       $message= array_merge($message, $this->netConfigDNS->check());
654     }
656     /* Don't display check messages if this is a template object */
657     if(isset($this->parent->by_object['workgeneric'])){
658       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
659         return $message;
660       }
661     }elseif(isset($this->parent->by_object['termgeneric'])){
662       if($this->parent->by_object['termgeneric']->cn == "default"){
663         return $message;
664       }
665     }
667     $dn= "cn=".$this->cn.get_ou('printerou').",".$this->base;
669     /* must: cn */
670     if(($this->BelongsTo == "Printer") && (empty($this->cn))){
671       $message[]= _("The required field 'Printer name' is not set.");
672     }
674     /* must: cn */
675     if(($this->BelongsTo == "Printer") && !tests::is_dns_name($this->cn)){
676       $message[]= _("Invalid character in printer name.");
677     }
679     /* must: labeledURI */
680     if(empty($this->labeledURI)){
681       $message[]= "The required field 'Printer URL' is not set.";
682     }
683     
684     /* Check if there is already an entry with this cn*/
685     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
686       $ldap= $this->config->get_ldap_link();
687       $ldap->cd ($this->base);
688       $ldap->ls("(cn=".$this->cn.")",get_ou('printerou').$this->base, array("cn"));
689       if ($ldap->count() != 0){
690         while ($attrs= $ldap->fetch()){
691           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
692             continue;
693           }
694           if ($attrs['dn'] != $this->orig_dn){
695             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
696             break;
697           }
698         }
699       }
700     }
702     return ($message);
703   }
706   /* Save to LDAP */
707   function save()
708   {
709     /* Update dn, to ensure storing as printer instead of WS / terminal
710      */
711     if(preg_match("/terminal/i",$this->BelongsTo)){
712       $this->dn= preg_replace("/".normalizePreg(get_ou('terminalou')).",/",get_ou('printerou'),$this->dn);
713     }
715     if(preg_match("/workstation/i",$this->BelongsTo)){
716       $this->dn= preg_replace("/".normalizePreg(get_ou('workstationou'))."/",get_ou('printerou'),$this->dn);
717     }
718     
719     if(!$this->is_account) return;
720     if(isset($this->parent->by_object['workgeneric'])){
721       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
722         return;
723       }
724     }elseif(isset($this->parent->by_object['termgeneric'])){
725       if($this->parent->by_object['termgeneric']->cn == "default"){
726         return;
727       }
728     }
730     /* If type is still unknown, the initialisation of this printer failed, abort. */
731     if(preg_match("/unknown/i",$this->BelongsTo)){
732       return;
733     }
735     /* save ppd configuration */
736     if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
737       $this->PPDdialogToSave->save_ppd();
738     }
739     if($this->orig_dn != $this->dn){
740       if(!empty($this->gotoPrinterPPD)) {
741         $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
742         $this->PPDdialogToSave->cn = $this->cn;
743         $this->PPDdialogToSave->generateProperties();
744         $this->gotoPrinterPPD = $this->PPDdialogToSave->update_ppd_url();
745       }
746     }
748     /* Remove previously selected ppd file.*/
749     if($this->initial_PPD != $this->gotoPrinterPPD && $this->initially_was_account){
750       if(!empty($this->initial_PPD)){
751         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
752         $tmp->removeModifiedPPD();
753       }
754     }
756     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
757       $method="https://";
758     }else{
759       $method="http://";
760     }
762     /* If no ppd is selected, remove this attribute */
763     if(!empty($this->gotoPrinterPPD) && $this->initially_was_account) {
764       $this->gotoPrinterPPD = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
765     }else{
766       $this->gotoPrinterPPD = array();
767     }
769     $dn= $this->dn;
770     plugin::save();
771     $ldap= $this->config->get_ldap_link();
773     /* reduce objectClasses to minimun */
774     $this->attrs['objectClass']= $this->objectclasses;
776     /* Remove all empty values */
777     if ($this->orig_dn == 'new'){
778       $attrs= array();
779       foreach ($this->attrs as $key => $val){
780         if (is_array($val) && count($val) == 0){
781           continue;
782         }
783         $attrs[$key]= $val;
784       }
785       $this->attrs= $attrs;
786     }
788     /* Append printer user 
789      */
790     $this->attrs['gotoUserPrinter']=array();
791     foreach($this->member['AddUser'] as $mem){
792       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
793     }
795     /* Append printer group 
796      */
797     $this->attrs['gotoGroupPrinter'] = array();
798     foreach($this->member['AddGroup'] as $mem){
799       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
800     }
802     /* Append printer admin user 
803      */
804     $this->attrs['gotoUserAdminPrinter'] = array();
805     foreach($this->member['AddAdminUser'] as $mem){
806       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
807     }
809     /* Append printer admin group 
810      */
811     $this->attrs['gotoGroupAdminPrinter']= array();
812     foreach($this->member['AddAdminGroup'] as $mem){
813       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
814     }
816     if($this->orig_dn == 'new'){
817       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
818         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
819           unset($this->attrs[$checkVar]);
820         }
821       }
822     }else{
823       if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
824         $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
825       }
826     }
828     /* Ensure to create a new object */
829     if(preg_match("/".normalizePreg(get_ou('incomingou'))."/",$this->orig_dn)){
830       $this->orig_dn = "new";
831     }
833     /* Move object in necessary*/
834     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
835       $this->move($this->orig_dn, $this->dn);
836     }
838     /* Write back to ldap */
839     $ldap= $this->config->get_ldap_link();
840     $ldap->cat($this->dn);
841     if(!$ldap->count()){
842       $ldap->cd($this->config->current['BASE']);
843       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
844       $ldap->cd($this->dn);
846       /* Remove empty values */ 
847       foreach($this->attrs as $name => $value){
848         if(empty($value)){
849           unset($this->attrs[$name]);
850         }
851       }
853       $ldap->add($this->attrs);
854       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
855       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
856     } else {
857       $ldap->cd($this->dn);
858       $this->cleanup();
859       $ldap->modify ($this->attrs); 
860       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
861       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
862     }
863     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system print/generic with dn '%s' failed."),$this->dn));
865     if(preg_match("/printer/i",$this->BelongsTo)){
866       $this->netConfigDNS->cn = $this->cn;
867       $this->netConfigDNS->dn = $this->dn;
868       $this->netConfigDNS->save();
869     }
870   }
872   function generateList(){
873     $a_return=array();
875     foreach($this->member as $type => $values){
876       $a_return[$type]=array();
877       foreach($values as $value){
878         if((preg_match("/Group/i",$type))){
879           if(!isset($value['description'])){
880             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
881           }else{
882             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
883           }
884         }else{
885           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
886         }
887       }
888     }
889     return($a_return);
890   }
892   /* Return plugin informations for acl handling
893       #FIXME FAIscript seams to ununsed within this class... */
894   static function plInfo()
895   {
896     return (array(
897           "plShortName"   => _("Generic"),
898           "plDescription" => _("Print generic"),
899           "plSelfModify"  => FALSE,
900           "plDepends"     => array(),
901           "plPriority"    => 4,
902           "plSection"     => array("administration"),
903           "plCategory"    => array("printer" => array("description"  => _("Printer"),
904                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
905           "plProvidedAcls"=> array(
906             "cn"                => _("Name"),
907             "base"                => _("Base") ,         
908             "description"       => _("Description"), 
909             "l"                 => _("Location"), 
910             "labeledURI"        => _("LabeledURL"), 
911             "gotoPrinterPPD"    => _("Printer PPD"),
912             "gotoUserPrinter"   => _("Permissions")) 
913           ));
914   }
917   /* Delete member */
918   function DelMember($type,$id)
919   {
920     /* Check if there was a printer "dn" given, or the "cn" */
921     foreach($this->member[$type] as $key => $printer){
922       if($printer['dn'] == $id) {
923         $id = $key;
924       }
925     }
926   
927     if(!$this->acl_is_writeable("gotoUserPrinter")){
928       print_red(sprintf(_("You are not allowed to remove the given object '%s' from the list of members of printer '%s'."),$id,$this->dn));
929       return(FALSE);
930     }
931  
932     if(isset($this->member[$type][$id])){
933       unset($this->member[$type][$id]);
934       return(TRUE);
935     }
936     return(FALSE);
937   }
940   /* Add given obejct to members */
941   function AddMember($type,$dn)
942   {
943     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
944     if(!in_array_ics($type, $types)){
945       print_red(sprintf(_("Illegal printer type while adding '%s' to the list of '%s' printers,"),$dn,$type));
946       return(FALSE);
947     }
949     if(!$this->acl_is_writeable("gotoUserPrinter")){
950       print_red(sprintf(_("You are not allowed to add the given object '%s' to the list of members of '%s'."),$dn,$this->dn));
951       return(FALSE);
952     }
954     /* Get name of index attributes */
955     if(preg_match("/user/i",$type)){
956       $var = "uid";
957     }else{
958       $var = "cn";
959     }
961     $ldap = $this->config->get_ldap_link();
962     $ldap->cd($dn);
963     $ldap->cat($dn,array($var,"cn"));
964     if($ldap->count()){
966       $attrs = $ldap->fetch();
968       if(isset($attrs[$var][0])){
969         $name = $attrs[$var][0];
971         /* Check if this uid/cn is already assigned to any permission */
972         foreach($types as $ctype){
974           /* If we want to add a user, only check user/userAdmin members */
975           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
976             continue;
977           }
979           /* If we want to add a group, only check groups/adminGroups .. */
980           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
981             continue;
982           }
984           if(isset(  $this->member[$ctype][$name])){
985             print_red(sprintf(_("Can't add '%s' to the list of members, it is already used."),$attrs[$var][0]));
986             return(FALSE);
987           }
988         }
990         /* Everything is fine. So add the given object to members */
991         $this->member[$type][$attrs[$var][0]] = $attrs ;
992       }else{
993         print_a($attrs);
994       }
995     }else{
996       print_red(sprintf(_("Can't add '%s' to list of members, it is not reachable."),$dn));
997       return(FALSE);
998     }
999     return(TRUE);
1000   }
1003    /* Display generic part for server copy & paste */
1004   function getCopyDialog()
1005   {
1006     $vars = array("cn");
1007     $smarty = get_smarty();
1008     $smarty->assign("cn" ,$this->cn);
1009     $smarty->assign("object","printer");
1010     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1011     $ret = array();
1012     $ret['string'] = $str;
1013     $ret['status'] = "";
1014     return($ret);
1015   }
1018   function saveCopyDialog()
1019   {
1020     if(isset($_POST['cn'])){
1021       $this->cn = $_POST['cn'];
1022     }
1023   }
1025   function PrepareForCopyPaste($source)
1026   {
1027     plugin::PrepareForCopyPaste($source);
1028     if(isset($source['macAddress'][0])){
1029       $this->netConfigDNS->macAddress = $source['macAddress'][0];
1030     }
1031     if(isset($source['ipHostNumber'][0])){
1032       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1033     }
1035     $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1036     foreach($this->attributes as $attr){
1037       $this->$attr = $source_o->$attr;
1038     }
1039     $this->member = $source_o -> member;
1041     $this->gotoPrinterPPD = "";
1042   }
1045 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1046 ?>