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;
30 var $view_logged = FALSE;
32 /* attribute list for save action */
33 var $attributes= array("cn", "description",
34 "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
35 "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
36 "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie","goFonPermit","goFonDeny"
37 );
39 /* this array defines which attributes are schown / saved for the different type of phones */
40 var $usedattrs = array( "0"=>array("cn", "description",
41 "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
42 "goFonQualify"),
43 "1"=>array("cn", "description",
44 "goFonType","goFonHost","goFonDefaultIP",
45 "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
46 "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
47 "2"=>array("cn", "description", "goFonMSN"));
50 var $objectclasses= array("top", "goFonHardware");
52 function phoneGeneric (&$config, $dn= NULL, $parent= NULL)
53 {
54 plugin::plugin ($config, $dn, $parent);
55 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses, true);
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 set_acl_base($base)
94 {
95 plugin::set_acl_base($base);
96 $this->netConfigDNS->set_acl_base($base);
97 }
99 function set_acl_category($cat)
100 {
101 plugin::set_acl_category($cat);
102 $this->netConfigDNS->set_acl_category($cat);
103 }
106 function execute()
107 {
108 /* Call parent execute */
109 plugin::execute();
111 if($this->is_account && !$this->view_logged){
112 $this->view_logged = TRUE;
113 new log("view","phone/".get_class($this),$this->dn);
114 }
116 /* Do we represent a valid phone? */
117 if (!$this->is_account && $this->parent === NULL){
118 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
119 _("This 'dn' has no phone features.")."</b>";
120 return($display);
121 }
123 /* Base select dialog */
124 $once = true;
125 foreach($_POST as $name => $value){
126 if(preg_match("/^chooseBase/",$name) && $once){
127 $once = false;
128 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
129 $this->dialog->setCurrentBase($this->base);
130 }
131 }
133 /* Dialog handling */
134 if(is_object($this->dialog)){
135 /* Must be called before save_object */
136 $this->dialog->save_object();
138 if($this->dialog->isClosed()){
139 $this->dialog = false;
140 }elseif($this->dialog->isSelected()){
142 /* A new base was selected, check if it is a valid one */
143 $tmp = $this->get_allowed_bases();
144 if(isset($tmp[$this->dialog->isSelected()])){
145 $this->base = $this->dialog->isSelected();
146 }
148 $this->dialog= false;
149 }else{
150 return($this->dialog->execute());
151 }
152 }
154 /* handle Permit Add*/
155 if(isset($_POST['goFonPermitAdd']) && $this->acl_is_writeable("goFonPermit")){
156 if(isset($_POST['goFonPermitNew'])){
157 if(is_string($this->goFonPermit)){
158 $this->goFonPermit=array();
159 }
160 $new = $_POST['goFonPermitNew'];
161 if(strlen($new)> 1) {
162 $this->goFonPermit[]= $new;
163 }
164 }
165 }
167 /* handle Deny Add*/
168 if(isset($_POST['goFonDenyAdd']) && $this->acl_is_writeable("goFonDeny")){
169 if(isset($_POST['goFonDenyNew'])){
170 if(is_string($this->goFonDeny)){
171 $this->goFonDeny=array();
172 }
173 $new = $_POST['goFonDenyNew'];
174 if(strlen($new)> 1) {
175 $this->goFonDeny[]= $new;
176 }
177 }
178 }
180 /* Handle Permit Deletion*/
181 if(isset($_POST['goFonPermitDel']) && $this->acl_is_writeable("goFonPermit")){
182 if(isset($_POST['goFonPermitS'])){
183 if(is_string($this->goFonPermit)){
184 $this->goFonPermit=array();
185 }
186 $new = $_POST['goFonPermitS'];
187 $tmp = array_flip($this->goFonPermit);
188 unset($tmp[$new]);
189 $this->goFonPermit=array();
190 foreach(array_flip($tmp) as $tm){
191 $this->goFonPermit[]=$tm;
192 }
193 }
194 }
197 /* Handle Permit Deletion*/
198 if(isset($_POST['goFonDenyDel']) && $this->acl_is_writeable("goFonDeny")){
199 if(isset($_POST['goFonDenyS'])){
200 if(is_string($this->goFonDeny)){
201 $this->goFonDeny=array();
202 }
203 $new = $_POST['goFonDenyS'];
204 $tmp = array_flip($this->goFonDeny);
205 unset($tmp[$new]);
206 $this->goFonDeny=array();
207 foreach(array_flip($tmp) as $tm){
208 $this->goFonDeny[]=$tm;
209 }
210 }
211 }
213 /* Fill templating stuff */
214 $smarty= get_smarty();
216 $smarty->assign("bases", $this->get_allowed_bases());
218 $tmp = $this->plInfo();
219 foreach($tmp['plProvidedAcls'] as $name => $translation){
220 $smarty->assign($name."ACL",$this->getacl($name));
221 }
223 $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
225 /* Create Arrays for samrty select boxes */
226 $smarty->assign("categories", array("SIP","IAX","CAPI"));
227 $smarty->assign("goFonTypes", array("peer" =>"peer" ,"user" =>"user" ,"friend" =>"friend"));
228 $smarty->assign("goFonDmtfModes", array("inband" =>"inband" ,"rfc2833"=>"rfc2833" ,"info" =>"info"));
229 $smarty->assign("goFonAuths", array("plaintext" =>"plaintext" ,"md5" =>"md5" /*,"rsa" =>"rsa"*/));
230 $smarty->assign("goFonTrunks", array("yes" =>_("yes") ,"no" => _("no")));
232 /* deativate all fields that are not used by the specified type */
233 foreach($this->attributes as $att){
234 if((!in_array($att,$this->usedattrs[$this->selected_categorie]))){
235 $smarty->assign($att."USED", "disabled" );
236 $smarty->assign($att, "");
237 }else{
238 $smarty->assign($att."USED", "" );
239 $smarty->assign($att, $this->$att);
240 }
241 }
243 $smarty->assign("selected_categorie",$this->selected_categorie);
245 /* Assign attributes */
246 $smarty->assign("base_select", $this->base);
247 $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
249 /* Show main page */
250 $str = $this->netConfigDNS->execute();
251 if(is_object($this->netConfigDNS->dialog)){
252 return($str);
253 }
254 $smarty->assign("netconfig", $str);
255 $smarty->assign("phonesettings", get_template_path("phonesettings.tpl",TRUE,dirname(__FILE__)));
256 return($smarty->fetch (get_template_path('phone.tpl', TRUE,dirname(__FILE__))));
257 }
259 function remove_from_parent()
260 {
261 if($this->acl_is_removeable()){
262 $ldap= $this->config->get_ldap_link();
263 $ldap->cd($this->config->current['BASE']);
265 $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
266 while ($attr = $ldap->fetch()){
267 msg_dialog::display(_("Error"), sprintf(_("Cannot delete entry because it is still in use by '%s'!"), $attr['cn'][0]), ERROR_DIALOG);
268 return;
269 }
271 $this->netConfigDNS->remove_from_parent();
272 $ldap->rmdir($this->dn);
274 new log("remove","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
276 if (!$ldap->success()){
277 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
278 }
279 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
281 /* Delete references to object groups */
282 $ldap->cd ($this->config->current['BASE']);
283 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
284 while ($ldap->fetch()){
285 $og= new ogroup($this->config, $ldap->getDN());
286 unset($og->member[$this->dn]);
287 $og->save ();
288 }
289 }
290 }
293 /* Save data to object */
294 function save_object()
295 {
296 /* Create a base backup and reset the
297 base directly after calling plugin::save_object();
298 Base will be set seperatly a few lines below */
299 $base_tmp = $this->base;
300 plugin::save_object();
301 $this->base = $base_tmp;
303 $this->netConfigDNS->save_object();
305 /* Set new base if allowed */
306 $tmp = $this->get_allowed_bases();
307 if(isset($_POST['base'])){
308 if(isset($tmp[$_POST['base']])){
309 $this->base= $_POST['base'];
310 }
311 }
312 }
315 /* Check supplied data */
316 function check()
317 {
318 /* Call common method to give check the hook */
319 $message= plugin::check();
320 $message= array_merge($message, $this->netConfigDNS->check());
322 $this->dn= "cn=".$this->cn.",".get_ou('phoneou').$this->base;
324 /* To check for valid ip*/
325 if($this->netConfigDNS->ipHostNumber == ""){
326 $message[]= _("IP address is not set!");
327 } else {
328 if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
329 $message[]= _("IP address is not valid!");
330 }
331 }
333 /* Check if given name is a valid host/dns name */
334 if(!tests::is_dns_name($this->cn) ){
335 $message[] = _("Name is not valid!");
336 }
338 if ($this->cn == ""){
339 $message[]= _("Name is not set!");
340 }
341 if ($this->cn == "0"){
342 $message[]= _("'0' is a reserved name and cannot be used!");
343 }
345 if ($this->orig_dn != $this->dn){
346 $ldap= $this->config->get_ldap_link();
347 $ldap->cd ($this->base);
348 $ldap->search ("(cn=".$this->cn.")", array("cn"));
349 if ($ldap->count() != 0){
350 while ($attrs= $ldap->fetch()){
352 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
353 continue;
354 }
356 if ($attrs['dn'] != $this->orig_dn){
357 $message[]= _("Name is already in use!");
358 break;
359 }
360 }
361 }
362 }
364 return ($message);
365 }
368 /* Save to LDAP */
369 function save()
370 {
371 plugin::save();
374 /* only to define which attrs to save*/
375 $mode = $this->selected_categorie;
377 /* Remove all unwanted attrs */
378 foreach($this->attributes as $att){
380 /* Check all attributes, if they are needed for this type of phone */
381 if(!in_array($att,$this->usedattrs[$mode])){
382 $this->attrs[$att] = array();
383 }
384 }
386 /* unset the categorie*/
387 unset($this->attrs['selected_categorie']);
389 /* Remove all empty values */
390 if ($this->orig_dn == 'new'){
391 $attrs= array();
392 foreach ($this->attrs as $key => $val){
393 if (is_array($val) && count($val) == 0){
394 continue;
395 }
396 $attrs[$key]= $val;
397 }
398 $this->attrs= $attrs;
399 }
401 if($this->goFonDefaultIP!="dynamic"){
402 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
403 }
405 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
407 /* Write back to ldap */
408 $ldap= $this->config->get_ldap_link();
409 if ($this->orig_dn == 'new'){
410 $ldap->cd($this->config->current['BASE']);
411 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
412 $ldap->cd($this->dn);
413 $ldap->add($this->attrs);
414 new log("create","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
415 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
416 } else {
417 if ($this->orig_dn != $this->dn){
418 $this->move($this->orig_dn, $this->dn);
419 }
421 $ldap->cd($this->dn);
422 $this->cleanup();
423 $ldap->modify ($this->attrs);
424 new log("modify","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
426 // $user_phone_reload
427 $ldap->cd ($this->config->current['BASE']);
428 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
429 if($user_phone_assignment){
430 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
431 $usertab->by_object['phoneAccount']->is_modified = true;
432 $usertab->save();
433 unset($usertab);
434 }
435 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
436 }
437 $this->netConfigDNS->cn = $this->cn;
438 $this->netConfigDNS->save();
439 if (!$ldap->success()){
440 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
441 }
442 }
445 /* Display generic part for server copy & paste */
446 function getCopyDialog()
447 {
448 $vars = array("cn");
449 $smarty = get_smarty();
450 $smarty->assign("cn" ,$this->cn);
451 $smarty->assign("object","phone");
452 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
453 $ret = array();
454 $ret['string'] = $str;
455 $ret['status'] = "";
456 return($ret);
457 }
460 function saveCopyDialog()
461 {
462 if(isset($_POST['cn'])){
463 $this->cn = $_POST['cn'];
464 }
465 }
468 function PrepareForCopyPaste($source)
469 {
470 plugin::PrepareForCopyPaste($source);
471 if(isset($source['macAddress'][0])){
472 $this->netConfigDNS->macAddress = $source['macAddress'][0];
473 }
474 if(isset($source['ipHostNumber'][0])){
475 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
476 }
478 $source_o = new phoneGeneric($this->config, $source['dn']);
479 $this->selected_categorie = $source_o->selected_categorie;
480 }
483 /* Return plugin informations for acl handling
484 #FIXME FAIscript seams to ununsed within this class... */
485 static function plInfo()
486 {
487 return (array(
488 "plShortName" => _("Generic"),
489 "plDescription" => _("Phone generic"),
490 "plSelfModify" => FALSE,
491 "plDepends" => array(),
492 "plPriority" => 0,
493 "plSection" => array("administration"),
494 "plCategory" => array("phone" => array("description" => _("Phone hardware"),
495 "objectClass" => "goFonHardware")),
496 "plProvidedAcls"=> array(
497 "cn" => _("Name"),
498 "base" => _("Base"),
499 "description" => _("Description"),
500 "goFonType" => _("SIP Mode"),
501 "goFonDmtfMode" => _("SIP DTMF mode"),
502 "goFonDefaultIP" => _("SIP Default ip"),
503 "goFonQualify" => _("SIP Qualify"),
504 "goFonAuth" => _("IAX authentication type"),
505 "goFonSecret" => _("IAX secret"),
506 "goFonAccountCode" => _("IAX account code"),
507 "goFonTrunk" => _("IAX trunk lines"),
508 "goFonPermit" => _("IAX permit settings"),
509 "goFonDeny" => _("IAX deny settings"),
510 "goFonMSN" => _("CAPI MSN"),
511 "categorie" => _("Hardware type"))
512 ));
513 }
516 }
518 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
519 ?>