Code

Added support for general check hooks
[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)
57   {
58     plugin::plugin ($config, $dn);
59     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
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 execute()
98   {
99         /* Call parent execute */
100         plugin::execute();
102     /* Do we represent a valid phone? */
103     if (!$this->is_account && $this->parent == NULL){
104       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
105         _("This 'dn' has no phone features.")."</b>";
106       return($display);
107     }
109     /* Base select dialog */
110     $once = true;
111     foreach($_POST as $name => $value){
112       if(preg_match("/^chooseBase/",$name) && $once){
113         $once = false;
114         $this->dialog = new baseSelectDialog($this->config);
115         $this->dialog->setCurrentBase($this->base);
116       }
117     }
119     /* Dialog handling */
120     if(is_object($this->dialog)){
121       /* Must be called before save_object */
122       $this->dialog->save_object();
124       if($this->dialog->isClosed()){
125         $this->dialog = false;
126       }elseif($this->dialog->isSelected()){
127         $this->base = $this->dialog->isSelected();
128         $this->dialog= false;
129       }else{
130         return($this->dialog->execute());
131       }
132     }
134     /* handle Permit Add*/
135     if(isset($_POST['goFonPermitAdd'])){
136       if(isset($_POST['goFonPermitNew'])){
137         if(is_string($this->goFonPermit)){
138           $this->goFonPermit=array();
139         }
140         $new = $_POST['goFonPermitNew'];
141         if(strlen($new)> 1) {
142           $this->goFonPermit[]= $new;
143         }
144       }
145     }
146       
147     /* handle Deny Add*/
148     if(isset($_POST['goFonDenyAdd'])){
149       if(isset($_POST['goFonDenyNew'])){
150         if(is_string($this->goFonDeny)){
151           $this->goFonDeny=array();
152         }
153         $new = $_POST['goFonDenyNew'];
154         if(strlen($new)> 1) {
155           $this->goFonDeny[]= $new;
156         }
157       }
158     }
160     /* Handle Permit Deletion*/
161     if(isset($_POST['goFonPermitDel'])){
162       if(isset($_POST['goFonPermitS'])){
163         if(is_string($this->goFonPermit)){
164           $this->goFonPermit=array();
165         }
166         $new = $_POST['goFonPermitS'];
167         $tmp = array_flip($this->goFonPermit);
168         unset($tmp[$new]);
169         $this->goFonPermit=array();
170         foreach(array_flip($tmp) as $tm){
171           $this->goFonPermit[]=$tm;
172         }
173       }
174     }
177     /* Handle Permit Deletion*/
178     if(isset($_POST['goFonDenyDel'])){
179       if(isset($_POST['goFonDenyS'])){
180         if(is_string($this->goFonDeny)){
181           $this->goFonDeny=array();
182         }
183         $new = $_POST['goFonDenyS'];
184         $tmp = array_flip($this->goFonDeny);
185         unset($tmp[$new]);
186         $this->goFonDeny=array();
187         foreach(array_flip($tmp) as $tm){
188           $this->goFonDeny[]=$tm;
189         }
190       }
191     }
192   
193     /* Fill templating stuff */
194     $smarty= get_smarty();
195     $smarty->assign("bases", $this->config->idepartments);
196     $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
198     /* Create Arrays for samrty select boxes */
199     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
200     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
201     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
202     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
203     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
205     /* deativate all fields that are not used by the specified type */
206     foreach($this->attributes as $att){
207       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
208         $smarty->assign($att."USED", "disabled" );
209         $smarty->assign($att, "");
210       }else{
211         $smarty->assign($att."USED", "" );
212         $smarty->assign($att, $this->$att);
213       }
214     }
215     
216     $smarty->assign("selected_categorie",$this->selected_categorie);
218     /* Assign attributes */
219     foreach ($this->attributes as $attr){
220       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
221     }
222     $smarty->assign("base_select", $this->base);
223    
224     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
225  
226     /* Show main page */
227     $smarty->assign("netconfig", $this->netConfigDNS->execute());
228     $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
229     return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
230   }
232   function remove_from_parent()
233   {
234     $ldap= $this->config->get_ldap_link();
235     $ldap->cd($this->config->current['BASE']);
237     $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
238     while ($attr =  $ldap->fetch()){
239       print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
240         ($attr['uid'][0]." - ".$attr['cn'][0])));
241       return;
242     }
244     $this->netConfigDNS->remove_from_parent();
245     $ldap->rmdir($this->dn);
246     show_ldap_error($ldap->get_error());
247     $this->handle_post_events("remove");
249     /* Delete references to object groups */
250     $ldap->cd ($this->config->current['BASE']);
251     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
252     while ($ldap->fetch()){
253       $og= new ogroup($this->config, $ldap->getDN());
254       unset($og->member[$this->dn]);
255       $og->save ();
256     }
257   }
260   /* Save data to object */
261   function save_object()
262   {
263     plugin::save_object();
264     $this->netConfigDNS->save_object();
265     /* Save base, since this is no LDAP attribute */
266     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
267       $this->base= $_POST['base'];
268     }
269   }
272   /* Check supplied data */
273   function check()
274   {
275     /* Call common method to give check the hook */
276     $message= plugin::check();
277     $message= array_merge($message, $this->netConfigDNS->check());
279     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
281     /* To check for valid ip*/
282     $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
284 //    if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
285 //      if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
286 //       $message[]= _("Wrong IP format in field goFonDefaultIP.");
287 //      }
288 //    }
289     
290 //    /* Check for valid number */
291 //    if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
292 //      if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
293 //        $message[]= _("The given value for 'Response timeout' is not a valid number.");
294 //      } 
295 //    }
297     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
298       $message[]= _("The required field 'Phone name' is not set.");
299     }
300     if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
301       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
302     }
304     $ui= get_userinfo();
305     $acl= get_permissions ($this->dn, $ui->subtreeACL);
306     $acl= get_module_permission($acl, "phone", $this->dn);
307     if (chkacl($acl, "create") != ""){
308       $message[]= _("You have no permissions to create a phone on this 'Base'.");
309     }
311     if ($this->orig_dn != $this->dn){
312       $ldap= $this->config->get_ldap_link();
313       $ldap->cd ($this->base);
314       $ldap->search ("(cn=".$this->cn.")", array("cn"));
315       if ($ldap->count() != 0){
316         while ($attrs= $ldap->fetch()){
317           if ($attrs['dn'] != $this->orig_dn){
318             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
319             break;
320           }
321         }
322       }
323     }
325     return ($message);
326   }
329   /* Save to LDAP */
330   function save()
331   {
332     plugin::save();
333    
334    
335     /* only to define which attrs to save*/ 
336     $mode = $this->attrs['selected_categorie'];
338     /* Remove all unwanted attrs */
339     foreach($this->attributes as $att){
340       
341       /* Check all attributes, if they are needed for this type of phone */
342       if(!in_array($att,$this->usedattrs[$mode])){
343         $this->attrs[$att] = array();
344       }
345     
346       /* Test rights of this user ... */
347       if(chkacl($this->acl,$att)!=""){
348         unset($this->attrs[$att]);
349       }
350     }
352     /* unset the categorie*/
353     unset($this->attrs['selected_categorie']);
354     
355     /* Remove all empty values */
356     if ($this->orig_dn == 'new'){
357       $attrs= array();
358       foreach ($this->attrs as $key => $val){
359         if (is_array($val) && count($val) == 0){
360           continue;
361         }
362         $attrs[$key]= $val;
363       }
364       $this->attrs= $attrs;
365     }
367     if($this->goFonDefaultIP!="dynamic"){
368       $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
369     }    
371     $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
373     /* Write back to ldap */
374     $ldap= $this->config->get_ldap_link();
375     if ($this->orig_dn == 'new'){
376       $ldap->cd($this->config->current['BASE']);
377       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
378       $ldap->cd($this->dn);
379       $ldap->add($this->attrs);
380       $this->handle_post_events("add");
381     } else {
382       if ($this->orig_dn != $this->dn){
383         $this->move($this->orig_dn, $this->dn);
384       }
386       $ldap->cd($this->dn);
387       $this->cleanup();
388 $ldap->modify ($this->attrs); 
390       // $user_phone_reload   
391       $ldap->cd ($this->config->current['BASE']); 
392       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
393       if($user_phone_assignment){
394         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
395         $usertab->by_object['phoneAccount']->is_modified = true;
396         $usertab->save();
397         unset($usertab);
398       }
399       $this->handle_post_events("modify");
400     }
401     $this->netConfigDNS->cn = $this->cn;    
402     $this->netConfigDNS->save($this->dn);
404     show_ldap_error($ldap->get_error());
405     /* Optionally execute a command after we're done */
406     $this->postcreate();
407   }
411 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
412 ?>