Code

Backport from trunk
[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_category    = 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_category","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_category","goFonPermit","goFonDeny"),
50             "2"=>array("cn", "description", "goFonMSN"));
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("phoneGeneric", "phoneRDN"), '/')."/i", "", $this->dn);
68         }
70         if($this->goFonMSN != ""){
71             $this->selected_category = 2;
72         }elseif($this->goFonAccountCode != ""){
73             $this->selected_category = 1 ;
75             if(isset($this->attrs['goFonPermit']['count'])){
76                 unset ($this->attrs['goFonPermit']['count']);
77                 $this->goFonPermit=$this->attrs['goFonPermit'];
78             }  
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_category = 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 = get_post('goFonPermitNew');
142                 if(strlen($new)> 1) {
143                     $this->goFonPermit[]= $new;
144                 }
145             }
146         }
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 = get_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 = get_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 = get_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         }
194         /* Fill templating stuff */
195         $smarty= get_smarty();
198         $tmp = $this->plInfo();
199         foreach($tmp['plProvidedAcls'] as $name => $translation){
200             $smarty->assign($name."ACL",$this->getacl($name));
201         }
203         /* Create Arrays for samrty select boxes */
204         $smarty->assign("categorys",    set_post(array("SIP","IAX","CAPI")));
205         $smarty->assign("goFonTypes",    set_post(array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend")));
206         $smarty->assign("goFonDmtfModes",set_post(array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info")));
207         $smarty->assign("goFonAuths",    set_post(array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/)));
208         $smarty->assign("goFonTrunks",   set_post(array("yes" =>_("yes") ,"no" => _("no"))));
210         /* deativate all fields that are not used by the specified type */
211         foreach($this->attributes as $att){
212             if((!in_array_strict($att,$this->usedattrs[$this->selected_category]))){
213                 $smarty->assign($att."USED", "disabled" );
214                 $smarty->assign($att, "");
215             }else{
216                 $smarty->assign($att."USED", "" );
217                 $smarty->assign($att, set_post($this->$att));
218             }
219         }
221         $smarty->assign("selected_category", set_post($this->selected_category));
223         /* Assign attributes */
224         $smarty->assign("base", $this->baseSelector->render());
225         $smarty->assign("goFonDefaultIPs",set_post(array("dynamic"=>_("dynamic"),"network"=>_("Network settings"))));
227         /* Show main page */
228         $str = $this->netConfigDNS->execute();
229         if(is_object($this->netConfigDNS->dialog)){
230             return($str);
231         }
232         $smarty->assign("netconfig", $str);
233         $smarty->assign("phonesettings", get_template_path("phonesettings.tpl",TRUE,dirname(__FILE__)));
234         return($smarty->fetch (get_template_path('phone.tpl', TRUE,dirname(__FILE__))));
235     }
237     function remove_from_parent()
238     {
239         if($this->acl_is_removeable()){
240             $ldap= $this->config->get_ldap_link();
241             $ldap->cd($this->config->current['BASE']);
243             $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
244             while ($attr =  $ldap->fetch()){
245                 msg_dialog::display(_("Error"), sprintf(_("Cannot delete entry because it is still in use by '%s'!"), $attr['cn'][0]), ERROR_DIALOG);
246                 return;
247             }
249             $this->netConfigDNS->remove_from_parent();
250             $ldap->rmdir($this->dn);
252             new log("remove","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
254             if (!$ldap->success()){
255                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
256             }
257             $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
259             /* Delete references to object groups */
260             $ldap->cd ($this->config->current['BASE']);
261             $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
262             while ($ldap->fetch()){
263                 $og= new ogroup($this->config, $ldap->getDN());
264                 unset($og->member[$this->dn]);
265                 $og->save ();
266             }
267         }
268     }
271     /* Save data to object */
272     function save_object()
273     {
274         /* Create a base backup and reset the
275            base directly after calling plugin::save_object();
276            Base will be set seperatly a few lines below */
277         $base_tmp = $this->base;
278         plugin::save_object();
279         $this->base = $base_tmp;
281         $this->netConfigDNS->save_object();
283         if(isset($_POST['selected_category']) && $this->acl_is_writeable('category')){
284             $this->selected_category=get_post('selected_category');
285         }
287         /* Refresh base */
288         if ($this->acl_is_moveable($this->base)){
289             if (!$this->baseSelector->update()) {
290                 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
291             }
292             if ($this->base != $this->baseSelector->getBase()) {
293                 $this->base= $this->baseSelector->getBase();
294                 $this->is_modified= TRUE;
295             }
296         }
298     }
301     /* Check supplied data */
302     function check()
303     {
304         /* Call common method to give check the hook */
305         $message= plugin::check();
306         $message= array_merge($message, $this->netConfigDNS->check());
308         $this->dn= "cn=".$this->cn.",".get_ou("phoneGeneric", "phoneRDN").$this->base;
310         /* To check for valid ip*/
311         if($this->netConfigDNS->ipHostNumber == ""){
312             $message[]=  msgPool::required(_("IP address"));
313         } else {
314             if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
315                 $message[]= msgPool::invalid(_("IP address"));
316             }
317         }
319         // Check if a wrong base was supplied
320         if(!$this->baseSelector->checkLastBaseUpdate()){
321             $message[]= msgPool::check_base();;
322         }
324         /* Check if given name is a valid host/dns name */
325         if(!tests::is_dns_name($this->cn) ){
326             $message[] = msgPool::invalid(_("Name"));
327         }
329         if ($this->cn == ""){
330             $message[]= msgPool::required(_("Name"));
331         }
332         if ($this->cn == "0"){ 
333             $message[]= msgPool::reserved(_("Name"));
334         }
336         if ($this->orig_dn != $this->dn){
337             $ldap= $this->config->get_ldap_link();
338             $ldap->cd ($this->base);
339             $ldap->search ("(cn=".$this->cn.")", array("cn"));
340             if ($ldap->count() != 0){
341                 while ($attrs= $ldap->fetch()){
343                     if(preg_match("/cn=dhcp,/",$attrs['dn'])){
344                         continue;
345                     }
347                     if ($attrs['dn'] != $this->orig_dn){
348                         $message[]= msgPool::duplicated(_("Name"));
349                         break;
350                     }
351                 }
352             }
353         }
355         /* Check if we are allowed to create or move this object
356          */
357         if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
358             $message[] = msgPool::permCreate();
359         }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
360             $message[] = msgPool::permMove();
361         }
363         return ($message);
364     }
367     /* Save to LDAP */
368     function save()
369     {
370         plugin::save();
373         /* only to define which attrs to save*/ 
374         $mode = $this->selected_category;
376         /* Remove all unwanted attrs */
377         foreach($this->attributes as $att){
379             /* Check all attributes, if they are needed for this type of phone */
380             if(!in_array_strict($att,$this->usedattrs[$mode])){
381                 $this->attrs[$att] = array();
382             }
383         }
385         /* unset the category*/
386         unset($this->attrs['selected_category']);
388         /* Remove all empty values */
389         if ($this->orig_dn == 'new'){
390             $attrs= array();
391             foreach ($this->attrs as $key => $val){
392                 if (is_array($val) && count($val) == 0){
393                     continue;
394                 }
395                 $attrs[$key]= $val;
396             }
397             $this->attrs= $attrs;
398         }
400         if($this->goFonDefaultIP!="dynamic"){
401             $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
402         }    
404         $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
406         /* Write back to ldap */
407         $ldap= $this->config->get_ldap_link();
408         if ($this->orig_dn == 'new'){
409             $ldap->cd($this->config->current['BASE']);
410             $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
411             $ldap->cd($this->dn);
412             $ldap->add($this->attrs);
413             if (!$ldap->success()){
414                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
415             }
416             new log("create","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
417             $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
418         } else {
419             if ($this->orig_dn != $this->dn){
420                 $this->move($this->orig_dn, $this->dn);
421             }
423             $ldap->cd($this->dn);
424             $this->cleanup();
425             $ldap->modify ($this->attrs); 
426             new log("modify","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
427             if (!$ldap->success()){
428                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
429             }
431             // $user_phone_reload   
432             $ldap->cd ($this->config->current['BASE']); 
433             $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
434             if($user_phone_assignment){
435                 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
436                 $usertab->by_object['phoneAccount']->is_modified = true;
437                 $usertab->save();
438                 unset($usertab);
439             }
440             $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
441         }
442         $this->netConfigDNS->cn = $this->cn;    
443         $this->netConfigDNS->save();
444         if (!$ldap->success()){
445             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
446         }
447     }
450     /* Display generic part for server copy & paste */
451     function getCopyDialog()
452     {
453         $vars = array("cn");
454         $smarty = get_smarty();
455         $smarty->assign("cn" ,set_post($this->cn));
456         $smarty->assign("object","phone");
457         $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
458         $ret = array();
459         $ret['string'] = $str;
460         $ret['status'] = "";
461         return($ret);
462     }
465     function saveCopyDialog()
466     {
467         if(isset($_POST['cn'])){
468             $this->cn = get_post('cn');
469         }
470     }
473     function PrepareForCopyPaste($source)
474     {
475         plugin::PrepareForCopyPaste($source);
476         if(isset($source['macAddress'][0])){
477             $this->netConfigDNS->macAddress = $source['macAddress'][0];
478         }
479         if(isset($source['ipHostNumber'][0])){
480             $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
481         }
483         $source_o = new phoneGeneric($this->config, $source['dn']);
484         $this->selected_category = $source_o->selected_category;
485     }
488     /* Return plugin informations for acl handling
489 #FIXME FAIscript seams to ununsed within this class... */
490     static function plInfo()
491     {
492         return (array(
493                     "plShortName"   => _("Generic"),
494                     "plDescription" => _("Phone generic"),
495                     "plSelfModify"  => FALSE,
496                     "plDepends"     => array(),
497                     "plPriority"    => 0,
498                     "plSection"     => array("administration"),
499                     "plRequirements"=> array(
500                         'ldapSchema' => array('goFonHardware' => '>=2.7'),
501                         'onFailureDisablePlugin' => array(get_class())
502                         ),
503                     "plCategory"    => array("phone" => array("description"  => _("Phone hardware"),
504                             "objectClass"  => "goFonHardware")),
505                     "plProperties" =>
506                     array(
507                         array(
508                             "name"          => "phoneRDN",
509                             "type"          => "rdn",
510                             "default"       => "ou=phones,ou=systems,",
511                             "description"   => _("RDN for phone storage."),
512                             "check"         => "gosaProperty::isRdn",
513                             "migrate"       => "migrate_phoneRDN",
514                             "group"         => "plugin",
515                             "mandatory"     => FALSE
516                             )
517                         ),
519             "plProvidedAcls"=> array(
520                     "cn"                  => _("Name"),
521                     "base"                => _("Base"),
522                     "description"         => _("Description"),
523                     "goFonType"           => _("SIP Mode"),
524                     "goFonDmtfMode"       => _("SIP DTMF mode"),
525                     "goFonDefaultIP"      => _("SIP Default IP"),
526                     "goFonQualify"        => _("SIP Qualify"),
527                     "goFonAuth"           => _("IAX authentication type"),
528                     "goFonSecret"         => _("IAX secret"),
529                     "goFonAccountCode"    => _("IAX account code"),
530                     "goFonTrunk"          => _("IAX trunk lines"),
531                     "goFonPermit"         => _("IAX permit settings"),
532                     "goFonDeny"           => _("IAX deny settings"),
533                     "goFonMSN"            => _("CAPI MSN"),
534                     "category"  => _("Hardware type"))
535                 ));
536     }
541 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
542 ?>