Code

Systems Workstation Startup
[gosa.git] / plugins / admin / systems / 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 $goFonType        = "";
15   var $goFonDmtfMode    = "";
16   var $goFonHost        = "";
17   var $goFonDefaultIP   = "dynamic";
18   var $goFonQualify     = "";
19   var $goFonAuth        = "";
20   var $goFonSecret      = "";
21   var $goFonInkeys      = "";
22   var $goFonPermit      = array();
23   var $goFonDeny        = array();
24   var $goFonOutkey      = "";
25   var $goFonTrunk       = "";
26   var $goFonAccountCode = "";
27   var $goFonMSN         = "";
28   var $selected_categorie    = 0;
29   var $netConfigDNS;
31   /* attribute list for save action */
32   var $attributes= array("cn", "description", 
33                           "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
34                           "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
35                           "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie","goFonPermit","goFonDeny" 
36                          );
38   /* this array defines which attributes are schown / saved for the different type of phones */ 
39   var $usedattrs =    array( "0"=>array("cn", "description", 
40                                   "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
41                                   "goFonQualify"),
42                              "1"=>array("cn", "description", 
43                                   "goFonType","goFonHost","goFonDefaultIP",
44                                   "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
45                                   "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
46                              "2"=>array("cn", "description", "goFonMSN"));
47   
49   var $objectclasses= array("top", "goFonHardware");
51   function phonegeneric ($config, $dn= NULL, $parent= NULL)
52   {
53     plugin::plugin ($config, $dn, $parent);
54     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
55     $this->netConfigDNS->acl = $this->acl;
57     /* Set base */
58     if ($this->dn == "new"){
59       $ui= get_userinfo();
60       $this->base= dn2base($ui->dn);
61       $this->cn= "";
62     } else {
63       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
64     }
65   
66     if($this->goFonMSN != ""){
67       $this->selected_categorie = 2;
68     }elseif($this->goFonAccountCode != ""){
69       $this->selected_categorie = 1 ;
70     
71       if(isset($this->attrs['goFonPermit']['count'])){
72         unset ($this->attrs['goFonPermit']['count']);
73         $this->goFonPermit=$this->attrs['goFonPermit'];
74       }  
75      
76       if(isset($this->attrs['goFonDeny']['count'])){
77         unset ($this->attrs['goFonDeny']['count'])   ;
78         $this->goFonDeny=$this->attrs['goFonDeny'];
79       } 
81     } else {
82       $this->selected_categorie = 0;
83     }
85     if($this->goFonDefaultIP!="dynamic"){
86       $this->goFonDefaultIP = "network";
87     }
89     /* Save dn for later references */
90     $this->orig_dn= $this->dn;
91   }
93   function execute()
94   {
95     $this->netConfigDNS->acl = $this->acl;
97     /* Call parent execute */
98     plugin::execute();
100     /* Do we represent a valid phone? */
101     if (!$this->is_account && $this->parent == NULL){
102       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
103         _("This 'dn' has no phone features.")."</b>";
104       return($display);
105     }
107     /* Base select dialog */
108     $once = true;
109     foreach($_POST as $name => $value){
110       if(preg_match("/^chooseBase/",$name) && $once){
111         $once = false;
112         $this->dialog = new baseSelectDialog($this->config);
113         $this->dialog->setCurrentBase($this->base);
114       }
115     }
117     /* Dialog handling */
118     if(is_object($this->dialog)){
119       /* Must be called before save_object */
120       $this->dialog->save_object();
122       if($this->dialog->isClosed()){
123         $this->dialog = false;
124       }elseif($this->dialog->isSelected()){
125         $this->base = $this->dialog->isSelected();
126         $this->dialog= false;
127       }else{
128         return($this->dialog->execute());
129       }
130     }
132     /* handle Permit Add*/
133     if(isset($_POST['goFonPermitAdd'])){
134       if(isset($_POST['goFonPermitNew'])){
135         if(is_string($this->goFonPermit)){
136           $this->goFonPermit=array();
137         }
138         $new = $_POST['goFonPermitNew'];
139         if(strlen($new)> 1) {
140           $this->goFonPermit[]= $new;
141         }
142       }
143     }
144       
145     /* handle Deny Add*/
146     if(isset($_POST['goFonDenyAdd'])){
147       if(isset($_POST['goFonDenyNew'])){
148         if(is_string($this->goFonDeny)){
149           $this->goFonDeny=array();
150         }
151         $new = $_POST['goFonDenyNew'];
152         if(strlen($new)> 1) {
153           $this->goFonDeny[]= $new;
154         }
155       }
156     }
158     /* Handle Permit Deletion*/
159     if(isset($_POST['goFonPermitDel'])){
160       if(isset($_POST['goFonPermitS'])){
161         if(is_string($this->goFonPermit)){
162           $this->goFonPermit=array();
163         }
164         $new = $_POST['goFonPermitS'];
165         $tmp = array_flip($this->goFonPermit);
166         unset($tmp[$new]);
167         $this->goFonPermit=array();
168         foreach(array_flip($tmp) as $tm){
169           $this->goFonPermit[]=$tm;
170         }
171       }
172     }
175     /* Handle Permit Deletion*/
176     if(isset($_POST['goFonDenyDel'])){
177       if(isset($_POST['goFonDenyS'])){
178         if(is_string($this->goFonDeny)){
179           $this->goFonDeny=array();
180         }
181         $new = $_POST['goFonDenyS'];
182         $tmp = array_flip($this->goFonDeny);
183         unset($tmp[$new]);
184         $this->goFonDeny=array();
185         foreach(array_flip($tmp) as $tm){
186           $this->goFonDeny[]=$tm;
187         }
188       }
189     }
190   
191     /* Fill templating stuff */
192     $smarty= get_smarty();
193     $smarty->assign("bases", $this->config->idepartments);
194     $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
196     /* Create Arrays for samrty select boxes */
197     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
198     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
199     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
200     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
201     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
203     /* deativate all fields that are not used by the specified type */
204     foreach($this->attributes as $att){
205       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
206         $smarty->assign($att."USED", "disabled" );
207         $smarty->assign($att, "");
208       }else{
209         $smarty->assign($att."USED", "" );
210         $smarty->assign($att, $this->$att);
211       }
212     }
213     
214     $smarty->assign("selected_categorie",$this->selected_categorie);
216     /* Assign attributes */
217     foreach ($this->attributes as $attr){
218       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
219     }
220     $smarty->assign("base_select", $this->base);
221     $smarty->assign("baseACL", chkacl($this->acl,"base"));
222    
223     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
224  
225     /* Show main page */
226     $this->netConfigDNS->cn= $this->cn;
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     $this->netConfigDNS->acl = $this->acl;
235     $ldap= $this->config->get_ldap_link();
236     $ldap->cd($this->config->current['BASE']);
238     $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
239     while ($attr =  $ldap->fetch()){
240       print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
241         ($attr['uid'][0]." - ".$attr['cn'][0])));
242       return;
243     }
245     $this->netConfigDNS->remove_from_parent();
246     $ldap->rmdir($this->dn);
247     show_ldap_error($ldap->get_error(), _("Removing phone failed"));
248     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
250     /* Delete references to object groups */
251     $ldap->cd ($this->config->current['BASE']);
252     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
253     while ($ldap->fetch()){
254       $og= new ogroup($this->config, $ldap->getDN());
255       unset($og->member[$this->dn]);
256       $og->save ();
257     }
258   }
261   /* Save data to object */
262   function save_object()
263   {
264     plugin::save_object();
265     $this->netConfigDNS->save_object();
266     /* Save base, since this is no LDAP attribute */
267     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
268       $this->base= $_POST['base'];
269     }
270   }
273   /* Check supplied data */
274   function check()
275   {
276     /* Call common method to give check the hook */
277     $message= plugin::check();
278     $message= array_merge($message, $this->netConfigDNS->check());
280     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
282     /* To check for valid ip*/
283     if($this->netConfigDNS->ipHostNumber == ""){
284        $message[]= _("The required field IP address is empty.");
285     } else {
286       if (!is_ip($this->netConfigDNS->ipHostNumber)){
287         $message[]= _("The field IP address contains an invalid address.");
288       }
289     }
291     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
292       $message[]= _("The required field 'Phone name' is not set.");
293     }
294     if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
295       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
296     }
298     $ui= get_userinfo();
299     $acl= get_permissions ($this->dn, $ui->subtreeACL);
300     $acl= get_module_permission($acl, "phone", $this->dn);
301     if (chkacl($acl, "create") != ""){
302       $message[]= _("You have no permissions to create a phone on this 'Base'.");
303     }
305     if ($this->orig_dn != $this->dn){
306       $ldap= $this->config->get_ldap_link();
307       $ldap->cd ($this->base);
308       $ldap->search ("(cn=".$this->cn.")", array("cn"));
309       if ($ldap->count() != 0){
310         while ($attrs= $ldap->fetch()){
311           if ($attrs['dn'] != $this->orig_dn){
312             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
313             break;
314           }
315         }
316       }
317     }
319     return ($message);
320   }
323   /* Save to LDAP */
324   function save()
325   {
326     $this->netConfigDNS->acl = $this->acl;
327     plugin::save();
328    
329    
330     /* only to define which attrs to save*/ 
331     $mode = $this->attrs['selected_categorie'];
333     /* Remove all unwanted attrs */
334     foreach($this->attributes as $att){
335       
336       /* Check all attributes, if they are needed for this type of phone */
337       if(!in_array($att,$this->usedattrs[$mode])){
338         $this->attrs[$att] = array();
339       }
340     
341       /* Test rights of this user ... */
342       if(chkacl($this->acl,$att)!=""){
343         unset($this->attrs[$att]);
344       }
345     }
347     /* unset the categorie*/
348     unset($this->attrs['selected_categorie']);
349     
350     /* Remove all empty values */
351     if ($this->orig_dn == 'new'){
352       $attrs= array();
353       foreach ($this->attrs as $key => $val){
354         if (is_array($val) && count($val) == 0){
355           continue;
356         }
357         $attrs[$key]= $val;
358       }
359       $this->attrs= $attrs;
360     }
362     if($this->goFonDefaultIP!="dynamic"){
363       $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
364     }    
366     $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
368     /* Write back to ldap */
369     $ldap= $this->config->get_ldap_link();
370     if ($this->orig_dn == 'new'){
371       $ldap->cd($this->config->current['BASE']);
372       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
373       $ldap->cd($this->dn);
374       $ldap->add($this->attrs);
375       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
376     } else {
377       if ($this->orig_dn != $this->dn){
378         $this->move($this->orig_dn, $this->dn);
379       }
381       $ldap->cd($this->dn);
382       $this->cleanup();
383       $ldap->modify ($this->attrs); 
385       // $user_phone_reload   
386       $ldap->cd ($this->config->current['BASE']); 
387       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
388       if($user_phone_assignment){
389         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
390         $usertab->by_object['phoneAccount']->is_modified = true;
391         $usertab->save();
392         unset($usertab);
393       }
394       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
395     }
396     $this->netConfigDNS->cn = $this->cn;    
397     $this->netConfigDNS->save($this->dn);
398     show_ldap_error($ldap->get_error(), _("Saving phone failed"));
399   }
403 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
404 ?>