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 $description = "";
18 var $orig_dn = "";
19 var $goFonType = "";
20 var $goFonDmtfMode = "";
21 var $goFonHost = "";
22 var $goFonDefaultIP = "dynamic";
23 var $goFonQualify = "";
24 var $goFonAuth = "";
25 var $goFonSecret = "";
26 var $goFonInkeys = "";
27 var $goFonPermit = array();
28 var $goFonDeny = array();
29 var $goFonOutkey = "";
30 var $goFonTrunk = "";
31 var $goFonAccountCode = "";
32 var $goFonMSN = "";
33 var $selected_categorie = 0;
34 var $netConfigDNS;
35 var $view_logged = FALSE;
37 /* attribute list for save action */
38 var $attributes= array("cn", "description",
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",
46 "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
47 "goFonQualify"),
48 "1"=>array("cn", "description",
49 "goFonType","goFonHost","goFonDefaultIP",
50 "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
51 "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
52 "2"=>array("cn", "description", "goFonMSN"));
55 var $objectclasses= array("top", "goFonHardware");
57 function phoneGeneric (&$config, $dn= NULL, $parent= NULL)
58 {
59 plugin::plugin ($config, $dn, $parent);
60 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses, true);
62 /* Set base */
63 if ($this->dn == "new"){
64 $ui= get_userinfo();
65 $this->base= dn2base($ui->dn);
66 $this->cn= "";
67 } else {
68 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
69 }
71 if($this->goFonMSN != ""){
72 $this->selected_categorie = 2;
73 }elseif($this->goFonAccountCode != ""){
74 $this->selected_categorie = 1 ;
76 if(isset($this->attrs['goFonPermit']['count'])){
77 unset ($this->attrs['goFonPermit']['count']);
78 $this->goFonPermit=$this->attrs['goFonPermit'];
79 }
81 if(isset($this->attrs['goFonDeny']['count'])){
82 unset ($this->attrs['goFonDeny']['count']) ;
83 $this->goFonDeny=$this->attrs['goFonDeny'];
84 }
86 } else {
87 $this->selected_categorie = 0;
88 }
90 if($this->goFonDefaultIP!="dynamic"){
91 $this->goFonDefaultIP = "network";
92 }
94 /* Save dn for later references */
95 $this->orig_dn= $this->dn;
96 }
98 function set_acl_base($base)
99 {
100 plugin::set_acl_base($base);
101 $this->netConfigDNS->set_acl_base($base);
102 }
104 function set_acl_category($cat)
105 {
106 plugin::set_acl_category($cat);
107 $this->netConfigDNS->set_acl_category($cat);
108 }
111 function execute()
112 {
113 /* Call parent execute */
114 plugin::execute();
116 if($this->is_account && !$this->view_logged){
117 $this->view_logged = TRUE;
118 new log("view","phone/".get_class($this),$this->dn);
119 }
121 /* Do we represent a valid phone? */
122 if (!$this->is_account && $this->parent === NULL){
123 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
124 _("This 'dn' has no phone features.")."</b>";
125 return($display);
126 }
128 /* Base select dialog */
129 $once = true;
130 foreach($_POST as $name => $value){
131 if(preg_match("/^chooseBase/",$name) && $once){
132 $once = false;
133 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
134 $this->dialog->setCurrentBase($this->base);
135 }
136 }
138 /* Dialog handling */
139 if(is_object($this->dialog)){
140 /* Must be called before save_object */
141 $this->dialog->save_object();
143 if($this->dialog->isClosed()){
144 $this->dialog = false;
145 }elseif($this->dialog->isSelected()){
147 /* A new base was selected, check if it is a valid one */
148 $tmp = $this->get_allowed_bases();
149 if(isset($tmp[$this->dialog->isSelected()])){
150 $this->base = $this->dialog->isSelected();
151 }
153 $this->dialog= false;
154 }else{
155 return($this->dialog->execute());
156 }
157 }
159 /* handle Permit Add*/
160 if(isset($_POST['goFonPermitAdd']) && $this->acl_is_writeable("goFonPermit")){
161 if(isset($_POST['goFonPermitNew'])){
162 if(is_string($this->goFonPermit)){
163 $this->goFonPermit=array();
164 }
165 $new = $_POST['goFonPermitNew'];
166 if(strlen($new)> 1) {
167 $this->goFonPermit[]= $new;
168 }
169 }
170 }
172 /* handle Deny Add*/
173 if(isset($_POST['goFonDenyAdd']) && $this->acl_is_writeable("goFonDeny")){
174 if(isset($_POST['goFonDenyNew'])){
175 if(is_string($this->goFonDeny)){
176 $this->goFonDeny=array();
177 }
178 $new = $_POST['goFonDenyNew'];
179 if(strlen($new)> 1) {
180 $this->goFonDeny[]= $new;
181 }
182 }
183 }
185 /* Handle Permit Deletion*/
186 if(isset($_POST['goFonPermitDel']) && $this->acl_is_writeable("goFonPermit")){
187 if(isset($_POST['goFonPermitS'])){
188 if(is_string($this->goFonPermit)){
189 $this->goFonPermit=array();
190 }
191 $new = $_POST['goFonPermitS'];
192 $tmp = array_flip($this->goFonPermit);
193 unset($tmp[$new]);
194 $this->goFonPermit=array();
195 foreach(array_flip($tmp) as $tm){
196 $this->goFonPermit[]=$tm;
197 }
198 }
199 }
202 /* Handle Permit Deletion*/
203 if(isset($_POST['goFonDenyDel']) && $this->acl_is_writeable("goFonDeny")){
204 if(isset($_POST['goFonDenyS'])){
205 if(is_string($this->goFonDeny)){
206 $this->goFonDeny=array();
207 }
208 $new = $_POST['goFonDenyS'];
209 $tmp = array_flip($this->goFonDeny);
210 unset($tmp[$new]);
211 $this->goFonDeny=array();
212 foreach(array_flip($tmp) as $tm){
213 $this->goFonDeny[]=$tm;
214 }
215 }
216 }
218 /* Fill templating stuff */
219 $smarty= get_smarty();
221 $smarty->assign("bases", $this->get_allowed_bases());
223 $tmp = $this->plInfo();
224 foreach($tmp['plProvidedAcls'] as $name => $translation){
225 $smarty->assign($name."ACL",$this->getacl($name));
226 }
228 $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
230 /* Create Arrays for samrty select boxes */
231 $smarty->assign("categories", array("SIP","IAX","CAPI"));
232 $smarty->assign("goFonTypes", array("peer" =>"peer" ,"user" =>"user" ,"friend" =>"friend"));
233 $smarty->assign("goFonDmtfModes", array("inband" =>"inband" ,"rfc2833"=>"rfc2833" ,"info" =>"info"));
234 $smarty->assign("goFonAuths", array("plaintext" =>"plaintext" ,"md5" =>"md5" /*,"rsa" =>"rsa"*/));
235 $smarty->assign("goFonTrunks", array("yes" =>_("yes") ,"no" => _("no")));
237 /* deativate all fields that are not used by the specified type */
238 foreach($this->attributes as $att){
239 if((!in_array($att,$this->usedattrs[$this->selected_categorie]))){
240 $smarty->assign($att."USED", "disabled" );
241 $smarty->assign($att, "");
242 }else{
243 $smarty->assign($att."USED", "" );
244 $smarty->assign($att, $this->$att);
245 }
246 }
248 $smarty->assign("selected_categorie",$this->selected_categorie);
250 /* Assign attributes */
251 $smarty->assign("base_select", $this->base);
252 $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
254 /* Show main page */
255 $str = $this->netConfigDNS->execute();
256 if(is_object($this->netConfigDNS->dialog)){
257 return($str);
258 }
259 $smarty->assign("netconfig", $str);
260 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
261 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
262 }
264 function remove_from_parent()
265 {
266 if($this->acl_is_removeable()){
267 $ldap= $this->config->get_ldap_link();
268 $ldap->cd($this->config->current['BASE']);
270 $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
271 while ($attr = $ldap->fetch()){
272 print_red(sprintf(_("Can't delete because there are users which are depending on this phone. One of them is user '%s'."),
273 ($attr['uid'][0]." - ".$attr['cn'][0])));
274 return;
275 }
277 $this->netConfigDNS->remove_from_parent();
278 $ldap->rmdir($this->dn);
280 new log("remove","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
282 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system phone/generic with dn '%s' failed."),$this->dn));
283 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
285 /* Delete references to object groups */
286 $ldap->cd ($this->config->current['BASE']);
287 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
288 while ($ldap->fetch()){
289 $og= new ogroup($this->config, $ldap->getDN());
290 unset($og->member[$this->dn]);
291 $og->save ();
292 }
293 }
294 }
297 /* Save data to object */
298 function save_object()
299 {
300 /* Create a base backup and reset the
301 base directly after calling plugin::save_object();
302 Base will be set seperatly a few lines below */
303 $base_tmp = $this->base;
304 plugin::save_object();
305 $this->base = $base_tmp;
307 $this->netConfigDNS->save_object();
309 /* Set new base if allowed */
310 $tmp = $this->get_allowed_bases();
311 if(isset($_POST['base'])){
312 if(isset($tmp[$_POST['base']])){
313 $this->base= $_POST['base'];
314 }
315 }
316 }
319 /* Check supplied data */
320 function check()
321 {
322 /* Call common method to give check the hook */
323 $message= plugin::check();
324 $message= array_merge($message, $this->netConfigDNS->check());
326 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
328 /* To check for valid ip*/
329 if($this->netConfigDNS->ipHostNumber == ""){
330 $message[]= _("The required field IP address is empty.");
331 } else {
332 if (!is_ip($this->netConfigDNS->ipHostNumber)){
333 $message[]= _("The field IP address contains an invalid address.");
334 }
335 }
337 if ($this->cn == ""){
338 $message[]= _("The required field 'Phone name' is not set.");
339 }
340 if ($this->cn == "0"){
341 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
342 }
344 if ($this->orig_dn != $this->dn){
345 $ldap= $this->config->get_ldap_link();
346 $ldap->cd ($this->base);
347 $ldap->search ("(cn=".$this->cn.")", array("cn"));
348 if ($ldap->count() != 0){
349 while ($attrs= $ldap->fetch()){
351 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
352 continue;
353 }
355 if ($attrs['dn'] != $this->orig_dn){
356 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
357 break;
358 }
359 }
360 }
361 }
363 return ($message);
364 }
367 /* Save to LDAP */
368 function save()
369 {
370 plugin::save();
373 /* only to define which attrs to save*/
374 $mode = $this->selected_categorie;
376 /* Remove all unwanted attrs */
377 foreach($this->attributes as $att){
379 /* Check all attributes, if they are needed for this type of phone */
380 if(!in_array($att,$this->usedattrs[$mode])){
381 $this->attrs[$att] = array();
382 }
383 }
385 /* unset the categorie*/
386 unset($this->attrs['selected_categorie']);
388 /* Remove all empty values */
389 if ($this->orig_dn == 'new'){
390 $attrs= array();
391 foreach ($this->attrs as $key => $val){
392 if (is_array($val) && count($val) == 0){
393 continue;
394 }
395 $attrs[$key]= $val;
396 }
397 $this->attrs= $attrs;
398 }
400 if($this->goFonDefaultIP!="dynamic"){
401 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
402 }
404 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
406 /* Write back to ldap */
407 $ldap= $this->config->get_ldap_link();
408 if ($this->orig_dn == 'new'){
409 $ldap->cd($this->config->current['BASE']);
410 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
411 $ldap->cd($this->dn);
412 $ldap->add($this->attrs);
413 new log("create","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
414 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
415 } else {
416 if ($this->orig_dn != $this->dn){
417 $this->move($this->orig_dn, $this->dn);
418 }
420 $ldap->cd($this->dn);
421 $this->cleanup();
422 $ldap->modify ($this->attrs);
423 new log("modify","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
425 // $user_phone_reload
426 $ldap->cd ($this->config->current['BASE']);
427 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
428 if($user_phone_assignment){
429 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
430 $usertab->by_object['phoneAccount']->is_modified = true;
431 $usertab->save();
432 unset($usertab);
433 }
434 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
435 }
436 $this->netConfigDNS->cn = $this->cn;
437 $this->netConfigDNS->save();
438 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system phone/generic with dn '%s' failed."),$this->dn));
439 }
442 /* Display generic part for server copy & paste */
443 function getCopyDialog()
444 {
445 $vars = array("cn");
446 $smarty = get_smarty();
447 $smarty->assign("cn" ,$this->cn);
448 $smarty->assign("object","phone");
449 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
450 $ret = array();
451 $ret['string'] = $str;
452 $ret['status'] = "";
453 return($ret);
454 }
457 function saveCopyDialog()
458 {
459 if(isset($_POST['cn'])){
460 $this->cn = $_POST['cn'];
461 }
462 }
465 function PrepareForCopyPaste($source)
466 {
467 plugin::PrepareForCopyPaste($source);
468 if(isset($source['macAddress'][0])){
469 $this->netConfigDNS->macAddress = $source['macAddress'][0];
470 }
471 if(isset($source['ipHostNumber'][0])){
472 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
473 }
475 $source_o = new phoneGeneric($this->config, $source['dn']);
476 $this->selected_categorie = $source_o->selected_categorie;
477 }
480 /* Return plugin informations for acl handling
481 #FIXME FAIscript seams to ununsed within this class... */
482 function plInfo()
483 {
484 return (array(
485 "plShortName" => _("Generic"),
486 "plDescription" => _("Phone generic"),
487 "plSelfModify" => FALSE,
488 "plDepends" => array(),
489 "plPriority" => 0,
490 "plSection" => array("administration"),
491 "plCategory" => array("phone" => array("description" => _("Phone hardware"),
492 "objectClass" => "goFonHardware")),
493 "plProvidedAcls"=> array(
494 "cn" => _("Name"),
495 "base" => _("Base"),
496 "description" => _("Description"),
497 "goFonType" => _("SIP Mode"),
498 "goFonDmtfMode" => _("SIP DTMF mode"),
499 "goFonDefaultIP" => _("SIP Default ip"),
500 "goFonQualify" => _("SIP Qualify"),
501 "goFonAuth" => _("IAX authentication type"),
502 "goFonSecret" => _("IAX secret"),
503 "goFonAccountCode" => _("IAX account code"),
504 "goFonTrunk" => _("IAX trunk lines"),
505 "goFonPermit" => _("IAX permit settings"),
506 "goFonDeny" => _("IAX deny settings"),
507 "goFonMSN" => _("CAPI MSN"),
508 "categorie" => _("Hardware type"))
509 ));
510 }
513 }
515 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
516 ?>