Code

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