Code

daf86244adc9d3ab3130b99ca5d3c112202422cd
[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         /* Call parent execute */
100         plugin::execute();
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->cd($this->config->current['BASE']);
213     $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
214     while ($attr =  $ldap->fetch()){
215       print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
216         ($attr['uid'][0]." - ".$attr['cn'][0])));
217       return;
218     }
220     $ldap->rmdir($this->dn);
221     show_ldap_error($ldap->get_error());
222     $this->handle_post_events("remove");
224     /* Delete references to object groups */
225     $ldap->cd ($this->config->current['BASE']);
226     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
227     while ($ldap->fetch()){
228       $og= new ogroup($this->config, $ldap->getDN());
229       unset($og->member[$this->dn]);
230       $og->save ();
231     }
233   }
236   /* Save data to object */
237   function save_object()
238   {
239     plugin::save_object();
241     /* Save base, since this is no LDAP attribute */
242     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
243       $this->base= $_POST['base'];
244     }
245   }
248   /* Check supplied data */
249   function check()
250   {
251     $message= array();
252     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
254     /* To check for valid ip*/
255     $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
257 //    if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
258 //      if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
259 //       $message[]= _("Wrong IP format in field goFonDefaultIP.");
260 //      }
261 //    }
262     
263 //    /* Check for valid number */
264 //    if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
265 //      if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
266 //        $message[]= _("The given value for 'Response timeout' is not a valid number.");
267 //      } 
268 //    }
270     /* must: cn, macAddress, ipHostNumber */
271     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
272       $message[]= _("The required field 'Phone name' is not set.");
273     }
274     if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
275       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
276     }
277     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
278       $message[]= _("The required field 'MAC-address' is not set.");
279     }
280     if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
281       $message[]= _("The required field 'IP-address' is not set.");
282     }
283     if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
284        $message[]= _("Wrong IP format in field IP-address.");
285     }
287     $ui= get_userinfo();
288     $acl= get_permissions ($this->dn, $ui->subtreeACL);
289     $acl= get_module_permission($acl, "phone", $this->dn);
290     if (chkacl($acl, "create") != ""){
291       $message[]= _("You have no permissions to create a phone on this 'Base'.");
292     }
294     if ($this->orig_dn != $this->dn){
295       $ldap= $this->config->get_ldap_link();
296       $ldap->cd ($this->base);
297       $ldap->search ("(cn=".$this->cn.")", array("cn"));
298       if ($ldap->count() != 0){
299         while ($attrs= $ldap->fetch()){
300           if ($attrs['dn'] != $this->orig_dn){
301             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
302             break;
303           }
304         }
305       }
306     }
308     return ($message);
309   }
312   /* Save to LDAP */
313   function save()
314   {
315     plugin::save();
316    
317    
318     /* only to define which attrs to save*/ 
319     $mode = $this->attrs['selected_categorie'];
321     /* Remove all unwanted attrs */
322     foreach($this->attributes as $att){
323       
324       /* Check all attributes, if they are needed for this type of phone */
325       if(!in_array($att,$this->usedattrs[$mode])){
326         $this->attrs[$att] = array();
327       }
328     
329       /* Test rights of this user ... */
330       if(chkacl($this->acl,$att)!=""){
331         unset($this->attrs[$att]);
332       }
333     }
335     /* unset the categorie*/
336     unset($this->attrs['selected_categorie']);
337     
338     /* Remove all empty values */
339     if ($this->orig_dn == 'new'){
340       $attrs= array();
341       foreach ($this->attrs as $key => $val){
342         if (is_array($val) && count($val) == 0){
343           continue;
344         }
345         $attrs[$key]= $val;
346       }
347       $this->attrs= $attrs;
348     }
350     if($this->goFonDefaultIP!="dynamic"){
351       $this->attrs['goFonDefaultIP'] = $this->ipHostNumber;
352     }    
354     /* Write back to ldap */
355     $ldap= $this->config->get_ldap_link();
356     if ($this->orig_dn == 'new'){
357       $ldap->cd($this->config->current['BASE']);
358       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
359       $ldap->cd($this->dn);
360       $ldap->add($this->attrs);
361       $this->handle_post_events("add");
362     } else {
363       if ($this->orig_dn != $this->dn){
364         $this->move($this->orig_dn, $this->dn);
365       }
367       $ldap->cd($this->dn);
368       $ldap->modify($this->attrs);
369       // $user_phone_reload   
370       $ldap->cd ($this->config->current['BASE']); 
371       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
372       if($user_phone_assignment){
373         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
374         $usertab->by_object['phoneAccount']->is_modified = true;
375         $usertab->save();
376         unset($usertab);
377       }
378       $this->handle_post_events("modify");
379     }
380     show_ldap_error($ldap->get_error());
381     /* Optionally execute a command after we're done */
382     $this->postcreate();
383   }
387 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
388 ?>