Code

Updated trunk, introduced gosa-core
[gosa.git] / plugins / admin / systems / 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("/ou=terminals,/","ou=printers,",$this->dn);
70     }
72     if(preg_match("/Workstation/i",$this->BelongsTo) || preg_match("/WorkstationTemplate/i",$this->BelongsTo)){
73       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$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("/ou=incoming,/",$this->dn)){
103         $this->base= preg_replace("/ou=incoming,/","",dn2base($this->dn));
104       }else{
105         $this->base= preg_replace("/ou=printers,ou=systems,/","",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['CurrentMainBase']);
110         $this->base = $_SESSION['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("/ou=terminals,/","ou=printers,",$this->dn);
226       }
228       if(preg_match("/workstation/i",$this->BelongsTo)){
229         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$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     if((isset($_SESSION['config']->data['MAIN']['PPD_PATH']))&&(is_dir($_SESSION['config']->data['MAIN']['PPD_PATH']))){
507       $path = $_SESSION['config']->data['MAIN']['PPD_PATH'];
508       if(!preg_match("/\/$/",$path)){
509         $path = $path."/";
510       }
512       $ppdManager= new ppdManager($path);
513       if(!empty($this->gotoPrinterPPD)){
514         if((!file_exists($path.$this->gotoPrinterPPD))){
515           $smarty->assign("driverInfo", "<b>".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD)."</b>");
516         }else{
517           $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD));
518         }
519       }else{
520         $smarty->assign("driverInfo", _("Not defined"));
521       }
522     }else{
523       $smarty->assign("driverInfo",_("Can't get ppd informations."));
524     }
526     /* Create user & admin user list */
527     $list=$this->generateList();
528     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
529     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
531     asort($userlist);
532     asort($adminlist);
534     if(!preg_match("/Printer/i",$this->BelongsTo)){
535       if(preg_match("/Terminal/i",$this->BelongsTo)){
536         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
537       }else{
538         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
539       }
540       $smarty->assign("cnACL"    , $this->getacl("cn",true));
541     }else{
542       $smarty->assign("desc"    ,"");
543     }
544     $smarty->assign("UserMember"    ,$this->UserMember);
545     $smarty->assign("UserMembers"   ,$userlist);
546     $smarty->assign("UserMemberKeys",array_flip($userlist));
548     $smarty->assign("AdminMember"    ,$this->AdminMember);
549     $smarty->assign("AdminMembers"   ,$adminlist);
550     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
551     if(preg_match("/Printer/i",$this->BelongsTo)){
553       /* Show main page */
554       $str = $this->netConfigDNS->execute();
555       if(is_object($this->netConfigDNS->dialog)){
556         return($str);
557       }
558       $smarty->assign("netconfig", $str);
559     } else {
560       $smarty->assign("netconfig", "");
561     }
563     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
564   }
566   function remove_from_parent()
567   {
568     /* Only remove if there was initially an account */
569     if($this->initially_was_account){
571       /* Update dn, to ensure storing as printer instead of WS / terminal
572        */
573       if(preg_match("/terminal/i",$this->BelongsTo)){
574         $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
575       }
577       if(preg_match("/workstation/i",$this->BelongsTo)){
578         $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
579       }
581       /* Check if this dn points to a printer, to avoid deleting something else */
582       $ldap= $this->config->get_ldap_link();
583       $ldap->cat($this->dn, array('dn',"objectClass"));
584       if(!$ldap->count()){
585         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
586         return;
587       }
589       /* Check if obejct is a printer */
590       $CheckPrinter = $ldap->fetch();
591       if(!in_array("gotoPrinter",$CheckPrinter['objectClass'])){
592         print_red("Trying to remove printer object which isn't a printer. Aborted to avoid data loss.");
593         return;
594       }
596       /* Remove account & dns extension */ 
597       $this->netConfigDNS->remove_from_parent();
598       $ldap->rmdir($this->dn);
600       new log("remove","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
601   
602       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system print/generic with dn '%s' failed."),$this->dn));
603       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
605       /* Delete references to object groups */
606       $ldap->cd ($this->config->current['BASE']);
607       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
608       while ($ldap->fetch()){
609         $og= new ogroup($this->config, $ldap->getDN());
610         unset($og->member[$this->dn]);
611         $og->save ();
612       }
614       /* Remove previously selected ppd file.*/
615       if(!empty($this->initial_PPD)){
616         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
617         $tmp->removeModifiedPPD();
618       }
619     }
620   }
623   /* Save data to object */
624   function save_object()
625   {
626     /* Create a base backup and reset the
627        base directly after calling plugin::save_object();
628        Base will be set seperatly a few lines below */
629     $base_tmp = $this->base;
630     plugin::save_object();
631     $this->base = $base_tmp;
633     if(is_object($this->netConfigDNS)){
634       $this->netConfigDNS->save_object();
635     }
636     
637     /* Set new base if allowed */
638     $tmp = $this->get_allowed_bases();
639     if(isset($_POST['base'])){
640       if(isset($tmp[$_POST['base']])){
641         $this->base= $_POST['base'];
642       }
643     }
644   }
646   /* Check supplied data */
647   function check()
648   {
649     /* Call common method to give check the hook */
650     $message= plugin::check();
651     if (preg_match("/printer/i",$this->BelongsTo)){
652       $message= array_merge($message, $this->netConfigDNS->check());
653     }
655     /* Don't display check messages if this is a template object */
656     if(isset($this->parent->by_object['workgeneric'])){
657       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
658         return $message;
659       }
660     }elseif(isset($this->parent->by_object['termgeneric'])){
661       if($this->parent->by_object['termgeneric']->cn == "default"){
662         return $message;
663       }
664     }
666     $dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
668     /* must: cn */
669     if(($this->BelongsTo == "Printer") && (empty($this->cn))){
670       $message[]= _("The required field 'Printer name' is not set.");
671     }
673     /* must: cn */
674     if(($this->BelongsTo == "Printer") && !is_dns_name($this->cn)){
675       $message[]= _("Invalid character in printer name.");
676     }
678     /* must: labeledURI */
679     if(empty($this->labeledURI)){
680       $message[]= "The required field 'Printer URL' is not set.";
681     }
682     
683     /* Check if there is already an entry with this cn*/
684     if (($this->orig_dn != $dn)&&( preg_match("/printer/i",$this->BelongsTo))){
685       $ldap= $this->config->get_ldap_link();
686       $ldap->cd ($this->base);
687       $ldap->ls("(cn=".$this->cn.")","ou=printers,ou=systems,".$this->base, array("cn"));
688       if ($ldap->count() != 0){
689         while ($attrs= $ldap->fetch()){
690           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
691             continue;
692           }
693           if ($attrs['dn'] != $this->orig_dn){
694             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
695             break;
696           }
697         }
698       }
699     }
701     return ($message);
702   }
705   /* Save to LDAP */
706   function save()
707   {
708     /* Update dn, to ensure storing as printer instead of WS / terminal
709      */
710     if(preg_match("/terminal/i",$this->BelongsTo)){
711       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
712     }
714     if(preg_match("/workstation/i",$this->BelongsTo)){
715       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
716     }
717     
718     if(!$this->is_account) return;
719     if(isset($this->parent->by_object['workgeneric'])){
720       if($this->parent->by_object['workgeneric']->cn == "wdefault"){
721         return;
722       }
723     }elseif(isset($this->parent->by_object['termgeneric'])){
724       if($this->parent->by_object['termgeneric']->cn == "default"){
725         return;
726       }
727     }
729     /* If type is still unknown, the initialisation of this printer failed, abort. */
730     if(preg_match("/unknown/i",$this->BelongsTo)){
731       return;
732     }
734     /* save ppd configuration */
735     if($this->PPDdialogToSave && is_object($this->PPDdialogToSave)){
736       $this->PPDdialogToSave->save_ppd();
737     }
738     if($this->orig_dn != $this->dn){
739       if(!empty($this->gotoPrinterPPD)) {
740         $this->PPDdialogToSave = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
741         $this->PPDdialogToSave->cn = $this->cn;
742         $this->PPDdialogToSave->generateProperties();
743         $this->PPDdialogToSave->update_ppd_url();
744       }
745     }
747     /* Remove previously selected ppd file.*/
748     if($this->initial_PPD != $this->gotoPrinterPPD){
749       if(!empty($this->initial_PPD)){
750         $tmp = new printerPPDDialog($this->config, $this->dn,$this->initial_PPD);
751         $tmp->removeModifiedPPD();
752       }
753     }
755     if(preg_match("/https/i",$_SERVER['HTTP_REFERER'])){
756       $method="https://";
757     }else{
758       $method="http://";
759     }
761     /* If no ppd is selected, remove this attribute */
762     if(!empty($this->gotoPrinterPPD)) {
763       $this->gotoPrinterPPD = $method.str_replace("//","/",$_SERVER['SERVER_NAME']."/ppd/".$this->gotoPrinterPPD);
764     }else{
765       $this->gotoPrinterPPD = array();
766     }
768     $dn= $this->dn;
769     plugin::save();
770     $ldap= $this->config->get_ldap_link();
772     /* reduce objectClasses to minimun */
773     $this->attrs['objectClass']= $this->objectclasses;
775     /* Remove all empty values */
776     if ($this->orig_dn == 'new'){
777       $attrs= array();
778       foreach ($this->attrs as $key => $val){
779         if (is_array($val) && count($val) == 0){
780           continue;
781         }
782         $attrs[$key]= $val;
783       }
784       $this->attrs= $attrs;
785     }
787     /* Append printer user 
788      */
789     $this->attrs['gotoUserPrinter']=array();
790     foreach($this->member['AddUser'] as $mem){
791       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
792     }
794     /* Append printer group 
795      */
796     $this->attrs['gotoGroupPrinter'] = array();
797     foreach($this->member['AddGroup'] as $mem){
798       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
799     }
801     /* Append printer admin user 
802      */
803     $this->attrs['gotoUserAdminPrinter'] = array();
804     foreach($this->member['AddAdminUser'] as $mem){
805       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
806     }
808     /* Append printer admin group 
809      */
810     $this->attrs['gotoGroupAdminPrinter']= array();
811     foreach($this->member['AddAdminGroup'] as $mem){
812       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
813     }
815     if($this->orig_dn == 'new'){
816       foreach(array("gotoGroupPrinter","gotoUserAdminPrinter","gotoGroupAdminPrinter","gotoUserPrinter") as $checkVar){
817         if(count($this->attrs[$checkVar])  == 0 || empty($this->attrs[$checkVar])){
818           unset($this->attrs[$checkVar]);
819         }
820       }
821     }else{
822       if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
823         $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
824       }
825     }
827     /* Ensure to create a new object */
828     if(preg_match("/ou=incoming,/",$this->orig_dn)){
829       $this->orig_dn = "new";
830     }
832     /* Move object in necessary*/
833     if (($this->orig_dn != $this->dn) && ($this->orig_dn != 'new')){
834       $this->move($this->orig_dn, $this->dn);
835     }
837     /* Write back to ldap */
838     $ldap= $this->config->get_ldap_link();
839     $ldap->cat($this->dn);
840     if(!$ldap->count()){
841       $ldap->cd($this->config->current['BASE']);
842       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
843       $ldap->cd($this->dn);
845       /* Remove empty values */ 
846       foreach($this->attrs as $name => $value){
847         if(empty($value)){
848           unset($this->attrs[$name]);
849         }
850       }
852       $ldap->add($this->attrs);
853       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
854       new log("create","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
855     } else {
856       $ldap->cd($this->dn);
857       $this->cleanup();
858       $ldap->modify ($this->attrs); 
859       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
860       new log("modify","printer/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
861     }
862     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system print/generic with dn '%s' failed."),$this->dn));
864     if(preg_match("/printer/i",$this->BelongsTo)){
865       $this->netConfigDNS->cn = $this->cn;
866       $this->netConfigDNS->dn = $this->dn;
867       $this->netConfigDNS->save();
868     }
870     /* This is a multi object. Handle tagging here... */
871     $this->handle_object_tagging();
872   }
874   function generateList(){
875     $a_return=array();
877     foreach($this->member as $type => $values){
878       $a_return[$type]=array();
879       foreach($values as $value){
880         if((preg_match("/Group/i",$type))){
881           if(!isset($value['description'])){
882             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
883           }else{
884             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
885           }
886         }else{
887           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
888         }
889       }
890     }
891     return($a_return);
892   }
894   /* Return plugin informations for acl handling
895       #FIXME FAIscript seams to ununsed within this class... */
896   static function plInfo()
897   {
898     return (array(
899           "plShortName"   => _("Generic"),
900           "plDescription" => _("Print generic"),
901           "plSelfModify"  => FALSE,
902           "plDepends"     => array(),
903           "plPriority"    => 4,
904           "plSection"     => array("administration"),
905           "plCategory"    => array("printer" => array("description"  => _("Printer"),
906                                                     "objectClass"  => "gotoPrinter"),"workstation","terminal"),
907           "plProvidedAcls"=> array(
908             "cn"                => _("Name"),
909             "base"                => _("Base") ,         
910             "description"       => _("Description"), 
911             "l"                 => _("Location"), 
912             "labeledURI"        => _("LabeledURL"), 
913             "gotoPrinterPPD"    => _("Printer PPD"),
914             "gotoUserPrinter"   => _("Permissions")) 
915           ));
916   }
919   /* Delete member */
920   function DelMember($type,$id)
921   {
922     /* Check if there was a printer "dn" given, or the "cn" */
923     foreach($this->member[$type] as $key => $printer){
924       if($printer['dn'] == $id) {
925         $id = $key;
926       }
927     }
928   
929     if(!$this->acl_is_writeable("gotoUserPrinter")){
930       print_red(sprintf(_("You are not allowed to remove the given object '%s' from the list of members of printer '%s'."),$id,$this->dn));
931       return(FALSE);
932     }
933  
934     if(isset($this->member[$type][$id])){
935       unset($this->member[$type][$id]);
936       return(TRUE);
937     }
938     return(FALSE);
939   }
942   /* Add given obejct to members */
943   function AddMember($type,$dn)
944   {
945     $types = array("AddUser","AddGroup","AddAdminUser","AddAdminGroup");
946     if(!in_array_ics($type, $types)){
947       print_red(sprintf(_("Illegal printer type while adding '%s' to the list of '%s' printers,"),$dn,$type));
948       return(FALSE);
949     }
951     if(!$this->acl_is_writeable("gotoUserPrinter")){
952       print_red(sprintf(_("You are not allowed to add the given object '%s' to the list of members of '%s'."),$dn,$this->dn));
953       return(FALSE);
954     }
956     /* Get name of index attributes */
957     if(preg_match("/user/i",$type)){
958       $var = "uid";
959     }else{
960       $var = "cn";
961     }
963     $ldap = $this->config->get_ldap_link();
964     $ldap->cd($dn);
965     $ldap->cat($dn,array($var,"cn"));
966     if($ldap->count()){
968       $attrs = $ldap->fetch();
970       if(isset($attrs[$var][0])){
971         $name = $attrs[$var][0];
973         /* Check if this uid/cn is already assigned to any permission */
974         foreach($types as $ctype){
976           /* If we want to add a user, only check user/userAdmin members */
977           if((preg_match("/user/i",$type)) && (!preg_match("/user/i",$ctype))){
978             continue;
979           }
981           /* If we want to add a group, only check groups/adminGroups .. */
982           if((preg_match("/group/i",$type)) && (!preg_match("/group/i",$ctype))){
983             continue;
984           }
986           if(isset(  $this->member[$ctype][$name])){
987             print_red(sprintf(_("Can't add '%s' to the list of members, it is already used."),$attrs[$var][0]));
988             return(FALSE);
989           }
990         }
992         /* Everything is fine. So add the given object to members */
993         $this->member[$type][$attrs[$var][0]] = $attrs ;
994       }else{
995         print_a($attrs);
996       }
997     }else{
998       print_red(sprintf(_("Can't add '%s' to list of members, it is not reachable."),$dn));
999       return(FALSE);
1000     }
1001     return(TRUE);
1002   }
1005    /* Display generic part for server copy & paste */
1006   function getCopyDialog()
1007   {
1008     $vars = array("cn");
1009     $smarty = get_smarty();
1010     $smarty->assign("cn" ,$this->cn);
1011     $smarty->assign("object","printer");
1012     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1013     $ret = array();
1014     $ret['string'] = $str;
1015     $ret['status'] = "";
1016     return($ret);
1017   }
1020   function saveCopyDialog()
1021   {
1022     if(isset($_POST['cn'])){
1023       $this->cn = $_POST['cn'];
1024     }
1025   }
1027   function PrepareForCopyPaste($source)
1028   {
1029     plugin::PrepareForCopyPaste($source);
1030     if(isset($source['macAddress'][0])){
1031       $this->netConfigDNS->macAddress = $source['macAddress'][0];
1032     }
1033     if(isset($source['ipHostNumber'][0])){
1034       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
1035     }
1037     $source_o = new printgeneric($this->config,$source['dn'],NULL,$this->parent);
1038     foreach($this->attributes as $attr){
1039       $this->$attr = $source_o->$attr;
1040     }
1041     $this->member = $source_o -> member;
1043     $this->gotoPrinterPPD = "";
1044   }
1047 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1048 ?>