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;
36 /* attribute list for save action */
37 var $attributes= array("cn", "description",
38 "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
39 "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
40 "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie","goFonPermit","goFonDeny"
41 );
43 /* this array defines which attributes are schown / saved for the different type of phones */
44 var $usedattrs = array( "0"=>array("cn", "description",
45 "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
46 "goFonQualify"),
47 "1"=>array("cn", "description",
48 "goFonType","goFonHost","goFonDefaultIP",
49 "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
50 "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
51 "2"=>array("cn", "description", "goFonMSN"));
54 var $objectclasses= array("top", "goFonHardware");
56 function phonegeneric ($config, $dn= NULL, $parent= NULL)
57 {
58 plugin::plugin ($config, $dn, $parent);
59 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
61 /* Set base */
62 if ($this->dn == "new"){
63 $ui= get_userinfo();
64 $this->base= dn2base($ui->dn);
65 $this->cn= "";
66 } else {
67 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
68 }
70 if($this->goFonMSN != ""){
71 $this->selected_categorie = 2;
72 }elseif($this->goFonAccountCode != ""){
73 $this->selected_categorie = 1 ;
75 if(isset($this->attrs['goFonPermit']['count'])){
76 unset ($this->attrs['goFonPermit']['count']);
77 $this->goFonPermit=$this->attrs['goFonPermit'];
78 }
80 if(isset($this->attrs['goFonDeny']['count'])){
81 unset ($this->attrs['goFonDeny']['count']) ;
82 $this->goFonDeny=$this->attrs['goFonDeny'];
83 }
85 } else {
86 $this->selected_categorie = 0;
87 }
89 if($this->goFonDefaultIP!="dynamic"){
90 $this->goFonDefaultIP = "network";
91 }
93 /* Save dn for later references */
94 $this->orig_dn= $this->dn;
95 }
97 function set_acl_base($base)
98 {
99 plugin::set_acl_base($base);
100 $this->netConfigDNS->set_acl_base($base);
101 }
103 function set_acl_category($cat)
104 {
105 plugin::set_acl_category($cat);
106 $this->netConfigDNS->set_acl_category($cat);
107 }
110 function execute()
111 {
112 /* Call parent execute */
113 plugin::execute();
115 /* Do we represent a valid phone? */
116 if (!$this->is_account && $this->parent == NULL){
117 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
118 _("This 'dn' has no phone features.")."</b>";
119 return($display);
120 }
122 /* Base select dialog */
123 $once = true;
124 foreach($_POST as $name => $value){
125 if(preg_match("/^chooseBase/",$name) && $once){
126 $once = false;
127 $this->dialog = new baseSelectDialog($this->config,$this);
128 $this->dialog->setCurrentBase($this->base);
129 }
130 }
132 /* Dialog handling */
133 if(is_object($this->dialog)){
134 /* Must be called before save_object */
135 $this->dialog->save_object();
137 if($this->dialog->isClosed()){
138 $this->dialog = false;
139 }elseif($this->dialog->isSelected()){
140 $this->base = $this->dialog->isSelected();
141 $this->dialog= false;
142 }else{
143 return($this->dialog->execute());
144 }
145 }
147 /* handle Permit Add*/
148 if(isset($_POST['goFonPermitAdd']) && $this->acl_is_writeable("goFonPermit")){
149 if(isset($_POST['goFonPermitNew'])){
150 if(is_string($this->goFonPermit)){
151 $this->goFonPermit=array();
152 }
153 $new = $_POST['goFonPermitNew'];
154 if(strlen($new)> 1) {
155 $this->goFonPermit[]= $new;
156 }
157 }
158 }
160 /* handle Deny Add*/
161 if(isset($_POST['goFonDenyAdd']) && $this->acl_is_writeable("goFonDeny")){
162 if(isset($_POST['goFonDenyNew'])){
163 if(is_string($this->goFonDeny)){
164 $this->goFonDeny=array();
165 }
166 $new = $_POST['goFonDenyNew'];
167 if(strlen($new)> 1) {
168 $this->goFonDeny[]= $new;
169 }
170 }
171 }
173 /* Handle Permit Deletion*/
174 if(isset($_POST['goFonPermitDel']) && $this->acl_is_writeable("goFonPermit")){
175 if(isset($_POST['goFonPermitS'])){
176 if(is_string($this->goFonPermit)){
177 $this->goFonPermit=array();
178 }
179 $new = $_POST['goFonPermitS'];
180 $tmp = array_flip($this->goFonPermit);
181 unset($tmp[$new]);
182 $this->goFonPermit=array();
183 foreach(array_flip($tmp) as $tm){
184 $this->goFonPermit[]=$tm;
185 }
186 }
187 }
190 /* Handle Permit Deletion*/
191 if(isset($_POST['goFonDenyDel']) && $this->acl_is_writeable("goFonDeny")){
192 if(isset($_POST['goFonDenyS'])){
193 if(is_string($this->goFonDeny)){
194 $this->goFonDeny=array();
195 }
196 $new = $_POST['goFonDenyS'];
197 $tmp = array_flip($this->goFonDeny);
198 unset($tmp[$new]);
199 $this->goFonDeny=array();
200 foreach(array_flip($tmp) as $tm){
201 $this->goFonDeny[]=$tm;
202 }
203 }
204 }
206 /* Fill templating stuff */
207 $smarty= get_smarty();
209 /* Assign base ACL */
210 $baseACL = $this->getacl("base");
211 if(!$this->acl_is_moveable()) {
212 $baseACL = preg_replace("/w/","",$baseACL);
213 }
214 $smarty->assign("baseACL", $baseACL);
215 $smarty->assign("bases", $this->get_allowed_bases());
217 $tmp = $this->plInfo();
218 foreach($tmp['plProvidedAcls'] as $name => $translation){
219 $smarty->assign($name."ACL",$this->getacl($name));
220 }
222 $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
224 /* Create Arrays for samrty select boxes */
225 $smarty->assign("categories", array("SIP","IAX","CAPI"));
226 $smarty->assign("goFonTypes", array("peer" =>"peer" ,"user" =>"user" ,"friend" =>"friend"));
227 $smarty->assign("goFonDmtfModes", array("inband" =>"inband" ,"rfc2833"=>"rfc2833" ,"info" =>"info"));
228 $smarty->assign("goFonAuths", array("plaintext" =>"plaintext" ,"md5" =>"md5" /*,"rsa" =>"rsa"*/));
229 $smarty->assign("goFonTrunks", array("yes" =>_("yes") ,"no" => _("no")));
231 /* deativate all fields that are not used by the specified type */
232 foreach($this->attributes as $att){
233 if((!in_array($att,$this->usedattrs[$this->selected_categorie]))){
234 $smarty->assign($att."USED", "disabled" );
235 $smarty->assign($att, "");
236 }else{
237 $smarty->assign($att."USED", "" );
238 $smarty->assign($att, $this->$att);
239 }
240 }
242 $smarty->assign("selected_categorie",$this->selected_categorie);
244 /* Assign attributes */
245 $smarty->assign("base_select", $this->base);
246 $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
248 /* Show main page */
249 $smarty->assign("netconfig", $this->netConfigDNS->execute());
250 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
251 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
252 }
254 function remove_from_parent()
255 {
256 if($this->acl_is_removeable()){
257 $ldap= $this->config->get_ldap_link();
258 $ldap->cd($this->config->current['BASE']);
260 $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
261 while ($attr = $ldap->fetch()){
262 print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
263 ($attr['uid'][0]." - ".$attr['cn'][0])));
264 return;
265 }
267 $this->netConfigDNS->remove_from_parent();
268 $ldap->rmdir($this->dn);
269 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system phone/generic with dn '%s' failed."),$this->dn));
270 $this->handle_post_events("remove");
272 /* Delete references to object groups */
273 $ldap->cd ($this->config->current['BASE']);
274 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
275 while ($ldap->fetch()){
276 $og= new ogroup($this->config, $ldap->getDN());
277 unset($og->member[$this->dn]);
278 $og->save ();
279 }
280 }
281 }
284 /* Save data to object */
285 function save_object()
286 {
287 plugin::save_object();
288 $this->netConfigDNS->save_object();
289 /* Save base, since this is no LDAP attribute */
290 if (isset($_POST['base']) && $this->acl_is_moveable()){
291 $this->base= $_POST['base'];
292 }
293 }
296 /* Check supplied data */
297 function check()
298 {
299 /* Call common method to give check the hook */
300 $message= plugin::check();
301 $message= array_merge($message, $this->netConfigDNS->check());
303 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
305 /* To check for valid ip*/
306 if($this->netConfigDNS->ipHostNumber == ""){
307 $message[]= _("The required field IP address is empty.");
308 } else {
309 if (!is_ip($this->netConfigDNS->ipHostNumber)){
310 $message[]= _("The field IP address contains an invalid address.");
311 }
312 }
314 if ($this->cn == ""){
315 $message[]= _("The required field 'Phone name' is not set.");
316 }
317 if ($this->cn == "0"){
318 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
319 }
321 $ui= get_userinfo();
322 if ($this->dn == "new" && $this->acl_is_createable()){
323 $message[]= _("You have no permissions to create a phone on this 'Base'.");
324 }
326 if ($this->orig_dn != $this->dn){
327 $ldap= $this->config->get_ldap_link();
328 $ldap->cd ($this->base);
329 $ldap->search ("(cn=".$this->cn.")", array("cn"));
330 if ($ldap->count() != 0){
331 while ($attrs= $ldap->fetch()){
332 if ($attrs['dn'] != $this->orig_dn){
333 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
334 break;
335 }
336 }
337 }
338 }
340 return ($message);
341 }
344 /* Save to LDAP */
345 function save()
346 {
347 plugin::save();
350 /* only to define which attrs to save*/
351 $mode = $this->attrs['selected_categorie'];
353 /* Remove all unwanted attrs */
354 foreach($this->attributes as $att){
356 /* Check all attributes, if they are needed for this type of phone */
357 if(!in_array($att,$this->usedattrs[$mode])){
358 $this->attrs[$att] = array();
359 }
360 }
362 /* unset the categorie*/
363 unset($this->attrs['selected_categorie']);
365 /* Remove all empty values */
366 if ($this->orig_dn == 'new'){
367 $attrs= array();
368 foreach ($this->attrs as $key => $val){
369 if (is_array($val) && count($val) == 0){
370 continue;
371 }
372 $attrs[$key]= $val;
373 }
374 $this->attrs= $attrs;
375 }
377 if($this->goFonDefaultIP!="dynamic"){
378 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
379 }
381 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
383 /* Write back to ldap */
384 $ldap= $this->config->get_ldap_link();
385 if ($this->orig_dn == 'new'){
386 $ldap->cd($this->config->current['BASE']);
387 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
388 $ldap->cd($this->dn);
389 $ldap->add($this->attrs);
390 $this->handle_post_events("add");
391 } else {
392 if ($this->orig_dn != $this->dn){
393 $this->move($this->orig_dn, $this->dn);
394 }
396 $ldap->cd($this->dn);
397 $this->cleanup();
398 $ldap->modify ($this->attrs);
400 // $user_phone_reload
401 $ldap->cd ($this->config->current['BASE']);
402 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
403 if($user_phone_assignment){
404 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
405 $usertab->by_object['phoneAccount']->is_modified = true;
406 $usertab->save();
407 unset($usertab);
408 }
409 $this->handle_post_events("modify");
410 }
411 $this->netConfigDNS->cn = $this->cn;
412 $this->netConfigDNS->save($this->dn);
413 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system phone/generic with dn '%s' failed."),$this->dn));
415 /* Optionally execute a command after we're done */
416 $this->postcreate();
417 }
420 /* Return plugin informations for acl handling
421 #FIXME FAIscript seams to ununsed within this class... */
422 function plInfo()
423 {
424 return (array(
425 "plShortName" => _("Generic"),
426 "plDescription" => _("Phone generic"),
427 "plSelfModify" => FALSE,
428 "plDepends" => array(),
429 "plPriority" => 0,
430 "plSection" => array("administration"),
431 "plCategory" => array("phone" => array("description" => _("Phone hardware"),
432 "objectClass" => "goFonHardware")),
433 "plProvidedAcls"=> array(
434 "cn" => _("Name"),
435 "description" => _("Description"),
436 "goFonType" => _("SIP Mode"),
437 "goFonDmtfMode" => _("SIP DTMF mode"),
438 "goFonDefaultIP" => _("SIP Default ip"),
439 "goFonQualify" => _("SIP Qualify"),
440 "goFonAuth" => _("IAX authentication type"),
441 "goFonSecret" => _("IAX secret"),
442 "goFonAccountCode" => _("IAX account code"),
443 "goFonTrunk" => _("IAX trunk lines"),
444 "goFonPermit" => _("IAX permit settings"),
445 "goFonDeny" => _("IAX deny settings"),
446 "goFonMSN" => _("CAPI MSN"),
447 "categorie" => _("Hardware type"))
448 ));
449 }
452 }
454 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
455 ?>