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 = "dynamic";
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 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> <b>".
105 _("This 'dn' has no phone features.")."</b>";
106 return($display);
107 }
109 /* handle Permit Add*/
110 if(isset($_POST['goFonPermitAdd'])){
111 if(isset($_POST['goFonPermitNew'])){
112 if(is_string($this->goFonPermit)){
113 $this->goFonPermit=array();
114 }
115 $new = $_POST['goFonPermitNew'];
116 if(strlen($new)> 1) {
117 $this->goFonPermit[]= $new;
118 }
119 }
120 }
122 /* handle Deny Add*/
123 if(isset($_POST['goFonDenyAdd'])){
124 if(isset($_POST['goFonDenyNew'])){
125 if(is_string($this->goFonDeny)){
126 $this->goFonDeny=array();
127 }
128 $new = $_POST['goFonDenyNew'];
129 if(strlen($new)> 1) {
130 $this->goFonDeny[]= $new;
131 }
132 }
133 }
135 /* Handle Permit Deletion*/
136 if(isset($_POST['goFonPermitDel'])){
137 if(isset($_POST['goFonPermitS'])){
138 if(is_string($this->goFonPermit)){
139 $this->goFonPermit=array();
140 }
141 $new = $_POST['goFonPermitS'];
142 $tmp = array_flip($this->goFonPermit);
143 unset($tmp[$new]);
144 $this->goFonPermit=array();
145 foreach(array_flip($tmp) as $tm){
146 $this->goFonPermit[]=$tm;
147 }
148 }
149 }
152 /* Handle Permit Deletion*/
153 if(isset($_POST['goFonDenyDel'])){
154 if(isset($_POST['goFonDenyS'])){
155 if(is_string($this->goFonDeny)){
156 $this->goFonDeny=array();
157 }
158 $new = $_POST['goFonDenyS'];
159 $tmp = array_flip($this->goFonDeny);
160 unset($tmp[$new]);
161 $this->goFonDeny=array();
162 foreach(array_flip($tmp) as $tm){
163 $this->goFonDeny[]=$tm;
164 }
165 }
166 }
168 /* Fill templating stuff */
169 $smarty= get_smarty();
170 $smarty->assign("bases", $this->config->idepartments);
172 /* Create Arrays for samrty select boxes */
173 $smarty->assign("categories", array("SIP","IAX","CAPI"));
174 $smarty->assign("goFonTypes", array("peer" =>"peer" ,"user" =>"user" ,"friend" =>"friend"));
175 $smarty->assign("goFonDmtfModes", array("inband" =>"inband" ,"rfc2833"=>"rfc2833" ,"info" =>"info"));
176 $smarty->assign("goFonAuths", array("plaintext" =>"plaintext" ,"md5" =>"md5" /*,"rsa" =>"rsa"*/));
177 $smarty->assign("goFonTrunks", array("yes" =>_("yes") ,"no" => _("no")));
179 /* deativate all fields that are not used by the specified type */
180 foreach($this->attributes as $att){
181 if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
182 $smarty->assign($att."USED", "disabled" );
183 $smarty->assign($att, "");
184 }else{
185 $smarty->assign($att."USED", "" );
186 $smarty->assign($att, $this->$att);
187 }
188 }
190 $smarty->assign("selected_categorie",$this->selected_categorie);
192 /* Assign attributes */
193 foreach ($this->attributes as $attr){
194 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
195 }
196 $smarty->assign("base_select", $this->base);
198 $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
200 /* Show Asterisk for required attribute ipHostNumber and macAddress */
201 $smarty->assign("staticAddress", "<font class=\"must\">*</font>");
203 /* Show main page */
204 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
205 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
206 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
207 }
209 function remove_from_parent()
210 {
211 $ldap= $this->config->get_ldap_link();
212 $ldap->cd($this->config->current['BASE']);
214 $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
215 while ($attr = $ldap->fetch()){
216 print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
217 ($attr['uid'][0]." - ".$attr['cn'][0])));
218 return;
219 }
221 $ldap->rmdir($this->dn);
222 show_ldap_error($ldap->get_error());
223 $this->handle_post_events("remove");
225 /* Delete references to object groups */
226 $ldap->cd ($this->config->current['BASE']);
227 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
228 while ($ldap->fetch()){
229 $og= new ogroup($this->config, $ldap->getDN());
230 unset($og->member[$this->dn]);
231 $og->save ();
232 }
234 }
237 /* Save data to object */
238 function save_object()
239 {
240 plugin::save_object();
242 /* Save base, since this is no LDAP attribute */
243 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
244 $this->base= $_POST['base'];
245 }
246 }
249 /* Check supplied data */
250 function check()
251 {
252 $message= array();
253 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
255 /* To check for valid ip*/
256 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
258 // if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
259 // if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
260 // $message[]= _("Wrong IP format in field goFonDefaultIP.");
261 // }
262 // }
264 // /* Check for valid number */
265 // if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
266 // if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
267 // $message[]= _("The given value for 'Response timeout' is not a valid number.");
268 // }
269 // }
271 /* must: cn, macAddress, ipHostNumber */
272 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
273 $message[]= _("The required field 'Phone name' is not set.");
274 }
275 if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
276 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
277 }
278 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
279 $message[]= _("The required field 'MAC-address' is not set.");
280 }
281 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
282 $message[]= _("The required field 'IP-address' is not set.");
283 }
284 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
285 $message[]= _("Wrong IP format in field IP-address.");
286 }
288 $tr = count(split(":",$this->macAddress));
289 if($tr!=6){
290 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
291 }
293 $ui= get_userinfo();
294 $acl= get_permissions ($this->dn, $ui->subtreeACL);
295 $acl= get_module_permission($acl, "phone", $this->dn);
296 if (chkacl($acl, "create") != ""){
297 $message[]= _("You have no permissions to create a phone on this 'Base'.");
298 }
300 if ($this->orig_dn != $this->dn){
301 $ldap= $this->config->get_ldap_link();
302 $ldap->cd ($this->base);
303 $ldap->search ("(cn=".$this->cn.")", array("cn"));
304 if ($ldap->count() != 0){
305 while ($attrs= $ldap->fetch()){
306 if ($attrs['dn'] != $this->orig_dn){
307 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
308 break;
309 }
310 }
311 }
312 }
314 return ($message);
315 }
318 /* Save to LDAP */
319 function save()
320 {
321 plugin::save();
324 /* only to define which attrs to save*/
325 $mode = $this->attrs['selected_categorie'];
327 /* Remove all unwanted attrs */
328 foreach($this->attributes as $att){
330 /* Check all attributes, if they are needed for this type of phone */
331 if(!in_array($att,$this->usedattrs[$mode])){
332 $this->attrs[$att] = array();
333 }
335 /* Test rights of this user ... */
336 if(chkacl($this->acl,$att)!=""){
337 unset($this->attrs[$att]);
338 }
339 }
341 /* unset the categorie*/
342 unset($this->attrs['selected_categorie']);
344 /* Remove all empty values */
345 if ($this->orig_dn == 'new'){
346 $attrs= array();
347 foreach ($this->attrs as $key => $val){
348 if (is_array($val) && count($val) == 0){
349 continue;
350 }
351 $attrs[$key]= $val;
352 }
353 $this->attrs= $attrs;
354 }
356 if($this->goFonDefaultIP!="dynamic"){
357 $this->attrs['goFonDefaultIP'] = $this->ipHostNumber;
358 }
360 /* Write back to ldap */
361 $ldap= $this->config->get_ldap_link();
362 if ($this->orig_dn == 'new'){
363 $ldap->cd($this->config->current['BASE']);
364 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
365 $ldap->cd($this->dn);
366 $ldap->add($this->attrs);
367 $this->handle_post_events("add");
368 } else {
369 if ($this->orig_dn != $this->dn){
370 $this->move($this->orig_dn, $this->dn);
371 }
373 $ldap->cd($this->dn);
374 $ldap->modify($this->attrs);
375 // $user_phone_reload
376 $ldap->cd ($this->config->current['BASE']);
377 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
378 if($user_phone_assignment){
379 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
380 $usertab->by_object['phoneAccount']->is_modified = true;
381 $usertab->save();
382 unset($usertab);
383 }
384 $this->handle_post_events("modify");
385 }
386 show_ldap_error($ldap->get_error());
387 /* Optionally execute a command after we're done */
388 $this->postcreate();
389 }
391 }
393 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
394 ?>