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 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system phone/generic with dn '%s' failed!"),$this->dn));
277 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
279 /* Delete references to object groups */
280 $ldap->cd ($this->config->current['BASE']);
281 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
282 while ($ldap->fetch()){
283 $og= new ogroup($this->config, $ldap->getDN());
284 unset($og->member[$this->dn]);
285 $og->save ();
286 }
287 }
288 }
291 /* Save data to object */
292 function save_object()
293 {
294 /* Create a base backup and reset the
295 base directly after calling plugin::save_object();
296 Base will be set seperatly a few lines below */
297 $base_tmp = $this->base;
298 plugin::save_object();
299 $this->base = $base_tmp;
301 $this->netConfigDNS->save_object();
303 /* Set new base if allowed */
304 $tmp = $this->get_allowed_bases();
305 if(isset($_POST['base'])){
306 if(isset($tmp[$_POST['base']])){
307 $this->base= $_POST['base'];
308 }
309 }
310 }
313 /* Check supplied data */
314 function check()
315 {
316 /* Call common method to give check the hook */
317 $message= plugin::check();
318 $message= array_merge($message, $this->netConfigDNS->check());
320 $this->dn= "cn=".$this->cn.",".get_ou('phoneou').$this->base;
322 /* To check for valid ip*/
323 if($this->netConfigDNS->ipHostNumber == ""){
324 $message[]= _("IP address is not set!");
325 } else {
326 if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
327 $message[]= _("IP address is not valid!");
328 }
329 }
331 /* Check if given name is a valid host/dns name */
332 if(!tests::is_dns_name($this->cn) ){
333 $message[] = _("Name is not valid!");
334 }
336 if ($this->cn == ""){
337 $message[]= _("Name is not set!");
338 }
339 if ($this->cn == "0"){
340 $message[]= _("'0' is a reserved name and cannot be used!");
341 }
343 if ($this->orig_dn != $this->dn){
344 $ldap= $this->config->get_ldap_link();
345 $ldap->cd ($this->base);
346 $ldap->search ("(cn=".$this->cn.")", array("cn"));
347 if ($ldap->count() != 0){
348 while ($attrs= $ldap->fetch()){
350 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
351 continue;
352 }
354 if ($attrs['dn'] != $this->orig_dn){
355 $message[]= _("Name is already in use!");
356 break;
357 }
358 }
359 }
360 }
362 return ($message);
363 }
366 /* Save to LDAP */
367 function save()
368 {
369 plugin::save();
372 /* only to define which attrs to save*/
373 $mode = $this->selected_categorie;
375 /* Remove all unwanted attrs */
376 foreach($this->attributes as $att){
378 /* Check all attributes, if they are needed for this type of phone */
379 if(!in_array($att,$this->usedattrs[$mode])){
380 $this->attrs[$att] = array();
381 }
382 }
384 /* unset the categorie*/
385 unset($this->attrs['selected_categorie']);
387 /* Remove all empty values */
388 if ($this->orig_dn == 'new'){
389 $attrs= array();
390 foreach ($this->attrs as $key => $val){
391 if (is_array($val) && count($val) == 0){
392 continue;
393 }
394 $attrs[$key]= $val;
395 }
396 $this->attrs= $attrs;
397 }
399 if($this->goFonDefaultIP!="dynamic"){
400 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
401 }
403 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
405 /* Write back to ldap */
406 $ldap= $this->config->get_ldap_link();
407 if ($this->orig_dn == 'new'){
408 $ldap->cd($this->config->current['BASE']);
409 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
410 $ldap->cd($this->dn);
411 $ldap->add($this->attrs);
412 new log("create","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
413 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
414 } else {
415 if ($this->orig_dn != $this->dn){
416 $this->move($this->orig_dn, $this->dn);
417 }
419 $ldap->cd($this->dn);
420 $this->cleanup();
421 $ldap->modify ($this->attrs);
422 new log("modify","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
424 // $user_phone_reload
425 $ldap->cd ($this->config->current['BASE']);
426 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
427 if($user_phone_assignment){
428 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
429 $usertab->by_object['phoneAccount']->is_modified = true;
430 $usertab->save();
431 unset($usertab);
432 }
433 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
434 }
435 $this->netConfigDNS->cn = $this->cn;
436 $this->netConfigDNS->save();
437 show_ldap_error($ldap->get_error(), sprintf(_("Saving of phone/generic with dn '%s' failed!"),$this->dn));
438 }
441 /* Display generic part for server copy & paste */
442 function getCopyDialog()
443 {
444 $vars = array("cn");
445 $smarty = get_smarty();
446 $smarty->assign("cn" ,$this->cn);
447 $smarty->assign("object","phone");
448 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
449 $ret = array();
450 $ret['string'] = $str;
451 $ret['status'] = "";
452 return($ret);
453 }
456 function saveCopyDialog()
457 {
458 if(isset($_POST['cn'])){
459 $this->cn = $_POST['cn'];
460 }
461 }
464 function PrepareForCopyPaste($source)
465 {
466 plugin::PrepareForCopyPaste($source);
467 if(isset($source['macAddress'][0])){
468 $this->netConfigDNS->macAddress = $source['macAddress'][0];
469 }
470 if(isset($source['ipHostNumber'][0])){
471 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
472 }
474 $source_o = new phoneGeneric($this->config, $source['dn']);
475 $this->selected_categorie = $source_o->selected_categorie;
476 }
479 /* Return plugin informations for acl handling
480 #FIXME FAIscript seams to ununsed within this class... */
481 static function plInfo()
482 {
483 return (array(
484 "plShortName" => _("Generic"),
485 "plDescription" => _("Phone generic"),
486 "plSelfModify" => FALSE,
487 "plDepends" => array(),
488 "plPriority" => 0,
489 "plSection" => array("administration"),
490 "plCategory" => array("phone" => array("description" => _("Phone hardware"),
491 "objectClass" => "goFonHardware")),
492 "plProvidedAcls"=> array(
493 "cn" => _("Name"),
494 "base" => _("Base"),
495 "description" => _("Description"),
496 "goFonType" => _("SIP Mode"),
497 "goFonDmtfMode" => _("SIP DTMF mode"),
498 "goFonDefaultIP" => _("SIP Default ip"),
499 "goFonQualify" => _("SIP Qualify"),
500 "goFonAuth" => _("IAX authentication type"),
501 "goFonSecret" => _("IAX secret"),
502 "goFonAccountCode" => _("IAX account code"),
503 "goFonTrunk" => _("IAX trunk lines"),
504 "goFonPermit" => _("IAX permit settings"),
505 "goFonDeny" => _("IAX deny settings"),
506 "goFonMSN" => _("CAPI MSN"),
507 "categorie" => _("Hardware type"))
508 ));
509 }
512 }
514 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
515 ?>