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"));
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 }
66 if($this->goFonMSN != ""){
67 $this->selected_categorie = 2;
68 }elseif($this->goFonAccountCode != ""){
69 $this->selected_categorie = 1 ;
71 if(isset($this->attrs['goFonPermit']['count'])){
72 unset ($this->attrs['goFonPermit']['count']);
73 $this->goFonPermit=$this->attrs['goFonPermit'];
74 }
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> <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 }
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 }
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 }
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"));
223 $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Network settings")));
224 $this->netConfigDNS->cn= $this->cn;
225 $smarty->assign("netconfig", $this->netConfigDNS->execute());
227 /* Display sub dialog from network settings */
228 if($this->netConfigDNS->dialog){
229 $this->dialog = TRUE;
230 return($this->netConfigDNS->execute());
231 }else{
232 $this->dialog = FALSE;
233 }
235 /* Show main page */
236 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
237 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
238 }
240 function remove_from_parent()
241 {
242 $this->netConfigDNS->acl = $this->acl;
243 $ldap= $this->config->get_ldap_link();
244 $ldap->cd($this->config->current['BASE']);
246 $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
247 while ($attr = $ldap->fetch()){
248 print_red(sprintf(_("Can't delete because there are users which are depending on this phone. One of them is user '%s'."),
249 ($attr['uid'][0]." - ".$attr['cn'][0])));
250 return;
251 }
253 $this->netConfigDNS->remove_from_parent();
254 $ldap->rmdir($this->dn);
255 show_ldap_error($ldap->get_error(), _("Removing phone failed"));
256 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
258 /* Delete references to object groups */
259 $ldap->cd ($this->config->current['BASE']);
260 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
261 while ($ldap->fetch()){
262 $og= new ogroup($this->config, $ldap->getDN());
263 unset($og->member[$this->dn]);
264 $og->save ();
265 }
266 }
269 /* Save data to object */
270 function save_object()
271 {
272 plugin::save_object();
273 $this->netConfigDNS->save_object();
274 /* Save base, since this is no LDAP attribute */
275 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
276 $this->base= $_POST['base'];
277 }
278 }
281 /* Check supplied data */
282 function check()
283 {
284 /* Call common method to give check the hook */
285 $message= plugin::check();
286 $message= array_merge($message, $this->netConfigDNS->check());
288 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
290 /* To check for valid ip*/
291 if($this->netConfigDNS->ipHostNumber == ""){
292 $message[]= _("The required field IP address is empty.");
293 } else {
294 if (!is_ip($this->netConfigDNS->ipHostNumber)){
295 $message[]= _("The field IP address contains an invalid address.");
296 }
297 }
299 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
300 $message[]= _("The required field 'Phone name' is not set.");
301 }
302 if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
303 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
304 }
306 /* Check if given name is a valid host/dns name */
307 if(!is_dns_name($this->cn) && chkacl ($this->acl, "cn") == ""){
308 $message[] = _("Please specify a valid name for this object.");
309 }
311 $ui= get_userinfo();
312 $acl= get_permissions ($this->dn, $ui->subtreeACL);
313 $acl= get_module_permission($acl, "phone", $this->dn);
314 if (chkacl($acl, "create") != ""){
315 $message[]= _("You have no permissions to create a phone on this 'Base'.");
316 }
318 if ($this->orig_dn != $this->dn){
319 $ldap= $this->config->get_ldap_link();
320 $ldap->cd ($this->base);
321 $ldap->search ("(cn=".$this->cn.")", array("cn"));
322 if ($ldap->count() != 0){
323 while ($attrs= $ldap->fetch()){
325 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
326 continue;
327 }
329 if ($attrs['dn'] != $this->orig_dn){
330 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
331 break;
332 }
333 }
334 }
335 }
337 return ($message);
338 }
341 /* Save to LDAP */
342 function save()
343 {
344 $this->netConfigDNS->acl = $this->acl;
345 plugin::save();
348 /* only to define which attrs to save*/
349 $mode = $this->attrs['selected_categorie'];
351 /* Remove all unwanted attrs */
352 foreach($this->attributes as $att){
354 /* Check all attributes, if they are needed for this type of phone */
355 if(!in_array($att,$this->usedattrs[$mode])){
356 $this->attrs[$att] = array();
357 }
359 /* Test rights of this user ... */
360 if(chkacl($this->acl,$att)!=""){
361 unset($this->attrs[$att]);
362 }
363 }
365 /* unset the categorie*/
366 unset($this->attrs['selected_categorie']);
368 /* Remove all empty values */
369 if ($this->orig_dn == 'new'){
370 $attrs= array();
371 foreach ($this->attrs as $key => $val){
372 if (is_array($val) && count($val) == 0){
373 continue;
374 }
375 $attrs[$key]= $val;
376 }
377 $this->attrs= $attrs;
378 }
380 if($this->goFonDefaultIP!="dynamic"){
381 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
382 }
384 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
386 /* Write back to ldap */
387 $ldap= $this->config->get_ldap_link();
388 if ($this->orig_dn == 'new'){
389 $ldap->cd($this->config->current['BASE']);
390 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
391 $ldap->cd($this->dn);
392 $ldap->add($this->attrs);
393 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
394 } else {
395 if ($this->orig_dn != $this->dn){
396 $this->move($this->orig_dn, $this->dn);
397 }
399 $ldap->cd($this->dn);
400 $this->cleanup();
401 $ldap->modify ($this->attrs);
403 // $user_phone_reload
404 $ldap->cd ($this->config->current['BASE']);
405 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
406 if($user_phone_assignment){
407 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
408 $usertab->by_object['phoneAccount']->is_modified = true;
409 $usertab->save();
410 unset($usertab);
411 }
412 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
413 }
414 $this->netConfigDNS->cn = $this->cn;
415 $this->netConfigDNS->save($this->dn);
416 show_ldap_error($ldap->get_error(), _("Saving phone failed"));
417 }
419 }
421 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
422 ?>