Code

Updated translation, fixed some typo/errors
[gosa.git] / plugins / admin / systems / class_phoneGeneric.inc
1 <?php
3 class phoneGeneric extends plugin
4 {
5   /* Generic terminal attributes */
6   var $interfaces= array();
7   var $ignore_account= TRUE;
9   /* Needed values and lists */
10   var $base             = "";
11   var $cn               = "";
12   var $description      = "";
13   var $orig_dn          = "";
14   var $goFonType        = "";
15   var $goFonDmtfMode    = "";
16   var $goFonHost        = "";
17   var $goFonDefaultIP   = "dynamic";
18   var $goFonQualify     = "";
19   var $goFonAuth        = "";
20   var $goFonSecret      = "";
21   var $goFonInkeys      = "";
22   var $goFonPermit      = array();
23   var $goFonDeny        = array();
24   var $goFonOutkey      = "";
25   var $goFonTrunk       = "";
26   var $goFonAccountCode = "";
27   var $goFonMSN         = "";
28   var $selected_categorie    = 0;
29   var $netConfigDNS;
31   /* attribute list for save action */
32   var $attributes= array("cn", "description", 
33                           "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
34                           "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
35                           "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie","goFonPermit","goFonDeny" 
36                          );
38   /* this array defines which attributes are schown / saved for the different type of phones */ 
39   var $usedattrs =    array( "0"=>array("cn", "description", 
40                                   "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
41                                   "goFonQualify"),
42                              "1"=>array("cn", "description", 
43                                   "goFonType","goFonHost","goFonDefaultIP",
44                                   "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
45                                   "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
46                              "2"=>array("cn", "description", "goFonMSN"));
47   
49   var $objectclasses= array("top", "goFonHardware");
51   function phonegeneric ($config, $dn= NULL, $parent= NULL)
52   {
53     plugin::plugin ($config, $dn, $parent);
54     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
55     $this->netConfigDNS->acl = $this->acl;
57     /* Set base */
58     if ($this->dn == "new"){
59       $ui= get_userinfo();
60       $this->base= dn2base($ui->dn);
61       $this->cn= "";
62     } else {
63       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
64     }
65   
66     if($this->goFonMSN != ""){
67       $this->selected_categorie = 2;
68     }elseif($this->goFonAccountCode != ""){
69       $this->selected_categorie = 1 ;
70     
71       if(isset($this->attrs['goFonPermit']['count'])){
72         unset ($this->attrs['goFonPermit']['count']);
73         $this->goFonPermit=$this->attrs['goFonPermit'];
74       }  
75      
76       if(isset($this->attrs['goFonDeny']['count'])){
77         unset ($this->attrs['goFonDeny']['count'])   ;
78         $this->goFonDeny=$this->attrs['goFonDeny'];
79       } 
81     } else {
82       $this->selected_categorie = 0;
83     }
85     if($this->goFonDefaultIP!="dynamic"){
86       $this->goFonDefaultIP = "network";
87     }
89     /* Save dn for later references */
90     $this->orig_dn= $this->dn;
91   }
93   function execute()
94   {
95     $this->netConfigDNS->acl = $this->acl;
97     /* Call parent execute */
98     plugin::execute();
100     /* Do we represent a valid phone? */
101     if (!$this->is_account && $this->parent == NULL){
102       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
103         _("This 'dn' has no phone features.")."</b>";
104       return($display);
105     }
107     /* Base select dialog */
108     $once = true;
109     foreach($_POST as $name => $value){
110       if(preg_match("/^chooseBase/",$name) && $once){
111         $once = false;
112         $this->dialog = new baseSelectDialog($this->config);
113         $this->dialog->setCurrentBase($this->base);
114       }
115     }
117     /* Dialog handling */
118     if(is_object($this->dialog)){
119       /* Must be called before save_object */
120       $this->dialog->save_object();
122       if($this->dialog->isClosed()){
123         $this->dialog = false;
124       }elseif($this->dialog->isSelected()){
125         $this->base = $this->dialog->isSelected();
126         $this->dialog= false;
127       }else{
128         return($this->dialog->execute());
129       }
130     }
132     /* handle Permit Add*/
133     if(isset($_POST['goFonPermitAdd'])){
134       if(isset($_POST['goFonPermitNew'])){
135         if(is_string($this->goFonPermit)){
136           $this->goFonPermit=array();
137         }
138         $new = $_POST['goFonPermitNew'];
139         if(strlen($new)> 1) {
140           $this->goFonPermit[]= $new;
141         }
142       }
143     }
144       
145     /* handle Deny Add*/
146     if(isset($_POST['goFonDenyAdd'])){
147       if(isset($_POST['goFonDenyNew'])){
148         if(is_string($this->goFonDeny)){
149           $this->goFonDeny=array();
150         }
151         $new = $_POST['goFonDenyNew'];
152         if(strlen($new)> 1) {
153           $this->goFonDeny[]= $new;
154         }
155       }
156     }
158     /* Handle Permit Deletion*/
159     if(isset($_POST['goFonPermitDel'])){
160       if(isset($_POST['goFonPermitS'])){
161         if(is_string($this->goFonPermit)){
162           $this->goFonPermit=array();
163         }
164         $new = $_POST['goFonPermitS'];
165         $tmp = array_flip($this->goFonPermit);
166         unset($tmp[$new]);
167         $this->goFonPermit=array();
168         foreach(array_flip($tmp) as $tm){
169           $this->goFonPermit[]=$tm;
170         }
171       }
172     }
175     /* Handle Permit Deletion*/
176     if(isset($_POST['goFonDenyDel'])){
177       if(isset($_POST['goFonDenyS'])){
178         if(is_string($this->goFonDeny)){
179           $this->goFonDeny=array();
180         }
181         $new = $_POST['goFonDenyS'];
182         $tmp = array_flip($this->goFonDeny);
183         unset($tmp[$new]);
184         $this->goFonDeny=array();
185         foreach(array_flip($tmp) as $tm){
186           $this->goFonDeny[]=$tm;
187         }
188       }
189     }
190   
191     /* Fill templating stuff */
192     $smarty= get_smarty();
193     $smarty->assign("bases", $this->config->idepartments);
194     $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
196     /* Create Arrays for samrty select boxes */
197     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
198     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
199     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
200     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
201     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
203     /* deativate all fields that are not used by the specified type */
204     foreach($this->attributes as $att){
205       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
206         $smarty->assign($att."USED", "disabled" );
207         $smarty->assign($att, "");
208       }else{
209         $smarty->assign($att."USED", "" );
210         $smarty->assign($att, $this->$att);
211       }
212     }
213     
214     $smarty->assign("selected_categorie",$this->selected_categorie);
216     /* Assign attributes */
217     foreach ($this->attributes as $attr){
218       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
219     }
220     $smarty->assign("base_select", $this->base);
221     $smarty->assign("baseACL", chkacl($this->acl,"base"));
222    
223     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Network settings")));
224     $this->netConfigDNS->cn= $this->cn;
225     $smarty->assign("netconfig", $this->netConfigDNS->execute());
227     /* Display sub dialog from network settings */
228     if($this->netConfigDNS->dialog){
229       $this->dialog = TRUE;
230       return($this->netConfigDNS->execute());
231     }else{ 
232       $this->dialog = FALSE;
233     }
235     /* Show main page */
236     $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
237     return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
238   }
240   function remove_from_parent()
241   {
242     $this->netConfigDNS->acl = $this->acl;
243     $ldap= $this->config->get_ldap_link();
244     $ldap->cd($this->config->current['BASE']);
246     $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
247     while ($attr =  $ldap->fetch()){
248       print_red(sprintf(_("Can't delete because there are users which are depending on this phone. One of them is user '%s'."),
249         ($attr['uid'][0]." - ".$attr['cn'][0])));
250       return;
251     }
253     $this->netConfigDNS->remove_from_parent();
254     $ldap->rmdir($this->dn);
255     show_ldap_error($ldap->get_error(), _("Removing phone failed"));
256     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
258     /* Delete references to object groups */
259     $ldap->cd ($this->config->current['BASE']);
260     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
261     while ($ldap->fetch()){
262       $og= new ogroup($this->config, $ldap->getDN());
263       unset($og->member[$this->dn]);
264       $og->save ();
265     }
266   }
269   /* Save data to object */
270   function save_object()
271   {
272     plugin::save_object();
273     $this->netConfigDNS->save_object();
274     /* Save base, since this is no LDAP attribute */
275     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
276       $this->base= $_POST['base'];
277     }
278   }
281   /* Check supplied data */
282   function check()
283   {
284     /* Call common method to give check the hook */
285     $message= plugin::check();
286     $message= array_merge($message, $this->netConfigDNS->check());
288     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
290     /* To check for valid ip*/
291     if($this->netConfigDNS->ipHostNumber == ""){
292        $message[]= _("The required field IP address is empty.");
293     } else {
294       if (!is_ip($this->netConfigDNS->ipHostNumber)){
295         $message[]= _("The field IP address contains an invalid address.");
296       }
297     }
299     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
300       $message[]= _("The required field 'Phone name' is not set.");
301     }
302     if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
303       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
304     }
306     $ui= get_userinfo();
307     $acl= get_permissions ($this->dn, $ui->subtreeACL);
308     $acl= get_module_permission($acl, "phone", $this->dn);
309     if (chkacl($acl, "create") != ""){
310       $message[]= _("You have no permissions to create a phone on this 'Base'.");
311     }
313     if ($this->orig_dn != $this->dn){
314       $ldap= $this->config->get_ldap_link();
315       $ldap->cd ($this->base);
316       $ldap->search ("(cn=".$this->cn.")", array("cn"));
317       if ($ldap->count() != 0){
318         while ($attrs= $ldap->fetch()){
320           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
321             continue;
322           }
323   
324           if ($attrs['dn'] != $this->orig_dn){
325             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
326             break;
327           }
328         }
329       }
330     }
332     return ($message);
333   }
336   /* Save to LDAP */
337   function save()
338   {
339     $this->netConfigDNS->acl = $this->acl;
340     plugin::save();
341    
342    
343     /* only to define which attrs to save*/ 
344     $mode = $this->attrs['selected_categorie'];
346     /* Remove all unwanted attrs */
347     foreach($this->attributes as $att){
348       
349       /* Check all attributes, if they are needed for this type of phone */
350       if(!in_array($att,$this->usedattrs[$mode])){
351         $this->attrs[$att] = array();
352       }
353     
354       /* Test rights of this user ... */
355       if(chkacl($this->acl,$att)!=""){
356         unset($this->attrs[$att]);
357       }
358     }
360     /* unset the categorie*/
361     unset($this->attrs['selected_categorie']);
362     
363     /* Remove all empty values */
364     if ($this->orig_dn == 'new'){
365       $attrs= array();
366       foreach ($this->attrs as $key => $val){
367         if (is_array($val) && count($val) == 0){
368           continue;
369         }
370         $attrs[$key]= $val;
371       }
372       $this->attrs= $attrs;
373     }
375     if($this->goFonDefaultIP!="dynamic"){
376       $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
377     }    
379     $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
381     /* Write back to ldap */
382     $ldap= $this->config->get_ldap_link();
383     if ($this->orig_dn == 'new'){
384       $ldap->cd($this->config->current['BASE']);
385       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
386       $ldap->cd($this->dn);
387       $ldap->add($this->attrs);
388       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
389     } else {
390       if ($this->orig_dn != $this->dn){
391         $this->move($this->orig_dn, $this->dn);
392       }
394       $ldap->cd($this->dn);
395       $this->cleanup();
396       $ldap->modify ($this->attrs); 
398       // $user_phone_reload   
399       $ldap->cd ($this->config->current['BASE']); 
400       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
401       if($user_phone_assignment){
402         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
403         $usertab->by_object['phoneAccount']->is_modified = true;
404         $usertab->save();
405         unset($usertab);
406       }
407       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
408     }
409     $this->netConfigDNS->cn = $this->cn;    
410     $this->netConfigDNS->save($this->dn);
411     show_ldap_error($ldap->get_error(), _("Saving phone failed"));
412   }
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>