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