Code

Exlude dhcp entries from existence check in check()
[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;
35   var $view_logged      = FALSE;
37   /* attribute list for save action */
38   var $attributes= array("cn", "description",
39                           "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
40                           "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
41                           "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie","goFonPermit","goFonDeny" 
42                          );
44   /* this array defines which attributes are schown / saved for the different type of phones */ 
45   var $usedattrs =    array( "0"=>array("cn", "description", 
46                                   "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
47                                   "goFonQualify"),
48                              "1"=>array("cn", "description", 
49                                   "goFonType","goFonHost","goFonDefaultIP",
50                                   "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
51                                   "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
52                              "2"=>array("cn", "description", "goFonMSN"));
53   
55   var $objectclasses= array("top", "goFonHardware");
57   function phonegeneric ($config, $dn= NULL, $parent= NULL)
58   {
59     plugin::plugin ($config, $dn, $parent);
60     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
62     /* Set base */
63     if ($this->dn == "new"){
64       $ui= get_userinfo();
65       $this->base= dn2base($ui->dn);
66       $this->cn= "";
67     } else {
68       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
69     }
70   
71     if($this->goFonMSN != ""){
72       $this->selected_categorie = 2;
73     }elseif($this->goFonAccountCode != ""){
74       $this->selected_categorie = 1 ;
75     
76       if(isset($this->attrs['goFonPermit']['count'])){
77         unset ($this->attrs['goFonPermit']['count']);
78         $this->goFonPermit=$this->attrs['goFonPermit'];
79       }  
80      
81       if(isset($this->attrs['goFonDeny']['count'])){
82         unset ($this->attrs['goFonDeny']['count'])   ;
83         $this->goFonDeny=$this->attrs['goFonDeny'];
84       } 
86     } else {
87       $this->selected_categorie = 0;
88     }
90     if($this->goFonDefaultIP!="dynamic"){
91       $this->goFonDefaultIP = "network";
92     }
94     /* Save dn for later references */
95     $this->orig_dn= $this->dn;
96   }
98   function set_acl_base($base)
99   {
100     plugin::set_acl_base($base);
101     $this->netConfigDNS->set_acl_base($base);
102   }
104   function set_acl_category($cat)
105   {
106     plugin::set_acl_category($cat);
107     $this->netConfigDNS->set_acl_category($cat);
108   }
111   function execute()
112   {
113     /* Call parent execute */
114     plugin::execute();
116     if($this->is_account && !$this->view_logged){
117       $this->view_logged = TRUE;
118       new log("view","phone/".get_class($this),$this->dn);
119     }
121     /* Do we represent a valid phone? */
122     if (!$this->is_account && $this->parent == NULL){
123       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
124         _("This 'dn' has no phone features.")."</b>";
125       return($display);
126     }
128     /* Base select dialog */
129     $once = true;
130     foreach($_POST as $name => $value){
131       if(preg_match("/^chooseBase/",$name) && $once){
132         $once = false;
133         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
134         $this->dialog->setCurrentBase($this->base);
135       }
136     }
138     /* Dialog handling */
139     if(is_object($this->dialog)){
140       /* Must be called before save_object */
141       $this->dialog->save_object();
143       if($this->dialog->isClosed()){
144         $this->dialog = false;
145       }elseif($this->dialog->isSelected()){
146         
147         /* A new base was selected, check if it is a valid one */
148         $tmp = $this->get_allowed_bases();
149         if(isset($tmp[$this->dialog->isSelected()])){
150           $this->base = $this->dialog->isSelected();
151         }
153         $this->dialog= false;
154       }else{
155         return($this->dialog->execute());
156       }
157     }
159     /* handle Permit Add*/
160     if(isset($_POST['goFonPermitAdd']) && $this->acl_is_writeable("goFonPermit")){
161       if(isset($_POST['goFonPermitNew'])){
162         if(is_string($this->goFonPermit)){
163           $this->goFonPermit=array();
164         }
165         $new = $_POST['goFonPermitNew'];
166         if(strlen($new)> 1) {
167           $this->goFonPermit[]= $new;
168         }
169       }
170     }
171       
172     /* handle Deny Add*/
173     if(isset($_POST['goFonDenyAdd']) && $this->acl_is_writeable("goFonDeny")){
174       if(isset($_POST['goFonDenyNew'])){
175         if(is_string($this->goFonDeny)){
176           $this->goFonDeny=array();
177         }
178         $new = $_POST['goFonDenyNew'];
179         if(strlen($new)> 1) {
180           $this->goFonDeny[]= $new;
181         }
182       }
183     }
185     /* Handle Permit Deletion*/
186     if(isset($_POST['goFonPermitDel']) && $this->acl_is_writeable("goFonPermit")){
187       if(isset($_POST['goFonPermitS'])){
188         if(is_string($this->goFonPermit)){
189           $this->goFonPermit=array();
190         }
191         $new = $_POST['goFonPermitS'];
192         $tmp = array_flip($this->goFonPermit);
193         unset($tmp[$new]);
194         $this->goFonPermit=array();
195         foreach(array_flip($tmp) as $tm){
196           $this->goFonPermit[]=$tm;
197         }
198       }
199     }
202     /* Handle Permit Deletion*/
203     if(isset($_POST['goFonDenyDel']) && $this->acl_is_writeable("goFonDeny")){
204       if(isset($_POST['goFonDenyS'])){
205         if(is_string($this->goFonDeny)){
206           $this->goFonDeny=array();
207         }
208         $new = $_POST['goFonDenyS'];
209         $tmp = array_flip($this->goFonDeny);
210         unset($tmp[$new]);
211         $this->goFonDeny=array();
212         foreach(array_flip($tmp) as $tm){
213           $this->goFonDeny[]=$tm;
214         }
215       }
216     }
217   
218     /* Fill templating stuff */
219     $smarty= get_smarty();
221     $smarty->assign("bases", $this->get_allowed_bases());
223     $tmp = $this->plInfo();
224     foreach($tmp['plProvidedAcls'] as $name => $translation){
225       $smarty->assign($name."ACL",$this->getacl($name));
226     }
228     $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
230     /* Create Arrays for samrty select boxes */
231     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
232     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
233     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
234     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
235     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
237     /* deativate all fields that are not used by the specified type */
238     foreach($this->attributes as $att){
239       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))){
240         $smarty->assign($att."USED", "disabled" );
241         $smarty->assign($att, "");
242       }else{
243         $smarty->assign($att."USED", "" );
244         $smarty->assign($att, $this->$att);
245       }
246     }
247     
248     $smarty->assign("selected_categorie",$this->selected_categorie);
250     /* Assign attributes */
251     $smarty->assign("base_select", $this->base);
252     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
253  
254     /* Show main page */
255     $smarty->assign("netconfig", $this->netConfigDNS->execute());
256     $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
257     return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
258   }
260   function remove_from_parent()
261   {
262     if($this->acl_is_removeable()){
263       $ldap= $this->config->get_ldap_link();
264       $ldap->cd($this->config->current['BASE']);
266       $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
267       while ($attr =  $ldap->fetch()){
268         print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
269               ($attr['uid'][0]." - ".$attr['cn'][0])));
270         return;
271       }
273       $this->netConfigDNS->remove_from_parent();
274       $ldap->rmdir($this->dn);
275   
276       new log("remove","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
278       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system phone/generic with dn '%s' failed."),$this->dn));
279       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
281       /* Delete references to object groups */
282       $ldap->cd ($this->config->current['BASE']);
283       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
284       while ($ldap->fetch()){
285         $og= new ogroup($this->config, $ldap->getDN());
286         unset($og->member[$this->dn]);
287         $og->save ();
288       }
289     }
290   }
293   /* Save data to object */
294   function save_object()
295   {
296     /* Create a base backup and reset the
297        base directly after calling plugin::save_object();
298        Base will be set seperatly a few lines below */
299     $base_tmp = $this->base;
300     plugin::save_object();
301     $this->base = $base_tmp;
303     $this->netConfigDNS->save_object();
305     /* Set new base if allowed */
306     $tmp = $this->get_allowed_bases();
307     if(isset($_POST['base'])){
308       if(isset($tmp[$_POST['base']])){
309         $this->base= $_POST['base'];
310       }
311     }
312   }
315   /* Check supplied data */
316   function check()
317   {
318     /* Call common method to give check the hook */
319     $message= plugin::check();
320     $message= array_merge($message, $this->netConfigDNS->check());
322     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
324     /* To check for valid ip*/
325     if($this->netConfigDNS->ipHostNumber == ""){
326        $message[]= _("The required field IP address is empty.");
327     } else {
328       if (!is_ip($this->netConfigDNS->ipHostNumber)){
329         $message[]= _("The field IP address contains an invalid address.");
330       }
331     }
333     if ($this->cn == ""){
334       $message[]= _("The required field 'Phone name' is not set.");
335     }
336     if ($this->cn == "0"){ 
337       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
338     }
340     if ($this->orig_dn != $this->dn){
341       $ldap= $this->config->get_ldap_link();
342       $ldap->cd ($this->base);
343       $ldap->search ("(cn=".$this->cn.")", array("cn"));
344       if ($ldap->count() != 0){
345         while ($attrs= $ldap->fetch()){
347           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
348             continue;
349           }
350   
351           if ($attrs['dn'] != $this->orig_dn){
352             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
353             break;
354           }
355         }
356       }
357     }
359     return ($message);
360   }
363   /* Save to LDAP */
364   function save()
365   {
366     plugin::save();
367    
368    
369     /* only to define which attrs to save*/ 
370     $mode = $this->selected_categorie;
372     /* Remove all unwanted attrs */
373     foreach($this->attributes as $att){
374       
375       /* Check all attributes, if they are needed for this type of phone */
376       if(!in_array($att,$this->usedattrs[$mode])){
377         $this->attrs[$att] = array();
378       }
379     }
381     /* unset the categorie*/
382     unset($this->attrs['selected_categorie']);
383     
384     /* Remove all empty values */
385     if ($this->orig_dn == 'new'){
386       $attrs= array();
387       foreach ($this->attrs as $key => $val){
388         if (is_array($val) && count($val) == 0){
389           continue;
390         }
391         $attrs[$key]= $val;
392       }
393       $this->attrs= $attrs;
394     }
396     if($this->goFonDefaultIP!="dynamic"){
397       $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
398     }    
400     $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
402     /* Write back to ldap */
403     $ldap= $this->config->get_ldap_link();
404     if ($this->orig_dn == 'new'){
405       $ldap->cd($this->config->current['BASE']);
406       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
407       $ldap->cd($this->dn);
408       $ldap->add($this->attrs);
409       new log("create","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
410       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
411     } else {
412       if ($this->orig_dn != $this->dn){
413         $this->move($this->orig_dn, $this->dn);
414       }
416       $ldap->cd($this->dn);
417       $this->cleanup();
418       $ldap->modify ($this->attrs); 
419       new log("modify","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
421       // $user_phone_reload   
422       $ldap->cd ($this->config->current['BASE']); 
423       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
424       if($user_phone_assignment){
425         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
426         $usertab->by_object['phoneAccount']->is_modified = true;
427         $usertab->save();
428         unset($usertab);
429       }
430       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
431     }
432     $this->netConfigDNS->cn = $this->cn;    
433     $this->netConfigDNS->save($this->dn);
434     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system phone/generic with dn '%s' failed."),$this->dn));
435   }
438   /* Display generic part for server copy & paste */
439   function getCopyDialog()
440   {
441     $vars = array("cn");
442     $smarty = get_smarty();
443     $smarty->assign("cn" ,$this->cn);
444     $smarty->assign("object","phone");
445     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
446     $ret = array();
447     $ret['string'] = $str;
448     $ret['status'] = "";
449     return($ret);
450   }
453   function saveCopyDialog()
454   {
455     if(isset($_POST['cn'])){
456       $this->cn = $_POST['cn'];
457     }
458   }
461   function PrepareForCopyPaste($source)
462   {
463     plugin::PrepareForCopyPaste($source);
464     if(isset($source['macAddress'][0])){
465       $this->netConfigDNS->macAddress = $source['macAddress'][0];
466     }
467     if(isset($source['ipHostNumber'][0])){
468       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
469     }
471     $source_o = new phonegeneric($this->config, $source['dn']);
472     $this->selected_categorie = $source_o->selected_categorie;
473   }
476   /* Return plugin informations for acl handling
477       #FIXME FAIscript seams to ununsed within this class... */
478   function plInfo()
479   {
480     return (array(
481           "plShortName"   => _("Generic"),
482           "plDescription" => _("Phone generic"),
483           "plSelfModify"  => FALSE,
484           "plDepends"     => array(),
485           "plPriority"    => 0,
486           "plSection"     => array("administration"),
487           "plCategory"    => array("phone" => array("description"  => _("Phone hardware"),
488                                                     "objectClass"  => "goFonHardware")),
489           "plProvidedAcls"=> array(
490             "cn"                  => _("Name"),
491             "base"                => _("Base"),
492             "description"         => _("Description"),
493             "goFonType"           => _("SIP Mode"),
494             "goFonDmtfMode"       => _("SIP DTMF mode"),
495             "goFonDefaultIP"      => _("SIP Default ip"),
496             "goFonQualify"        => _("SIP Qualify"),
497             "goFonAuth"           => _("IAX authentication type"),
498             "goFonSecret"         => _("IAX secret"),
499             "goFonAccountCode"    => _("IAX account code"),
500             "goFonTrunk"          => _("IAX trunk lines"),
501             "goFonPermit"         => _("IAX permit settings"),
502             "goFonDeny"           => _("IAX deny settings"),
503             "goFonMSN"            => _("CAPI MSN"),
504             "categorie"  => _("Hardware type"))
505           ));
506   }
511 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
512 ?>