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->dn,$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 $smarty->assign("netconfig", $this->netConfigDNS->execute());
256 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
257 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
258 }
260 function remove_from_parent()
261 {
262 if($this->acl_is_removeable()){
263 $ldap= $this->config->get_ldap_link();
264 $ldap->cd($this->config->current['BASE']);
266 $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
267 while ($attr = $ldap->fetch()){
268 print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
269 ($attr['uid'][0]." - ".$attr['cn'][0])));
270 return;
271 }
273 $this->netConfigDNS->remove_from_parent();
274 $ldap->rmdir($this->dn);
276 new log("remove","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
278 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system phone/generic with dn '%s' failed."),$this->dn));
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=".$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.",ou=phones,ou=systems,".$this->base;
324 /* To check for valid ip*/
325 if($this->netConfigDNS->ipHostNumber == ""){
326 $message[]= _("The required field IP address is empty.");
327 } else {
328 if (!is_ip($this->netConfigDNS->ipHostNumber)){
329 $message[]= _("The field IP address contains an invalid address.");
330 }
331 }
333 if ($this->cn == ""){
334 $message[]= _("The required field 'Phone name' is not set.");
335 }
336 if ($this->cn == "0"){
337 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
338 }
340 if ($this->orig_dn != $this->dn){
341 $ldap= $this->config->get_ldap_link();
342 $ldap->cd ($this->base);
343 $ldap->search ("(cn=".$this->cn.")", array("cn"));
344 if ($ldap->count() != 0){
345 while ($attrs= $ldap->fetch()){
346 if ($attrs['dn'] != $this->orig_dn){
347 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
348 break;
349 }
350 }
351 }
352 }
354 return ($message);
355 }
358 /* Save to LDAP */
359 function save()
360 {
361 plugin::save();
364 /* only to define which attrs to save*/
365 $mode = $this->attrs['selected_categorie'];
367 /* Remove all unwanted attrs */
368 foreach($this->attributes as $att){
370 /* Check all attributes, if they are needed for this type of phone */
371 if(!in_array($att,$this->usedattrs[$mode])){
372 $this->attrs[$att] = array();
373 }
374 }
376 /* unset the categorie*/
377 unset($this->attrs['selected_categorie']);
379 /* Remove all empty values */
380 if ($this->orig_dn == 'new'){
381 $attrs= array();
382 foreach ($this->attrs as $key => $val){
383 if (is_array($val) && count($val) == 0){
384 continue;
385 }
386 $attrs[$key]= $val;
387 }
388 $this->attrs= $attrs;
389 }
391 if($this->goFonDefaultIP!="dynamic"){
392 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
393 }
395 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
397 /* Write back to ldap */
398 $ldap= $this->config->get_ldap_link();
399 if ($this->orig_dn == 'new'){
400 $ldap->cd($this->config->current['BASE']);
401 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
402 $ldap->cd($this->dn);
403 $ldap->add($this->attrs);
404 new log("create","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
405 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
406 } else {
407 if ($this->orig_dn != $this->dn){
408 $this->move($this->orig_dn, $this->dn);
409 }
411 $ldap->cd($this->dn);
412 $this->cleanup();
413 $ldap->modify ($this->attrs);
414 new log("modify","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
416 // $user_phone_reload
417 $ldap->cd ($this->config->current['BASE']);
418 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
419 if($user_phone_assignment){
420 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
421 $usertab->by_object['phoneAccount']->is_modified = true;
422 $usertab->save();
423 unset($usertab);
424 }
425 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
426 }
427 $this->netConfigDNS->cn = $this->cn;
428 $this->netConfigDNS->save($this->dn);
429 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system phone/generic with dn '%s' failed."),$this->dn));
430 }
433 /* Return plugin informations for acl handling
434 #FIXME FAIscript seams to ununsed within this class... */
435 function plInfo()
436 {
437 return (array(
438 "plShortName" => _("Generic"),
439 "plDescription" => _("Phone generic"),
440 "plSelfModify" => FALSE,
441 "plDepends" => array(),
442 "plPriority" => 0,
443 "plSection" => array("administration"),
444 "plCategory" => array("phone" => array("description" => _("Phone hardware"),
445 "objectClass" => "goFonHardware")),
446 "plProvidedAcls"=> array(
447 "cn" => _("Name"),
448 "base" => _("Base"),
449 "description" => _("Description"),
450 "goFonType" => _("SIP Mode"),
451 "goFonDmtfMode" => _("SIP DTMF mode"),
452 "goFonDefaultIP" => _("SIP Default ip"),
453 "goFonQualify" => _("SIP Qualify"),
454 "goFonAuth" => _("IAX authentication type"),
455 "goFonSecret" => _("IAX secret"),
456 "goFonAccountCode" => _("IAX account code"),
457 "goFonTrunk" => _("IAX trunk lines"),
458 "goFonPermit" => _("IAX permit settings"),
459 "goFonDeny" => _("IAX deny settings"),
460 "goFonMSN" => _("CAPI MSN"),
461 "categorie" => _("Hardware type"))
462 ));
463 }
466 }
468 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
469 ?>