Code

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