Code

Fixed base selection.
[gosa.git] / plugins / admin / systems / class_phoneGeneric.inc
1 <?php
3 class phoneGeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage phone 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= TRUE;
14   /* Needed values and lists */
15   var $base             = "";
16   var $cn               = "";
17   var $description      = "";
18   var $orig_dn          = "";
19   var $goFonType        = "";
20   var $goFonDmtfMode    = "";
21   var $goFonHost        = "";
22   var $goFonDefaultIP   = "dynamic";
23   var $goFonQualify     = "";
24   var $goFonAuth        = "";
25   var $goFonSecret      = "";
26   var $goFonInkeys      = "";
27   var $goFonPermit      = array();
28   var $goFonDeny        = array();
29   var $goFonOutkey      = "";
30   var $goFonTrunk       = "";
31   var $goFonAccountCode = "";
32   var $goFonMSN         = "";
33   var $selected_categorie    = 0;
34   var $netConfigDNS;
36   /* attribute list for save action */
37   var $attributes= array("cn", "description", 
38                           "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
39                           "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
40                           "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie","goFonPermit","goFonDeny" 
41                          );
43   /* this array defines which attributes are schown / saved for the different type of phones */ 
44   var $usedattrs =    array( "0"=>array("cn", "description", 
45                                   "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
46                                   "goFonQualify"),
47                              "1"=>array("cn", "description", 
48                                   "goFonType","goFonHost","goFonDefaultIP",
49                                   "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
50                                   "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
51                              "2"=>array("cn", "description", "goFonMSN"));
52   
54   var $objectclasses= array("top", "goFonHardware");
56   function phonegeneric ($config, $dn= NULL, $parent= NULL)
57   {
58     plugin::plugin ($config, $dn, $parent);
59     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
61     /* Set base */
62     if ($this->dn == "new"){
63       $ui= get_userinfo();
64       $this->base= dn2base($ui->dn);
65       $this->cn= "";
66     } else {
67       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
68     }
69   
70     if($this->goFonMSN != ""){
71       $this->selected_categorie = 2;
72     }elseif($this->goFonAccountCode != ""){
73       $this->selected_categorie = 1 ;
74     
75       if(isset($this->attrs['goFonPermit']['count'])){
76         unset ($this->attrs['goFonPermit']['count']);
77         $this->goFonPermit=$this->attrs['goFonPermit'];
78       }  
79      
80       if(isset($this->attrs['goFonDeny']['count'])){
81         unset ($this->attrs['goFonDeny']['count'])   ;
82         $this->goFonDeny=$this->attrs['goFonDeny'];
83       } 
85     } else {
86       $this->selected_categorie = 0;
87     }
89     if($this->goFonDefaultIP!="dynamic"){
90       $this->goFonDefaultIP = "network";
91     }
93     /* Save dn for later references */
94     $this->orig_dn= $this->dn;
95   }
97   function set_acl_base($base)
98   {
99     plugin::set_acl_base($base);
100     $this->netConfigDNS->set_acl_base($base);
101   }
103   function set_acl_category($cat)
104   {
105     plugin::set_acl_category($cat);
106     $this->netConfigDNS->set_acl_category($cat);
107   }
110   function execute()
111   {
112         /* Call parent execute */
113         plugin::execute();
115     /* Do we represent a valid phone? */
116     if (!$this->is_account && $this->parent == NULL){
117       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
118         _("This 'dn' has no phone features.")."</b>";
119       return($display);
120     }
122     /* Base select dialog */
123     $once = true;
124     foreach($_POST as $name => $value){
125       if(preg_match("/^chooseBase/",$name) && $once){
126         $once = false;
127         $this->dialog = new baseSelectDialog($this->config,$this);
128         $this->dialog->setCurrentBase($this->base);
129       }
130     }
132     /* Dialog handling */
133     if(is_object($this->dialog)){
134       /* Must be called before save_object */
135       $this->dialog->save_object();
137       if($this->dialog->isClosed()){
138         $this->dialog = false;
139       }elseif($this->dialog->isSelected()){
140         
141         /* A new base was selected, check if it is a valid one */
142         $tmp = $this->get_allowed_bases();
143         if(isset($tmp[$this->dialog->isSelected()])){
144           $this->base = $this->dialog->isSelected();
145         }
147         $this->dialog= false;
148       }else{
149         return($this->dialog->execute());
150       }
151     }
153     /* handle Permit Add*/
154     if(isset($_POST['goFonPermitAdd']) && $this->acl_is_writeable("goFonPermit")){
155       if(isset($_POST['goFonPermitNew'])){
156         if(is_string($this->goFonPermit)){
157           $this->goFonPermit=array();
158         }
159         $new = $_POST['goFonPermitNew'];
160         if(strlen($new)> 1) {
161           $this->goFonPermit[]= $new;
162         }
163       }
164     }
165       
166     /* handle Deny Add*/
167     if(isset($_POST['goFonDenyAdd']) && $this->acl_is_writeable("goFonDeny")){
168       if(isset($_POST['goFonDenyNew'])){
169         if(is_string($this->goFonDeny)){
170           $this->goFonDeny=array();
171         }
172         $new = $_POST['goFonDenyNew'];
173         if(strlen($new)> 1) {
174           $this->goFonDeny[]= $new;
175         }
176       }
177     }
179     /* Handle Permit Deletion*/
180     if(isset($_POST['goFonPermitDel']) && $this->acl_is_writeable("goFonPermit")){
181       if(isset($_POST['goFonPermitS'])){
182         if(is_string($this->goFonPermit)){
183           $this->goFonPermit=array();
184         }
185         $new = $_POST['goFonPermitS'];
186         $tmp = array_flip($this->goFonPermit);
187         unset($tmp[$new]);
188         $this->goFonPermit=array();
189         foreach(array_flip($tmp) as $tm){
190           $this->goFonPermit[]=$tm;
191         }
192       }
193     }
196     /* Handle Permit Deletion*/
197     if(isset($_POST['goFonDenyDel']) && $this->acl_is_writeable("goFonDeny")){
198       if(isset($_POST['goFonDenyS'])){
199         if(is_string($this->goFonDeny)){
200           $this->goFonDeny=array();
201         }
202         $new = $_POST['goFonDenyS'];
203         $tmp = array_flip($this->goFonDeny);
204         unset($tmp[$new]);
205         $this->goFonDeny=array();
206         foreach(array_flip($tmp) as $tm){
207           $this->goFonDeny[]=$tm;
208         }
209       }
210     }
211   
212     /* Fill templating stuff */
213     $smarty= get_smarty();
215     $smarty->assign("bases", $this->get_allowed_bases());
217     $tmp = $this->plInfo();
218     foreach($tmp['plProvidedAcls'] as $name => $translation){
219       $smarty->assign($name."ACL",$this->getacl($name));
220     }
222     $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
224     /* Create Arrays for samrty select boxes */
225     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
226     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
227     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
228     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
229     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
231     /* deativate all fields that are not used by the specified type */
232     foreach($this->attributes as $att){
233       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))){
234         $smarty->assign($att."USED", "disabled" );
235         $smarty->assign($att, "");
236       }else{
237         $smarty->assign($att."USED", "" );
238         $smarty->assign($att, $this->$att);
239       }
240     }
241     
242     $smarty->assign("selected_categorie",$this->selected_categorie);
244     /* Assign attributes */
245     $smarty->assign("base_select", $this->base);
246     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
247  
248     /* Show main page */
249     $smarty->assign("netconfig", $this->netConfigDNS->execute());
250     $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
251     return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
252   }
254   function remove_from_parent()
255   {
256     if($this->acl_is_removeable()){
257       $ldap= $this->config->get_ldap_link();
258       $ldap->cd($this->config->current['BASE']);
260       $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
261       while ($attr =  $ldap->fetch()){
262         print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
263               ($attr['uid'][0]." - ".$attr['cn'][0])));
264         return;
265       }
267       $this->netConfigDNS->remove_from_parent();
268       $ldap->rmdir($this->dn);
269       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system phone/generic with dn '%s' failed."),$this->dn));
270       $this->handle_post_events("remove");
272       /* Delete references to object groups */
273       $ldap->cd ($this->config->current['BASE']);
274       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
275       while ($ldap->fetch()){
276         $og= new ogroup($this->config, $ldap->getDN());
277         unset($og->member[$this->dn]);
278         $og->save ();
279       }
280     }
281   }
284   /* Save data to object */
285   function save_object()
286   {
287     /* Create a base backup and reset the
288        base directly after calling plugin::save_object();
289        Base will be set seperatly a few lines below */
290     $base_tmp = $this->base;
291     plugin::save_object();
292     $this->base = $base_tmp;
294     $this->netConfigDNS->save_object();
296     /* Set new base if allowed */
297     $tmp = $this->get_allowed_bases();
298     if(isset($_POST['base'])){
299       if(isset($tmp[$_POST['base']])){
300         $this->base= $_POST['base'];
301       }
302     }
303   }
306   /* Check supplied data */
307   function check()
308   {
309     /* Call common method to give check the hook */
310     $message= plugin::check();
311     $message= array_merge($message, $this->netConfigDNS->check());
313     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
315     /* To check for valid ip*/
316     if($this->netConfigDNS->ipHostNumber == ""){
317        $message[]= _("The required field IP address is empty.");
318     } else {
319       if (!is_ip($this->netConfigDNS->ipHostNumber)){
320         $message[]= _("The field IP address contains an invalid address.");
321       }
322     }
324     if ($this->cn == ""){
325       $message[]= _("The required field 'Phone name' is not set.");
326     }
327     if ($this->cn == "0"){ 
328       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
329     }
331     /* Set new acl base */
332     if($this->dn == "new") {
333       $this->set_acl_base($this->base);
334     }
336     $ui= get_userinfo();
337     if ($this->dn == "new" && $this->acl_is_createable()){
338       $message[]= _("You have no permissions to create a phone on this 'Base'.");
339     }
341     if ($this->orig_dn != $this->dn){
342       $ldap= $this->config->get_ldap_link();
343       $ldap->cd ($this->base);
344       $ldap->search ("(cn=".$this->cn.")", array("cn"));
345       if ($ldap->count() != 0){
346         while ($attrs= $ldap->fetch()){
347           if ($attrs['dn'] != $this->orig_dn){
348             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
349             break;
350           }
351         }
352       }
353     }
355     return ($message);
356   }
359   /* Save to LDAP */
360   function save()
361   {
362     plugin::save();
363    
364    
365     /* only to define which attrs to save*/ 
366     $mode = $this->attrs['selected_categorie'];
368     /* Remove all unwanted attrs */
369     foreach($this->attributes as $att){
370       
371       /* Check all attributes, if they are needed for this type of phone */
372       if(!in_array($att,$this->usedattrs[$mode])){
373         $this->attrs[$att] = array();
374       }
375     }
377     /* unset the categorie*/
378     unset($this->attrs['selected_categorie']);
379     
380     /* Remove all empty values */
381     if ($this->orig_dn == 'new'){
382       $attrs= array();
383       foreach ($this->attrs as $key => $val){
384         if (is_array($val) && count($val) == 0){
385           continue;
386         }
387         $attrs[$key]= $val;
388       }
389       $this->attrs= $attrs;
390     }
392     if($this->goFonDefaultIP!="dynamic"){
393       $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
394     }    
396     $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
398     /* Write back to ldap */
399     $ldap= $this->config->get_ldap_link();
400     if ($this->orig_dn == 'new'){
401       $ldap->cd($this->config->current['BASE']);
402       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
403       $ldap->cd($this->dn);
404       $ldap->add($this->attrs);
405       $this->handle_post_events("add");
406     } else {
407       if ($this->orig_dn != $this->dn){
408         $this->move($this->orig_dn, $this->dn);
409       }
411       $ldap->cd($this->dn);
412       $this->cleanup();
413       $ldap->modify ($this->attrs); 
415       // $user_phone_reload   
416       $ldap->cd ($this->config->current['BASE']); 
417       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
418       if($user_phone_assignment){
419         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
420         $usertab->by_object['phoneAccount']->is_modified = true;
421         $usertab->save();
422         unset($usertab);
423       }
424       $this->handle_post_events("modify");
425     }
426     $this->netConfigDNS->cn = $this->cn;    
427     $this->netConfigDNS->save($this->dn);
428     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system phone/generic with dn '%s' failed."),$this->dn));
430     /* Optionally execute a command after we're done */
431     $this->postcreate();
432   }
435   /* Return plugin informations for acl handling
436       #FIXME FAIscript seams to ununsed within this class... */
437   function plInfo()
438   {
439     return (array(
440           "plShortName"   => _("Generic"),
441           "plDescription" => _("Phone generic"),
442           "plSelfModify"  => FALSE,
443           "plDepends"     => array(),
444           "plPriority"    => 0,
445           "plSection"     => array("administration"),
446           "plCategory"    => array("phone" => array("description"  => _("Phone hardware"),
447                                                     "objectClass"  => "goFonHardware")),
448           "plProvidedAcls"=> array(
449             "cn"                  => _("Name"),
450             "description"         => _("Description"),
451             "goFonType"           => _("SIP Mode"),
452             "goFonDmtfMode"       => _("SIP DTMF mode"),
453             "goFonDefaultIP"      => _("SIP Default ip"),
454             "goFonQualify"        => _("SIP Qualify"),
455             "goFonAuth"           => _("IAX authentication type"),
456             "goFonSecret"         => _("IAX secret"),
457             "goFonAccountCode"    => _("IAX account code"),
458             "goFonTrunk"          => _("IAX trunk lines"),
459             "goFonPermit"         => _("IAX permit settings"),
460             "goFonDeny"           => _("IAX deny settings"),
461             "goFonMSN"            => _("CAPI MSN"),
462             "categorie"  => _("Hardware type"))
463           ));
464   }
469 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
470 ?>