Code

Added some fixes for iframe scrolling
[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, $parent= NULL)
57   {
58     plugin::plugin ($config, $dn, $parent);
59     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
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 set_acl_base($base)
98   {
99     plugin::set_acl_base($base);
100     $this->netConfigDNS->set_acl_base($base);
101   }
103   function set_acl_category($cat)
104   {
105     plugin::set_acl_category($cat);
106     $this->netConfigDNS->set_acl_category($cat);
107   }
110   function execute()
111   {
112         /* Call parent execute */
113         plugin::execute();
115     /* Do we represent a valid phone? */
116     if (!$this->is_account && $this->parent == NULL){
117       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
118         _("This 'dn' has no phone features.")."</b>";
119       return($display);
120     }
122     /* Base select dialog */
123     $once = true;
124     foreach($_POST as $name => $value){
125       if(preg_match("/^chooseBase/",$name) && $once){
126         $once = false;
127         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
128         $this->dialog->setCurrentBase($this->base);
129       }
130     }
132     /* Dialog handling */
133     if(is_object($this->dialog)){
134       /* Must be called before save_object */
135       $this->dialog->save_object();
137       if($this->dialog->isClosed()){
138         $this->dialog = false;
139       }elseif($this->dialog->isSelected()){
140         
141         /* A new base was selected, check if it is a valid one */
142         $tmp = $this->get_allowed_bases();
143         if(isset($tmp[$this->dialog->isSelected()])){
144           $this->base = $this->dialog->isSelected();
145         }
147         $this->dialog= false;
148       }else{
149         return($this->dialog->execute());
150       }
151     }
153     /* handle Permit Add*/
154     if(isset($_POST['goFonPermitAdd']) && $this->acl_is_writeable("goFonPermit")){
155       if(isset($_POST['goFonPermitNew'])){
156         if(is_string($this->goFonPermit)){
157           $this->goFonPermit=array();
158         }
159         $new = $_POST['goFonPermitNew'];
160         if(strlen($new)> 1) {
161           $this->goFonPermit[]= $new;
162         }
163       }
164     }
165       
166     /* handle Deny Add*/
167     if(isset($_POST['goFonDenyAdd']) && $this->acl_is_writeable("goFonDeny")){
168       if(isset($_POST['goFonDenyNew'])){
169         if(is_string($this->goFonDeny)){
170           $this->goFonDeny=array();
171         }
172         $new = $_POST['goFonDenyNew'];
173         if(strlen($new)> 1) {
174           $this->goFonDeny[]= $new;
175         }
176       }
177     }
179     /* Handle Permit Deletion*/
180     if(isset($_POST['goFonPermitDel']) && $this->acl_is_writeable("goFonPermit")){
181       if(isset($_POST['goFonPermitS'])){
182         if(is_string($this->goFonPermit)){
183           $this->goFonPermit=array();
184         }
185         $new = $_POST['goFonPermitS'];
186         $tmp = array_flip($this->goFonPermit);
187         unset($tmp[$new]);
188         $this->goFonPermit=array();
189         foreach(array_flip($tmp) as $tm){
190           $this->goFonPermit[]=$tm;
191         }
192       }
193     }
196     /* Handle Permit Deletion*/
197     if(isset($_POST['goFonDenyDel']) && $this->acl_is_writeable("goFonDeny")){
198       if(isset($_POST['goFonDenyS'])){
199         if(is_string($this->goFonDeny)){
200           $this->goFonDeny=array();
201         }
202         $new = $_POST['goFonDenyS'];
203         $tmp = array_flip($this->goFonDeny);
204         unset($tmp[$new]);
205         $this->goFonDeny=array();
206         foreach(array_flip($tmp) as $tm){
207           $this->goFonDeny[]=$tm;
208         }
209       }
210     }
211   
212     /* Fill templating stuff */
213     $smarty= get_smarty();
215     $smarty->assign("bases", $this->get_allowed_bases());
217     $tmp = $this->plInfo();
218     foreach($tmp['plProvidedAcls'] as $name => $translation){
219       $smarty->assign($name."ACL",$this->getacl($name));
220     }
222     $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
224     /* Create Arrays for samrty select boxes */
225     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
226     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
227     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
228     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
229     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
231     /* deativate all fields that are not used by the specified type */
232     foreach($this->attributes as $att){
233       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))){
234         $smarty->assign($att."USED", "disabled" );
235         $smarty->assign($att, "");
236       }else{
237         $smarty->assign($att."USED", "" );
238         $smarty->assign($att, $this->$att);
239       }
240     }
241     
242     $smarty->assign("selected_categorie",$this->selected_categorie);
244     /* Assign attributes */
245     $smarty->assign("base_select", $this->base);
246     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
247  
248     /* Show main page */
249     $smarty->assign("netconfig", $this->netConfigDNS->execute());
250     $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
251     return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
252   }
254   function remove_from_parent()
255   {
256     if($this->acl_is_removeable()){
257       $ldap= $this->config->get_ldap_link();
258       $ldap->cd($this->config->current['BASE']);
260       $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
261       while ($attr =  $ldap->fetch()){
262         print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
263               ($attr['uid'][0]." - ".$attr['cn'][0])));
264         return;
265       }
267       $this->netConfigDNS->remove_from_parent();
268       $ldap->rmdir($this->dn);
269       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system phone/generic with dn '%s' failed."),$this->dn));
270       $this->handle_post_events("remove");
272       /* Delete references to object groups */
273       $ldap->cd ($this->config->current['BASE']);
274       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
275       while ($ldap->fetch()){
276         $og= new ogroup($this->config, $ldap->getDN());
277         unset($og->member[$this->dn]);
278         $og->save ();
279       }
280     }
281   }
284   /* Save data to object */
285   function save_object()
286   {
287     /* Create a base backup and reset the
288        base directly after calling plugin::save_object();
289        Base will be set seperatly a few lines below */
290     $base_tmp = $this->base;
291     plugin::save_object();
292     $this->base = $base_tmp;
294     $this->netConfigDNS->save_object();
296     /* Set new base if allowed */
297     $tmp = $this->get_allowed_bases();
298     if(isset($_POST['base'])){
299       if(isset($tmp[$_POST['base']])){
300         $this->base= $_POST['base'];
301       }
302     }
303   }
306   /* Check supplied data */
307   function check()
308   {
309     /* Call common method to give check the hook */
310     $message= plugin::check();
311     $message= array_merge($message, $this->netConfigDNS->check());
313     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
315     /* To check for valid ip*/
316     if($this->netConfigDNS->ipHostNumber == ""){
317        $message[]= _("The required field IP address is empty.");
318     } else {
319       if (!is_ip($this->netConfigDNS->ipHostNumber)){
320         $message[]= _("The field IP address contains an invalid address.");
321       }
322     }
324     if ($this->cn == ""){
325       $message[]= _("The required field 'Phone name' is not set.");
326     }
327     if ($this->cn == "0"){ 
328       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
329     }
331     if ($this->orig_dn != $this->dn){
332       $ldap= $this->config->get_ldap_link();
333       $ldap->cd ($this->base);
334       $ldap->search ("(cn=".$this->cn.")", array("cn"));
335       if ($ldap->count() != 0){
336         while ($attrs= $ldap->fetch()){
337           if ($attrs['dn'] != $this->orig_dn){
338             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
339             break;
340           }
341         }
342       }
343     }
345     return ($message);
346   }
349   /* Save to LDAP */
350   function save()
351   {
352     plugin::save();
353    
354    
355     /* only to define which attrs to save*/ 
356     $mode = $this->attrs['selected_categorie'];
358     /* Remove all unwanted attrs */
359     foreach($this->attributes as $att){
360       
361       /* Check all attributes, if they are needed for this type of phone */
362       if(!in_array($att,$this->usedattrs[$mode])){
363         $this->attrs[$att] = array();
364       }
365     }
367     /* unset the categorie*/
368     unset($this->attrs['selected_categorie']);
369     
370     /* Remove all empty values */
371     if ($this->orig_dn == 'new'){
372       $attrs= array();
373       foreach ($this->attrs as $key => $val){
374         if (is_array($val) && count($val) == 0){
375           continue;
376         }
377         $attrs[$key]= $val;
378       }
379       $this->attrs= $attrs;
380     }
382     if($this->goFonDefaultIP!="dynamic"){
383       $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
384     }    
386     $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
388     /* Write back to ldap */
389     $ldap= $this->config->get_ldap_link();
390     if ($this->orig_dn == 'new'){
391       $ldap->cd($this->config->current['BASE']);
392       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
393       $ldap->cd($this->dn);
394       $ldap->add($this->attrs);
395       $this->handle_post_events("add");
396     } else {
397       if ($this->orig_dn != $this->dn){
398         $this->move($this->orig_dn, $this->dn);
399       }
401       $ldap->cd($this->dn);
402       $this->cleanup();
403       $ldap->modify ($this->attrs); 
405       // $user_phone_reload   
406       $ldap->cd ($this->config->current['BASE']); 
407       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
408       if($user_phone_assignment){
409         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
410         $usertab->by_object['phoneAccount']->is_modified = true;
411         $usertab->save();
412         unset($usertab);
413       }
414       $this->handle_post_events("modify");
415     }
416     $this->netConfigDNS->cn = $this->cn;    
417     $this->netConfigDNS->save($this->dn);
418     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system phone/generic with dn '%s' failed."),$this->dn));
420     /* Optionally execute a command after we're done */
421     $this->postcreate();
422   }
425   /* Return plugin informations for acl handling
426       #FIXME FAIscript seams to ununsed within this class... */
427   function plInfo()
428   {
429     return (array(
430           "plShortName"   => _("Generic"),
431           "plDescription" => _("Phone generic"),
432           "plSelfModify"  => FALSE,
433           "plDepends"     => array(),
434           "plPriority"    => 0,
435           "plSection"     => array("administration"),
436           "plCategory"    => array("phone" => array("description"  => _("Phone hardware"),
437                                                     "objectClass"  => "goFonHardware")),
438           "plProvidedAcls"=> array(
439             "cn"                  => _("Name"),
440             "base"                => _("Base"),
441             "description"         => _("Description"),
442             "goFonType"           => _("SIP Mode"),
443             "goFonDmtfMode"       => _("SIP DTMF mode"),
444             "goFonDefaultIP"      => _("SIP Default ip"),
445             "goFonQualify"        => _("SIP Qualify"),
446             "goFonAuth"           => _("IAX authentication type"),
447             "goFonSecret"         => _("IAX secret"),
448             "goFonAccountCode"    => _("IAX account code"),
449             "goFonTrunk"          => _("IAX trunk lines"),
450             "goFonPermit"         => _("IAX permit settings"),
451             "goFonDeny"           => _("IAX deny settings"),
452             "goFonMSN"            => _("CAPI MSN"),
453             "categorie"  => _("Hardware type"))
454           ));
455   }
460 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
461 ?>