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=".LDAP::prepare4filter($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.",".get_ou('phoneou').$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 (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
333 $message[]= _("The field IP address contains an invalid address.");
334 }
335 }
337 /* Check if given name is a valid host/dns name */
338 if(!tests::is_dns_name($this->cn) ){
339 $message[] = _("Please specify a valid name for this object.");
340 }
342 if ($this->cn == ""){
343 $message[]= _("The required field 'Phone name' is not set.");
344 }
345 if ($this->cn == "0"){
346 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
347 }
349 if ($this->orig_dn != $this->dn){
350 $ldap= $this->config->get_ldap_link();
351 $ldap->cd ($this->base);
352 $ldap->search ("(cn=".$this->cn.")", array("cn"));
353 if ($ldap->count() != 0){
354 while ($attrs= $ldap->fetch()){
356 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
357 continue;
358 }
360 if ($attrs['dn'] != $this->orig_dn){
361 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
362 break;
363 }
364 }
365 }
366 }
368 return ($message);
369 }
372 /* Save to LDAP */
373 function save()
374 {
375 plugin::save();
378 /* only to define which attrs to save*/
379 $mode = $this->selected_categorie;
381 /* Remove all unwanted attrs */
382 foreach($this->attributes as $att){
384 /* Check all attributes, if they are needed for this type of phone */
385 if(!in_array($att,$this->usedattrs[$mode])){
386 $this->attrs[$att] = array();
387 }
388 }
390 /* unset the categorie*/
391 unset($this->attrs['selected_categorie']);
393 /* Remove all empty values */
394 if ($this->orig_dn == 'new'){
395 $attrs= array();
396 foreach ($this->attrs as $key => $val){
397 if (is_array($val) && count($val) == 0){
398 continue;
399 }
400 $attrs[$key]= $val;
401 }
402 $this->attrs= $attrs;
403 }
405 if($this->goFonDefaultIP!="dynamic"){
406 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
407 }
409 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
411 /* Write back to ldap */
412 $ldap= $this->config->get_ldap_link();
413 if ($this->orig_dn == 'new'){
414 $ldap->cd($this->config->current['BASE']);
415 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
416 $ldap->cd($this->dn);
417 $ldap->add($this->attrs);
418 new log("create","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
419 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
420 } else {
421 if ($this->orig_dn != $this->dn){
422 $this->move($this->orig_dn, $this->dn);
423 }
425 $ldap->cd($this->dn);
426 $this->cleanup();
427 $ldap->modify ($this->attrs);
428 new log("modify","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
430 // $user_phone_reload
431 $ldap->cd ($this->config->current['BASE']);
432 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
433 if($user_phone_assignment){
434 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
435 $usertab->by_object['phoneAccount']->is_modified = true;
436 $usertab->save();
437 unset($usertab);
438 }
439 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
440 }
441 $this->netConfigDNS->cn = $this->cn;
442 $this->netConfigDNS->save();
443 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system phone/generic with dn '%s' failed."),$this->dn));
444 }
447 /* Display generic part for server copy & paste */
448 function getCopyDialog()
449 {
450 $vars = array("cn");
451 $smarty = get_smarty();
452 $smarty->assign("cn" ,$this->cn);
453 $smarty->assign("object","phone");
454 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
455 $ret = array();
456 $ret['string'] = $str;
457 $ret['status'] = "";
458 return($ret);
459 }
462 function saveCopyDialog()
463 {
464 if(isset($_POST['cn'])){
465 $this->cn = $_POST['cn'];
466 }
467 }
470 function PrepareForCopyPaste($source)
471 {
472 plugin::PrepareForCopyPaste($source);
473 if(isset($source['macAddress'][0])){
474 $this->netConfigDNS->macAddress = $source['macAddress'][0];
475 }
476 if(isset($source['ipHostNumber'][0])){
477 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
478 }
480 $source_o = new phoneGeneric($this->config, $source['dn']);
481 $this->selected_categorie = $source_o->selected_categorie;
482 }
485 /* Return plugin informations for acl handling
486 #FIXME FAIscript seams to ununsed within this class... */
487 static function plInfo()
488 {
489 return (array(
490 "plShortName" => _("Generic"),
491 "plDescription" => _("Phone generic"),
492 "plSelfModify" => FALSE,
493 "plDepends" => array(),
494 "plPriority" => 0,
495 "plSection" => array("administration"),
496 "plCategory" => array("phone" => array("description" => _("Phone hardware"),
497 "objectClass" => "goFonHardware")),
498 "plProvidedAcls"=> array(
499 "cn" => _("Name"),
500 "base" => _("Base"),
501 "description" => _("Description"),
502 "goFonType" => _("SIP Mode"),
503 "goFonDmtfMode" => _("SIP DTMF mode"),
504 "goFonDefaultIP" => _("SIP Default ip"),
505 "goFonQualify" => _("SIP Qualify"),
506 "goFonAuth" => _("IAX authentication type"),
507 "goFonSecret" => _("IAX secret"),
508 "goFonAccountCode" => _("IAX account code"),
509 "goFonTrunk" => _("IAX trunk lines"),
510 "goFonPermit" => _("IAX permit settings"),
511 "goFonDeny" => _("IAX deny settings"),
512 "goFonMSN" => _("CAPI MSN"),
513 "categorie" => _("Hardware type"))
514 ));
515 }
518 }
520 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
521 ?>