Code

Set phone type default to friend
[gosa.git] / gosa-plugins / gofon / admin / systems / gofon / 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 $orig_cn          = "";
15   var $orig_base        = "";
16   var $goFonType        = "friend";
17   var $goFonDmtfMode    = "";
18   var $goFonHost        = "";
19   var $goFonDefaultIP   = "dynamic";
20   var $goFonQualify     = "";
21   var $goFonAuth        = "";
22   var $goFonSecret      = "";
23   var $goFonInkeys      = "";
24   var $goFonPermit      = array();
25   var $goFonDeny        = array();
26   var $goFonOutkey      = "";
27   var $goFonTrunk       = "";
28   var $goFonAccountCode = "";
29   var $goFonMSN         = "";
30   var $selected_categorie    = 0;
31   var $netConfigDNS;
32   var $view_logged      = FALSE;
33   var $baseSelector;
35   /* attribute list for save action */
36   var $attributes= array("cn", "description",
37                           "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
38                           "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
39                           "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie","goFonPermit","goFonDeny" 
40                          );
42   /* this array defines which attributes are schown / saved for the different type of phones */ 
43   var $usedattrs =    array( "0"=>array("cn", "description", 
44                                   "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
45                                   "goFonQualify"),
46                              "1"=>array("cn", "description", 
47                                   "goFonType","goFonHost","goFonDefaultIP",
48                                   "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
49                                   "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
50                              "2"=>array("cn", "description", "goFonMSN"));
51   
53   var $objectclasses= array("top", "goFonHardware");
55   function phoneGeneric (&$config, $dn= NULL, $parent= NULL)
56   {
57     plugin::plugin ($config, $dn, $parent);
58     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses, true);
59     $this->netConfigDNS->MACisMust =TRUE;
61     /* Set base */
62     if ($this->dn == "new"){
63       $ui= get_userinfo();
64       $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
65       $this->cn= "";
66     } else {
67       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("phoneRDN"), '/')."/i", "", $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     $this->orig_base= $this->base;
96     $this->orig_cn= $this->cn;
98     /* Instanciate base selector */
99     $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
100     $this->baseSelector->setSubmitButton(false);
101     $this->baseSelector->setHeight(300);
102     $this->baseSelector->update(true);
103   }
105   function set_acl_base($base)
106   {
107     plugin::set_acl_base($base);
108     $this->netConfigDNS->set_acl_base($base);
109   }
111   function set_acl_category($cat)
112   {
113     plugin::set_acl_category($cat);
114     $this->netConfigDNS->set_acl_category($cat);
115   }
118   function execute()
119   {
120     /* Call parent execute */
121     plugin::execute();
123     if($this->is_account && !$this->view_logged){
124       $this->view_logged = TRUE;
125       new log("view","phone/".get_class($this),$this->dn);
126     }
128     /* Do we represent a valid phone? */
129     if (!$this->is_account && $this->parent === NULL){
130       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
131         msgPool::noValidExtension(_("phone"))."</b>";
132       return($display);
133     }
135     /* handle Permit Add*/
136     if(isset($_POST['goFonPermitAdd']) && $this->acl_is_writeable("goFonPermit")){
137       if(isset($_POST['goFonPermitNew'])){
138         if(is_string($this->goFonPermit)){
139           $this->goFonPermit=array();
140         }
141         $new = $_POST['goFonPermitNew'];
142         if(strlen($new)> 1) {
143           $this->goFonPermit[]= $new;
144         }
145       }
146     }
147       
148     /* handle Deny Add*/
149     if(isset($_POST['goFonDenyAdd']) && $this->acl_is_writeable("goFonDeny")){
150       if(isset($_POST['goFonDenyNew'])){
151         if(is_string($this->goFonDeny)){
152           $this->goFonDeny=array();
153         }
154         $new = $_POST['goFonDenyNew'];
155         if(strlen($new)> 1) {
156           $this->goFonDeny[]= $new;
157         }
158       }
159     }
161     /* Handle Permit Deletion*/
162     if(isset($_POST['goFonPermitDel']) && $this->acl_is_writeable("goFonPermit")){
163       if(isset($_POST['goFonPermitS'])){
164         if(is_string($this->goFonPermit)){
165           $this->goFonPermit=array();
166         }
167         $new = $_POST['goFonPermitS'];
168         $tmp = array_flip($this->goFonPermit);
169         unset($tmp[$new]);
170         $this->goFonPermit=array();
171         foreach(array_flip($tmp) as $tm){
172           $this->goFonPermit[]=$tm;
173         }
174       }
175     }
178     /* Handle Permit Deletion*/
179     if(isset($_POST['goFonDenyDel']) && $this->acl_is_writeable("goFonDeny")){
180       if(isset($_POST['goFonDenyS'])){
181         if(is_string($this->goFonDeny)){
182           $this->goFonDeny=array();
183         }
184         $new = $_POST['goFonDenyS'];
185         $tmp = array_flip($this->goFonDeny);
186         unset($tmp[$new]);
187         $this->goFonDeny=array();
188         foreach(array_flip($tmp) as $tm){
189           $this->goFonDeny[]=$tm;
190         }
191       }
192     }
193   
194     /* Fill templating stuff */
195     $smarty= get_smarty();
196     $smarty->assign("usePrototype", "true");
198     $tmp = $this->plInfo();
199     foreach($tmp['plProvidedAcls'] as $name => $translation){
200       $smarty->assign($name."ACL",$this->getacl($name));
201     }
203     $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
205     /* Create Arrays for samrty select boxes */
206     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
207     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
208     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
209     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
210     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
212     /* deativate all fields that are not used by the specified type */
213     foreach($this->attributes as $att){
214       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))){
215         $smarty->assign($att."USED", "disabled" );
216         $smarty->assign($att, "");
217       }else{
218         $smarty->assign($att."USED", "" );
219         $smarty->assign($att, $this->$att);
220       }
221     }
222     
223     $smarty->assign("selected_categorie",$this->selected_categorie);
225     /* Assign attributes */
226     $smarty->assign("base", $this->baseSelector->render());
227     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
228  
229     /* Show main page */
230     $str = $this->netConfigDNS->execute();
231     if(is_object($this->netConfigDNS->dialog)){
232       return($str);
233     }
234     $smarty->assign("netconfig", $str);
235     $smarty->assign("phonesettings", get_template_path("phonesettings.tpl",TRUE,dirname(__FILE__)));
236     return($smarty->fetch (get_template_path('phone.tpl', TRUE,dirname(__FILE__))));
237   }
239   function remove_from_parent()
240   {
241     if($this->acl_is_removeable()){
242       $ldap= $this->config->get_ldap_link();
243       $ldap->cd($this->config->current['BASE']);
245       $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
246       while ($attr =  $ldap->fetch()){
247         msg_dialog::display(_("Error"), sprintf(_("Cannot delete entry because it is still in use by '%s'!"), $attr['cn'][0]), ERROR_DIALOG);
248         return;
249       }
251       $this->netConfigDNS->remove_from_parent();
252       $ldap->rmdir($this->dn);
253   
254       new log("remove","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
256       if (!$ldap->success()){
257         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
258       }
259       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
261       /* Delete references to object groups */
262       $ldap->cd ($this->config->current['BASE']);
263       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
264       while ($ldap->fetch()){
265         $og= new ogroup($this->config, $ldap->getDN());
266         unset($og->member[$this->dn]);
267         $og->save ();
268       }
269     }
270   }
273   /* Save data to object */
274   function save_object()
275   {
276     /* Create a base backup and reset the
277        base directly after calling plugin::save_object();
278        Base will be set seperatly a few lines below */
279     $base_tmp = $this->base;
280     plugin::save_object();
281     $this->base = $base_tmp;
283     $this->netConfigDNS->save_object();
285     /* Refresh base */
286     if ($this->acl_is_moveable($this->base)){
287       if (!$this->baseSelector->update()) {
288         msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
289       }
290       if ($this->base != $this->baseSelector->getBase()) {
291         $this->base= $this->baseSelector->getBase();
292         $this->is_modified= TRUE;
293       }
294     }
296   }
299   /* Check supplied data */
300   function check()
301   {
302     /* Call common method to give check the hook */
303     $message= plugin::check();
304     $message= array_merge($message, $this->netConfigDNS->check());
306     $this->dn= "cn=".$this->cn.",".get_ou('phoneRDN').$this->base;
308     /* To check for valid ip*/
309     if($this->netConfigDNS->ipHostNumber == ""){
310        $message[]=  msgPool::required(_("IP address"));
311     } else {
312       if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
313         $message[]= msgPool::invalid(_("IP address"));
314       }
315     }
317     // Check if a wrong base was supplied
318     if(!$this->baseSelector->checkLastBaseUpdate()){
319       $message[]= msgPool::check_base();;
320     }
322     /* Check if given name is a valid host/dns name */
323     if(!tests::is_dns_name($this->cn) ){
324       $message[] = msgPool::invalid(_("Name"));
325     }
327     if ($this->cn == ""){
328       $message[]= msgPool::required(_("Name"));
329     }
330     if ($this->cn == "0"){ 
331       $message[]= msgPool::reserved(_("Name"));
332     }
334     if ($this->orig_dn != $this->dn){
335       $ldap= $this->config->get_ldap_link();
336       $ldap->cd ($this->base);
337       $ldap->search ("(cn=".$this->cn.")", array("cn"));
338       if ($ldap->count() != 0){
339         while ($attrs= $ldap->fetch()){
341           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
342             continue;
343           }
344   
345           if ($attrs['dn'] != $this->orig_dn){
346             $message[]= msgPool::duplicated(_("Name"));
347             break;
348           }
349         }
350       }
351     }
353     /* Check if we are allowed to create or move this object
354      */
355     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
356       $message[] = msgPool::permCreate();
357     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
358       $message[] = msgPool::permMove();
359     }
361     return ($message);
362   }
365   /* Save to LDAP */
366   function save()
367   {
368     plugin::save();
369    
370    
371     /* only to define which attrs to save*/ 
372     $mode = $this->selected_categorie;
374     /* Remove all unwanted attrs */
375     foreach($this->attributes as $att){
376       
377       /* Check all attributes, if they are needed for this type of phone */
378       if(!in_array($att,$this->usedattrs[$mode])){
379         $this->attrs[$att] = array();
380       }
381     }
383     /* unset the categorie*/
384     unset($this->attrs['selected_categorie']);
385     
386     /* Remove all empty values */
387     if ($this->orig_dn == 'new'){
388       $attrs= array();
389       foreach ($this->attrs as $key => $val){
390         if (is_array($val) && count($val) == 0){
391           continue;
392         }
393         $attrs[$key]= $val;
394       }
395       $this->attrs= $attrs;
396     }
398     if($this->goFonDefaultIP!="dynamic"){
399       $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
400     }    
402     $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
404     /* Write back to ldap */
405     $ldap= $this->config->get_ldap_link();
406     if ($this->orig_dn == 'new'){
407       $ldap->cd($this->config->current['BASE']);
408       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
409       $ldap->cd($this->dn);
410       $ldap->add($this->attrs);
411       if (!$ldap->success()){
412         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
413       }
414       new log("create","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
415       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
416     } else {
417       if ($this->orig_dn != $this->dn){
418         $this->move($this->orig_dn, $this->dn);
419       }
421       $ldap->cd($this->dn);
422       $this->cleanup();
423       $ldap->modify ($this->attrs); 
424       new log("modify","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
425       if (!$ldap->success()){
426         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
427       }
429       // $user_phone_reload   
430       $ldap->cd ($this->config->current['BASE']); 
431       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
432       if($user_phone_assignment){
433         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
434         $usertab->by_object['phoneAccount']->is_modified = true;
435         $usertab->save();
436         unset($usertab);
437       }
438       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
439     }
440     $this->netConfigDNS->cn = $this->cn;    
441     $this->netConfigDNS->save();
442     if (!$ldap->success()){
443       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
444     }
445   }
448   /* Display generic part for server copy & paste */
449   function getCopyDialog()
450   {
451     $vars = array("cn");
452     $smarty = get_smarty();
453     $smarty->assign("cn" ,$this->cn);
454     $smarty->assign("object","phone");
455     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
456     $ret = array();
457     $ret['string'] = $str;
458     $ret['status'] = "";
459     return($ret);
460   }
463   function saveCopyDialog()
464   {
465     if(isset($_POST['cn'])){
466       $this->cn = $_POST['cn'];
467     }
468   }
471   function PrepareForCopyPaste($source)
472   {
473     plugin::PrepareForCopyPaste($source);
474     if(isset($source['macAddress'][0])){
475       $this->netConfigDNS->macAddress = $source['macAddress'][0];
476     }
477     if(isset($source['ipHostNumber'][0])){
478       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
479     }
481     $source_o = new phoneGeneric($this->config, $source['dn']);
482     $this->selected_categorie = $source_o->selected_categorie;
483   }
486   /* Return plugin informations for acl handling
487       #FIXME FAIscript seams to ununsed within this class... */
488   static function plInfo()
489   {
490     return (array(
491           "plShortName"   => _("Generic"),
492           "plDescription" => _("Phone generic"),
493           "plSelfModify"  => FALSE,
494           "plDepends"     => array(),
495           "plPriority"    => 0,
496           "plSection"     => array("administration"),
497           "plCategory"    => array("phone" => array("description"  => _("Phone hardware"),
498                                                     "objectClass"  => "goFonHardware")),
499           "plProvidedAcls"=> array(
500             "cn"                  => _("Name"),
501             "base"                => _("Base"),
502             "description"         => _("Description"),
503             "goFonType"           => _("SIP Mode"),
504             "goFonDmtfMode"       => _("SIP DTMF mode"),
505             "goFonDefaultIP"      => _("SIP Default ip"),
506             "goFonQualify"        => _("SIP Qualify"),
507             "goFonAuth"           => _("IAX authentication type"),
508             "goFonSecret"         => _("IAX secret"),
509             "goFonAccountCode"    => _("IAX account code"),
510             "goFonTrunk"          => _("IAX trunk lines"),
511             "goFonPermit"         => _("IAX permit settings"),
512             "goFonDeny"           => _("IAX deny settings"),
513             "goFonMSN"            => _("CAPI MSN"),
514             "categorie"  => _("Hardware type"))
515           ));
516   }
521 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
522 ?>