Code

Added empty lines
[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();
102     /* Do we represent a valid phone? */
103     if (!$this->is_account && $this->parent == NULL){
104       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
105         _("This 'dn' has no phone features.")."</b>";
106       return($display);
107     }
109     /* handle Permit Add*/
110     if(isset($_POST['goFonPermitAdd'])){
111       if(isset($_POST['goFonPermitNew'])){
112         if(is_string($this->goFonPermit)){
113           $this->goFonPermit=array();
114         }
115         $new = $_POST['goFonPermitNew'];
116         if(strlen($new)> 1) {
117           $this->goFonPermit[]= $new;
118         }
119       }
120     }
121       
122     /* handle Deny Add*/
123     if(isset($_POST['goFonDenyAdd'])){
124       if(isset($_POST['goFonDenyNew'])){
125         if(is_string($this->goFonDeny)){
126           $this->goFonDeny=array();
127         }
128         $new = $_POST['goFonDenyNew'];
129         if(strlen($new)> 1) {
130           $this->goFonDeny[]= $new;
131         }
132       }
133     }
135     /* Handle Permit Deletion*/
136     if(isset($_POST['goFonPermitDel'])){
137       if(isset($_POST['goFonPermitS'])){
138         if(is_string($this->goFonPermit)){
139           $this->goFonPermit=array();
140         }
141         $new = $_POST['goFonPermitS'];
142         $tmp = array_flip($this->goFonPermit);
143         unset($tmp[$new]);
144         $this->goFonPermit=array();
145         foreach(array_flip($tmp) as $tm){
146           $this->goFonPermit[]=$tm;
147         }
148       }
149     }
152     /* Handle Permit Deletion*/
153     if(isset($_POST['goFonDenyDel'])){
154       if(isset($_POST['goFonDenyS'])){
155         if(is_string($this->goFonDeny)){
156           $this->goFonDeny=array();
157         }
158         $new = $_POST['goFonDenyS'];
159         $tmp = array_flip($this->goFonDeny);
160         unset($tmp[$new]);
161         $this->goFonDeny=array();
162         foreach(array_flip($tmp) as $tm){
163           $this->goFonDeny[]=$tm;
164         }
165       }
166     }
167   
168     /* Fill templating stuff */
169     $smarty= get_smarty();
170     $smarty->assign("bases", $this->config->idepartments);
172     /* Create Arrays for samrty select boxes */
173     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
174     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
175     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
176     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
177     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
179     /* deativate all fields that are not used by the specified type */
180     foreach($this->attributes as $att){
181       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
182         $smarty->assign($att."USED", "disabled" );
183         $smarty->assign($att, "");
184       }else{
185         $smarty->assign($att."USED", "" );
186         $smarty->assign($att, $this->$att);
187       }
188     }
189     
190     $smarty->assign("selected_categorie",$this->selected_categorie);
192     /* Assign attributes */
193     foreach ($this->attributes as $attr){
194       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
195     }
196     $smarty->assign("base_select", $this->base);
197    
198     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
199  
200     /* Show Asterisk for required attribute ipHostNumber and macAddress */
201     $smarty->assign("staticAddress", "<font class=\"must\">*</font>");
202         
203     /* Show main page */
204     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
205     $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
206     return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
207   }
209   function remove_from_parent()
210   {
211     $ldap= $this->config->get_ldap_link();
212     $ldap->cd($this->config->current['BASE']);
214     $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
215     while ($attr =  $ldap->fetch()){
216       print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
217         ($attr['uid'][0]." - ".$attr['cn'][0])));
218       return;
219     }
221     $ldap->rmdir($this->dn);
222     show_ldap_error($ldap->get_error());
223     $this->handle_post_events("remove");
225     /* Delete references to object groups */
226     $ldap->cd ($this->config->current['BASE']);
227     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
228     while ($ldap->fetch()){
229       $og= new ogroup($this->config, $ldap->getDN());
230       unset($og->member[$this->dn]);
231       $og->save ();
232     }
234   }
237   /* Save data to object */
238   function save_object()
239   {
240     plugin::save_object();
242     /* Save base, since this is no LDAP attribute */
243     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
244       $this->base= $_POST['base'];
245     }
246   }
249   /* Check supplied data */
250   function check()
251   {
252     $message= array();
253     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
255     /* To check for valid ip*/
256     $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
258 //    if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
259 //      if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
260 //       $message[]= _("Wrong IP format in field goFonDefaultIP.");
261 //      }
262 //    }
263     
264 //    /* Check for valid number */
265 //    if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
266 //      if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
267 //        $message[]= _("The given value for 'Response timeout' is not a valid number.");
268 //      } 
269 //    }
271     /* must: cn, macAddress, ipHostNumber */
272     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
273       $message[]= _("The required field 'Phone name' is not set.");
274     }
275     if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
276       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
277     }
278     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
279       $message[]= _("The required field 'MAC-address' is not set.");
280     }
281     if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
282       $message[]= _("The required field 'IP-address' is not set.");
283     }
284     if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
285        $message[]= _("Wrong IP format in field IP-address.");
286     }
288     $ui= get_userinfo();
289     $acl= get_permissions ($this->dn, $ui->subtreeACL);
290     $acl= get_module_permission($acl, "phone", $this->dn);
291     if (chkacl($acl, "create") != ""){
292       $message[]= _("You have no permissions to create a phone on this 'Base'.");
293     }
295     if ($this->orig_dn != $this->dn){
296       $ldap= $this->config->get_ldap_link();
297       $ldap->cd ($this->base);
298       $ldap->search ("(cn=".$this->cn.")", array("cn"));
299       if ($ldap->count() != 0){
300         while ($attrs= $ldap->fetch()){
301           if ($attrs['dn'] != $this->orig_dn){
302             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
303             break;
304           }
305         }
306       }
307     }
309     return ($message);
310   }
313   /* Save to LDAP */
314   function save()
315   {
316     plugin::save();
317    
318    
319     /* only to define which attrs to save*/ 
320     $mode = $this->attrs['selected_categorie'];
322     /* Remove all unwanted attrs */
323     foreach($this->attributes as $att){
324       
325       /* Check all attributes, if they are needed for this type of phone */
326       if(!in_array($att,$this->usedattrs[$mode])){
327         $this->attrs[$att] = array();
328       }
329     
330       /* Test rights of this user ... */
331       if(chkacl($this->acl,$att)!=""){
332         unset($this->attrs[$att]);
333       }
334     }
336     /* unset the categorie*/
337     unset($this->attrs['selected_categorie']);
338     
339     /* Remove all empty values */
340     if ($this->orig_dn == 'new'){
341       $attrs= array();
342       foreach ($this->attrs as $key => $val){
343         if (is_array($val) && count($val) == 0){
344           continue;
345         }
346         $attrs[$key]= $val;
347       }
348       $this->attrs= $attrs;
349     }
351     if($this->goFonDefaultIP!="dynamic"){
352       $this->attrs['goFonDefaultIP'] = $this->ipHostNumber;
353     }    
355     /* Write back to ldap */
356     $ldap= $this->config->get_ldap_link();
357     if ($this->orig_dn == 'new'){
358       $ldap->cd($this->config->current['BASE']);
359       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
360       $ldap->cd($this->dn);
361       $ldap->add($this->attrs);
362       $this->handle_post_events("add");
363     } else {
364       if ($this->orig_dn != $this->dn){
365         $this->move($this->orig_dn, $this->dn);
366       }
368       $ldap->cd($this->dn);
369       $ldap->modify($this->attrs);
370       // $user_phone_reload   
371       $ldap->cd ($this->config->current['BASE']); 
372       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
373       if($user_phone_assignment){
374         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
375         $usertab->by_object['phoneAccount']->is_modified = true;
376         $usertab->save();
377         unset($usertab);
378       }
379       $this->handle_post_events("modify");
380     }
381     show_ldap_error($ldap->get_error());
382     /* Optionally execute a command after we're done */
383     $this->postcreate();
384   }
388 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
389 ?>