Code

d0ecb5bdd45e52f96c03cd284a89fc758d2b9d9b
[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 $macAddress       = "";
18   var $ipHostNumber     = "";
19   var $description      = "";
20   var $orig_dn          = "";
21   var $goFonType        = "";
22   var $goFonDmtfMode    = "";
23   var $goFonHost        = "";
24   var $goFonDefaultIP   = "";
25   var $goFonQualify     = "";
26   var $goFonAuth        = "";
27   var $goFonSecret      = "";
28   var $goFonInkeys      = "";
29   var $goFonOutkey      = "";
30   var $goFonTrunk       = "";
31   var $goFonAccountCode = "";
32   var $goFonMSN         = "";
33   var $selected_categorie    = 0;
35   /* attribute list for save action */
36   var $attributes= array("cn", "description", "macAddress", "ipHostNumber"
37                          ,"goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
38                           "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
39                           "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie" 
40                          );
42   /* this array defines which attributes are schown / saved for the different type of phones */ 
43   var $usedattrs =    array( "0"=>array("cn", "description", "macAddress", "ipHostNumber",
44                                   "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
45                                   "goFonQualify"),
46                              "1"=>array("cn", "description", "macAddress", "ipHostNumber",
47                                   "goFonType","goFonHost","goFonDefaultIP",
48                                   "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
49                                   "goFonTrunk","goFonAccountCode","selected_categorie"),
50                              "2"=>array("cn", "description", "macAddress", "ipHostNumber","goFonMSN"));
51   
53   var $objectclasses= array("top", "goFonHardware");
55   function phonegeneric ($config, $dn= NULL)
56   {
57     plugin::plugin ($config, $dn);
59     /* Set base */
60     if ($this->dn == "new"){
61       $ui= get_userinfo();
62       $this->base= dn2base($ui->dn);
63       $this->cn= "";
64     } else {
65       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
66     }
67   
68     if($this->goFonMSN != ""){
69       $this->selected_categorie = 2;
70     }elseif($this->goFonAccountCode != ""){
71       $this->selected_categorie = 1 ;
72     } else {
73       $this->selected_categorie = 0;
74     }
75     
76     
78     /* Save dn for later references */
79     $this->orig_dn= $this->dn;
80   }
82   function execute()
83   {
84   
85   
86     /* Do we represent a valid phone? */
87     if (!$this->is_account && $this->parent == NULL){
88       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
89         _("This 'dn' has no phone features.")."</b>";
90       return($display);
91     }
93     /* Fill templating stuff */
94     $smarty= get_smarty();
95     $smarty->assign("bases", $this->config->idepartments);
97     /* Create Arrays for samrty select boxes */
98     $smarty->assign("categories",     array("SIP - phones","IAX - phones","CAPI - phones"));
99     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
100     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
101     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     ,"rsa"    =>"rsa"));
102     $smarty->assign("goFonTrunks",    array("yes" =>"yes"             ,"no"     =>"no"));
104     /* deativate all fields that are not used by the specified type */
105     foreach($this->attributes as $att){
106       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
107         $smarty->assign($att."USED", "disabled" );
108         $smarty->assign($att, "");
109       }else{
110         $smarty->assign($att."USED", "" );
111         $smarty->assign($att, $this->$att);
112       }
113     }
114     
115     $smarty->assign("selected_categorie",$this->selected_categorie);
117     /* Assign attributes */
118     foreach ($this->attributes as $attr){
119       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
120     }
121     $smarty->assign("base_select", $this->base);
122     
123     /* Show Asterisk for required attribute ipHostNumber and macAddress */
124     $smarty->assign("staticAddress", "<font class=\"must\">*</font>");
125         
126     /* Show main page */
127     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
128     $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
129     return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
130   }
132   function remove_from_parent()
133   {
134     $ldap= $this->config->get_ldap_link();
135     $ldap->rmdir($this->dn);
136     show_ldap_error($ldap->get_error());
137     $this->handle_post_events("remove");
139     /* Delete references to object groups */
140     $ldap->cd ($this->config->current['BASE']);
141     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
142     while ($ldap->fetch()){
143       $og= new ogroup($this->config, $ldap->getDN());
144       unset($og->member[$this->dn]);
145       $og->save ();
146     }
148   }
151   /* Save data to object */
152   function save_object()
153   {
154     plugin::save_object();
156     /* Save base, since this is no LDAP attribute */
157     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
158       $this->base= $_POST['base'];
159     }
160   }
163   /* Check supplied data */
164   function check()
165   {
166     $message= array();
167     $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
169     /* To check for valid ip*/
170     $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
171     if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
172        $message[]= _("Wrong IP format in field goFonDefaultIP.");
173     }
174     if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
175       $message[]= _("The given value for goFonQualify is not a valid number.");
176     }
178     /* must: cn, macAddress, ipHostNumber */
179     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
180       $message[]= _("The required field 'Phone name' is not set.");
181     }
182     if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
183       $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
184     }
185     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
186       $message[]= _("The required field 'MAC-address' is not set.");
187     }
188     if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
189       $message[]= _("The required field 'IP-address' is not set.");
190     }
191     if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
192        $message[]= _("Wrong IP format in field IP-address.");
193     }
195     $ui= get_userinfo();
196     $acl= get_permissions ($this->dn, $ui->subtreeACL);
197     $acl= get_module_permission($acl, "phone", $this->dn);
198     if (chkacl($acl, "create") != ""){
199       $message[]= _("You have no permissions to create a phone on this 'Base'.");
200     }
202     if ($this->orig_dn != $this->dn){
203       $ldap= $this->config->get_ldap_link();
204       $ldap->cd ($this->base);
205       $ldap->search ("(cn=".$this->cn.")", array("cn"));
206       if ($ldap->count() != 0){
207         while ($attrs= $ldap->fetch()){
208           if ($attrs['dn'] != $this->orig_dn){
209             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
210             break;
211           }
212         }
213       }
214     }
216     return ($message);
217   }
220   /* Save to LDAP */
221   function save()
222   {
223     plugin::save();
224    
225    
226     /* only to define which attrs to save*/ 
227     $mode = $this->attrs['selected_categorie'];
229     /* Remove all unwanted attrs */
230     foreach($this->attributes as $att){
231       
232       /* Check all attributes, if they are needed for this type of phone */
233       if(!in_array($att,$this->usedattrs[$mode])){
234         $this->attrs[$att] = array();
235       }
236     
237       /* Test rights of this user ... */
238       if(chkacl($this->acl,$att)!=""){
239         unset($this->attrs[$att]);
240       }
241     }
243     /* unset the categorie*/
244     unset($this->attrs['selected_categorie']);
245     
246     /* Remove all empty values */
247     if ($this->orig_dn == 'new'){
248       $attrs= array();
249       foreach ($this->attrs as $key => $val){
250         if (is_array($val) && count($val) == 0){
251           continue;
252         }
253         $attrs[$key]= $val;
254       }
255       $this->attrs= $attrs;
256     }
258     /* Write back to ldap */
259     $ldap= $this->config->get_ldap_link();
260     if ($this->orig_dn == 'new'){
261       $ldap->cd($this->config->current['BASE']);
262       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
263       $ldap->cd($this->dn);
264       $ldap->add($this->attrs);
265       $this->handle_post_events("add");
266     } else {
267       if ($this->orig_dn != $this->dn){
268         $this->move($this->orig_dn, $this->dn);
269       }
271       $ldap->cd($this->dn);
272       $ldap->modify($this->attrs);
273       $this->handle_post_events("modify");
274     }
275     show_ldap_error($ldap->get_error());
277     /* Optionally execute a command after we're done */
278     $this->postcreate();
279   }
283 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
284 ?>