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 $goFonPermit = array();
30 var $goFonDeny = array();
31 var $goFonOutkey = "";
32 var $goFonTrunk = "";
33 var $goFonAccountCode = "";
34 var $goFonMSN = "";
35 var $selected_categorie = 0;
37 /* attribute list for save action */
38 var $attributes= array("cn", "description", "macAddress", "ipHostNumber"
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", "macAddress", "ipHostNumber",
46 "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
47 "goFonQualify"),
48 "1"=>array("cn", "description", "macAddress", "ipHostNumber",
49 "goFonType","goFonHost","goFonDefaultIP",
50 "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
51 "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
52 "2"=>array("cn", "description", "macAddress", "ipHostNumber","goFonMSN"));
55 var $objectclasses= array("top", "goFonHardware");
57 function phonegeneric ($config, $dn= NULL)
58 {
59 plugin::plugin ($config, $dn);
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 }
70 if($this->goFonMSN != ""){
71 $this->selected_categorie = 2;
72 }elseif($this->goFonAccountCode != ""){
73 $this->selected_categorie = 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_categorie = 0;
87 }
89 /* Save dn for later references */
90 $this->orig_dn= $this->dn;
91 }
93 function execute()
94 {
97 /* Do we represent a valid phone? */
98 if (!$this->is_account && $this->parent == NULL){
99 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
100 _("This 'dn' has no phone features.")."</b>";
101 return($display);
102 }
104 /* handle Permit Add*/
105 if(isset($_POST['goFonPermitAdd'])){
106 if(isset($_POST['goFonPermitNew'])){
107 if(is_string($this->goFonPermit)){
108 $this->goFonPermit=array();
109 }
110 $new = $_POST['goFonPermitNew'];
111 if(strlen($new)> 1) {
112 $this->goFonPermit[]= $new;
113 }
114 }
115 }
117 /* handle Deny Add*/
118 if(isset($_POST['goFonDenyAdd'])){
119 if(isset($_POST['goFonDenyNew'])){
120 if(is_string($this->goFonDeny)){
121 $this->goFonDeny=array();
122 }
123 $new = $_POST['goFonDenyNew'];
124 if(strlen($new)> 1) {
125 $this->goFonDeny[]= $new;
126 }
127 }
128 }
130 /* Handle Permit Deletion*/
131 if(isset($_POST['goFonPermitDel'])){
132 if(isset($_POST['goFonPermitS'])){
133 if(is_string($this->goFonPermit)){
134 $this->goFonPermit=array();
135 }
136 $new = $_POST['goFonPermitS'];
137 $tmp = array_flip($this->goFonPermit);
138 unset($tmp[$new]);
139 $this->goFonPermit=array();
140 foreach(array_flip($tmp) as $tm){
141 $this->goFonPermit[]=$tm;
142 }
143 }
144 }
147 /* Handle Permit Deletion*/
148 if(isset($_POST['goFonDenyDel'])){
149 if(isset($_POST['goFonDenyS'])){
150 if(is_string($this->goFonDeny)){
151 $this->goFonDeny=array();
152 }
153 $new = $_POST['goFonDenyS'];
154 $tmp = array_flip($this->goFonDeny);
155 unset($tmp[$new]);
156 $this->goFonDeny=array();
157 foreach(array_flip($tmp) as $tm){
158 $this->goFonDeny[]=$tm;
159 }
160 }
161 }
163 /* Fill templating stuff */
164 $smarty= get_smarty();
165 $smarty->assign("bases", $this->config->idepartments);
167 /* Create Arrays for samrty select boxes */
168 $smarty->assign("categories", array("SIP","IAX","CAPI"));
169 $smarty->assign("goFonTypes", array("peer" =>"peer" ,"user" =>"user" ,"friend" =>"friend"));
170 $smarty->assign("goFonDmtfModes", array("inband" =>"inband" ,"rfc2833"=>"rfc2833" ,"info" =>"info"));
171 $smarty->assign("goFonAuths", array("plaintext" =>"plaintext" ,"md5" =>"md5" /*,"rsa" =>"rsa"*/));
172 $smarty->assign("goFonTrunks", array("yes" =>_("yes") ,"no" => _("no")));
174 /* deativate all fields that are not used by the specified type */
175 foreach($this->attributes as $att){
176 if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
177 $smarty->assign($att."USED", "disabled" );
178 $smarty->assign($att, "");
179 }else{
180 $smarty->assign($att."USED", "" );
181 $smarty->assign($att, $this->$att);
182 }
183 }
185 $smarty->assign("selected_categorie",$this->selected_categorie);
187 /* Assign attributes */
188 foreach ($this->attributes as $attr){
189 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
190 }
191 $smarty->assign("base_select", $this->base);
193 $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
195 /* Show Asterisk for required attribute ipHostNumber and macAddress */
196 $smarty->assign("staticAddress", "<font class=\"must\">*</font>");
198 /* Show main page */
199 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
200 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
201 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
202 }
204 function remove_from_parent()
205 {
206 $ldap= $this->config->get_ldap_link();
207 $ldap->rmdir($this->dn);
208 show_ldap_error($ldap->get_error());
209 $this->handle_post_events("remove");
211 /* Delete references to object groups */
212 $ldap->cd ($this->config->current['BASE']);
213 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
214 while ($ldap->fetch()){
215 $og= new ogroup($this->config, $ldap->getDN());
216 unset($og->member[$this->dn]);
217 $og->save ();
218 }
220 }
223 /* Save data to object */
224 function save_object()
225 {
226 plugin::save_object();
228 /* Save base, since this is no LDAP attribute */
229 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
230 $this->base= $_POST['base'];
231 }
232 }
235 /* Check supplied data */
236 function check()
237 {
238 $message= array();
239 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
241 /* To check for valid ip*/
242 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
244 // if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
245 // if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
246 // $message[]= _("Wrong IP format in field goFonDefaultIP.");
247 // }
248 // }
250 // /* Check for valid number */
251 // if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
252 // if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
253 // $message[]= _("The given value for 'Response timeout' is not a valid number.");
254 // }
255 // }
257 /* must: cn, macAddress, ipHostNumber */
258 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
259 $message[]= _("The required field 'Phone name' is not set.");
260 }
261 if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
262 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
263 }
264 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
265 $message[]= _("The required field 'MAC-address' is not set.");
266 }
267 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
268 $message[]= _("The required field 'IP-address' is not set.");
269 }
270 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
271 $message[]= _("Wrong IP format in field IP-address.");
272 }
274 $ui= get_userinfo();
275 $acl= get_permissions ($this->dn, $ui->subtreeACL);
276 $acl= get_module_permission($acl, "phone", $this->dn);
277 if (chkacl($acl, "create") != ""){
278 $message[]= _("You have no permissions to create a phone on this 'Base'.");
279 }
281 if ($this->orig_dn != $this->dn){
282 $ldap= $this->config->get_ldap_link();
283 $ldap->cd ($this->base);
284 $ldap->search ("(cn=".$this->cn.")", array("cn"));
285 if ($ldap->count() != 0){
286 while ($attrs= $ldap->fetch()){
287 if ($attrs['dn'] != $this->orig_dn){
288 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
289 break;
290 }
291 }
292 }
293 }
295 return ($message);
296 }
299 /* Save to LDAP */
300 function save()
301 {
302 plugin::save();
305 /* only to define which attrs to save*/
306 $mode = $this->attrs['selected_categorie'];
308 /* Remove all unwanted attrs */
309 foreach($this->attributes as $att){
311 /* Check all attributes, if they are needed for this type of phone */
312 if(!in_array($att,$this->usedattrs[$mode])){
313 $this->attrs[$att] = array();
314 }
316 /* Test rights of this user ... */
317 if(chkacl($this->acl,$att)!=""){
318 unset($this->attrs[$att]);
319 }
320 }
322 /* unset the categorie*/
323 unset($this->attrs['selected_categorie']);
325 /* Remove all empty values */
326 if ($this->orig_dn == 'new'){
327 $attrs= array();
328 foreach ($this->attrs as $key => $val){
329 if (is_array($val) && count($val) == 0){
330 continue;
331 }
332 $attrs[$key]= $val;
333 }
334 $this->attrs= $attrs;
335 }
337 /* Write back to ldap */
338 $ldap= $this->config->get_ldap_link();
339 if ($this->orig_dn == 'new'){
340 $ldap->cd($this->config->current['BASE']);
341 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
342 $ldap->cd($this->dn);
343 $ldap->add($this->attrs);
344 $this->handle_post_events("add");
345 } else {
346 if ($this->orig_dn != $this->dn){
347 $this->move($this->orig_dn, $this->dn);
348 }
350 $ldap->cd($this->dn);
351 $ldap->modify($this->attrs);
352 $this->handle_post_events("modify");
353 }
354 show_ldap_error($ldap->get_error());
355 /* Optionally execute a command after we're done */
356 $this->postcreate();
357 }
359 }
361 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
362 ?>