Code

dynamic/Network will be saved as thought
[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 $macAddress       = "";
18   var $ipHostNumber     = "";
19   var $description      = "";
20   var $orig_dn          = "";
21   var $goFonType        = "";
22   var $goFonDmtfMode    = "";
23   var $goFonHost        = "";
24   var $goFonDefaultIP   = "";
25   var $goFonQualify     = "";
26   var $goFonAuth        = "";
27   var $goFonSecret      = "";
28   var $goFonInkeys      = "";
29   var $goFonPermit      = array();
30   var $goFonDeny        = array();
31   var $goFonOutkey      = "";
32   var $goFonTrunk       = "";
33   var $goFonAccountCode = "";
34   var $goFonMSN         = "";
35   var $selected_categorie    = 0;
37   /* attribute list for save action */
38   var $attributes= array("cn", "description", "macAddress", "ipHostNumber"
39                          ,"goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
40                           "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
41                           "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie","goFonPermit","goFonDeny" 
42                          );
44   /* this array defines which attributes are schown / saved for the different type of phones */ 
45   var $usedattrs =    array( "0"=>array("cn", "description", "macAddress", "ipHostNumber",
46                                   "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
47                                   "goFonQualify"),
48                              "1"=>array("cn", "description", "macAddress", "ipHostNumber",
49                                   "goFonType","goFonHost","goFonDefaultIP",
50                                   "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
51                                   "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
52                              "2"=>array("cn", "description", "macAddress", "ipHostNumber","goFonMSN"));
53   
55   var $objectclasses= array("top", "goFonHardware");
57   function phonegeneric ($config, $dn= NULL)
58   {
59     plugin::plugin ($config, $dn);
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 execute()
98   {
99  
100   
101     /* Do we represent a valid phone? */
102     if (!$this->is_account && $this->parent == NULL){
103       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
104         _("This 'dn' has no phone features.")."</b>";
105       return($display);
106     }
108     /* handle Permit Add*/
109     if(isset($_POST['goFonPermitAdd'])){
110       if(isset($_POST['goFonPermitNew'])){
111         if(is_string($this->goFonPermit)){
112           $this->goFonPermit=array();
113         }
114         $new = $_POST['goFonPermitNew'];
115         if(strlen($new)> 1) {
116           $this->goFonPermit[]= $new;
117         }
118       }
119     }
120       
121     /* handle Deny Add*/
122     if(isset($_POST['goFonDenyAdd'])){
123       if(isset($_POST['goFonDenyNew'])){
124         if(is_string($this->goFonDeny)){
125           $this->goFonDeny=array();
126         }
127         $new = $_POST['goFonDenyNew'];
128         if(strlen($new)> 1) {
129           $this->goFonDeny[]= $new;
130         }
131       }
132     }
134     /* Handle Permit Deletion*/
135     if(isset($_POST['goFonPermitDel'])){
136       if(isset($_POST['goFonPermitS'])){
137         if(is_string($this->goFonPermit)){
138           $this->goFonPermit=array();
139         }
140         $new = $_POST['goFonPermitS'];
141         $tmp = array_flip($this->goFonPermit);
142         unset($tmp[$new]);
143         $this->goFonPermit=array();
144         foreach(array_flip($tmp) as $tm){
145           $this->goFonPermit[]=$tm;
146         }
147       }
148     }
151     /* Handle Permit Deletion*/
152     if(isset($_POST['goFonDenyDel'])){
153       if(isset($_POST['goFonDenyS'])){
154         if(is_string($this->goFonDeny)){
155           $this->goFonDeny=array();
156         }
157         $new = $_POST['goFonDenyS'];
158         $tmp = array_flip($this->goFonDeny);
159         unset($tmp[$new]);
160         $this->goFonDeny=array();
161         foreach(array_flip($tmp) as $tm){
162           $this->goFonDeny[]=$tm;
163         }
164       }
165     }
166   
167     /* Fill templating stuff */
168     $smarty= get_smarty();
169     $smarty->assign("bases", $this->config->idepartments);
171     /* Create Arrays for samrty select boxes */
172     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
173     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
174     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
175     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
176     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
178     /* deativate all fields that are not used by the specified type */
179     foreach($this->attributes as $att){
180       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
181         $smarty->assign($att."USED", "disabled" );
182         $smarty->assign($att, "");
183       }else{
184         $smarty->assign($att."USED", "" );
185         $smarty->assign($att, $this->$att);
186       }
187     }
188     
189     $smarty->assign("selected_categorie",$this->selected_categorie);
191     /* Assign attributes */
192     foreach ($this->attributes as $attr){
193       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
194     }
195     $smarty->assign("base_select", $this->base);
196    
197     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
198  
199     /* Show Asterisk for required attribute ipHostNumber and macAddress */
200     $smarty->assign("staticAddress", "<font class=\"must\">*</font>");
201         
202     /* Show main page */
203     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
204     $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
205     return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
206   }
208   function remove_from_parent()
209   {
210     $ldap= $this->config->get_ldap_link();
211     $ldap->rmdir($this->dn);
212     show_ldap_error($ldap->get_error());
213     $this->handle_post_events("remove");
215     /* Delete references to object groups */
216     $ldap->cd ($this->config->current['BASE']);
217     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
218     while ($ldap->fetch()){
219       $og= new ogroup($this->config, $ldap->getDN());
220       unset($og->member[$this->dn]);
221       $og->save ();
222     }
224   }
227   /* Save data to object */
228   function save_object()
229   {
230     plugin::save_object();
232     /* Save base, since this is no LDAP attribute */
233     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
234       $this->base= $_POST['base'];
235     }
236   }
239   /* Check supplied data */
240   function check()
241   {
242     $message= array();
243     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
245     /* To check for valid ip*/
246     $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
248 //    if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
249 //      if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
250 //       $message[]= _("Wrong IP format in field goFonDefaultIP.");
251 //      }
252 //    }
253     
254 //    /* Check for valid number */
255 //    if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
256 //      if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
257 //        $message[]= _("The given value for 'Response timeout' is not a valid number.");
258 //      } 
259 //    }
261     /* must: cn, macAddress, ipHostNumber */
262     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
263       $message[]= _("The required field 'Phone name' is not set.");
264     }
265     if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
266       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
267     }
268     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
269       $message[]= _("The required field 'MAC-address' is not set.");
270     }
271     if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
272       $message[]= _("The required field 'IP-address' is not set.");
273     }
274     if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
275        $message[]= _("Wrong IP format in field IP-address.");
276     }
278     $ui= get_userinfo();
279     $acl= get_permissions ($this->dn, $ui->subtreeACL);
280     $acl= get_module_permission($acl, "phone", $this->dn);
281     if (chkacl($acl, "create") != ""){
282       $message[]= _("You have no permissions to create a phone on this 'Base'.");
283     }
285     if ($this->orig_dn != $this->dn){
286       $ldap= $this->config->get_ldap_link();
287       $ldap->cd ($this->base);
288       $ldap->search ("(cn=".$this->cn.")", array("cn"));
289       if ($ldap->count() != 0){
290         while ($attrs= $ldap->fetch()){
291           if ($attrs['dn'] != $this->orig_dn){
292             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
293             break;
294           }
295         }
296       }
297     }
299     return ($message);
300   }
303   /* Save to LDAP */
304   function save()
305   {
306     plugin::save();
307    
308    
309     /* only to define which attrs to save*/ 
310     $mode = $this->attrs['selected_categorie'];
312     /* Remove all unwanted attrs */
313     foreach($this->attributes as $att){
314       
315       /* Check all attributes, if they are needed for this type of phone */
316       if(!in_array($att,$this->usedattrs[$mode])){
317         $this->attrs[$att] = array();
318       }
319     
320       /* Test rights of this user ... */
321       if(chkacl($this->acl,$att)!=""){
322         unset($this->attrs[$att]);
323       }
324     }
326     /* unset the categorie*/
327     unset($this->attrs['selected_categorie']);
328     
329     /* Remove all empty values */
330     if ($this->orig_dn == 'new'){
331       $attrs= array();
332       foreach ($this->attrs as $key => $val){
333         if (is_array($val) && count($val) == 0){
334           continue;
335         }
336         $attrs[$key]= $val;
337       }
338       $this->attrs= $attrs;
339     }
341     if($this->goFonDefaultIP!="dynamic"){
342       $this->attrs['goFonDefaultIP'] = $this->ipHostNumber;
343     }    
345     /* Write back to ldap */
346     $ldap= $this->config->get_ldap_link();
347     if ($this->orig_dn == 'new'){
348       $ldap->cd($this->config->current['BASE']);
349       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
350       $ldap->cd($this->dn);
351       $ldap->add($this->attrs);
352       $this->handle_post_events("add");
353     } else {
354       if ($this->orig_dn != $this->dn){
355         $this->move($this->orig_dn, $this->dn);
356       }
358       $ldap->cd($this->dn);
359       $ldap->modify($this->attrs);
360       $this->handle_post_events("modify");
361     }
362     show_ldap_error($ldap->get_error());
363     /* Optionally execute a command after we're done */
364     $this->postcreate();
365   }
369 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
370 ?>