62322f574abf0ed70d3e05f3967fbcec5caf37e2
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)
57 {
58 plugin::plugin ($config, $dn);
59 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
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 execute()
98 {
99 /* Call parent execute */
100 plugin::execute();
102 /* Do we represent a valid phone? */
103 if (!$this->is_account && $this->parent == NULL){
104 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
105 _("This 'dn' has no phone features.")."</b>";
106 return($display);
107 }
109 /* Base select dialog */
110 $once = true;
111 foreach($_POST as $name => $value){
112 if(preg_match("/^chooseBase/",$name) && $once){
113 $once = false;
114 $this->dialog = new baseSelectDialog($this->config);
115 $this->dialog->setCurrentBase($this->base);
116 }
117 }
119 /* Dialog handling */
120 if(is_object($this->dialog)){
121 /* Must be called before save_object */
122 $this->dialog->save_object();
124 if($this->dialog->isClosed()){
125 $this->dialog = false;
126 }elseif($this->dialog->isSelected()){
127 $this->base = $this->dialog->isSelected();
128 $this->dialog= false;
129 }else{
130 return($this->dialog->execute());
131 }
132 }
134 /* handle Permit Add*/
135 if(isset($_POST['goFonPermitAdd'])){
136 if(isset($_POST['goFonPermitNew'])){
137 if(is_string($this->goFonPermit)){
138 $this->goFonPermit=array();
139 }
140 $new = $_POST['goFonPermitNew'];
141 if(strlen($new)> 1) {
142 $this->goFonPermit[]= $new;
143 }
144 }
145 }
147 /* handle Deny Add*/
148 if(isset($_POST['goFonDenyAdd'])){
149 if(isset($_POST['goFonDenyNew'])){
150 if(is_string($this->goFonDeny)){
151 $this->goFonDeny=array();
152 }
153 $new = $_POST['goFonDenyNew'];
154 if(strlen($new)> 1) {
155 $this->goFonDeny[]= $new;
156 }
157 }
158 }
160 /* Handle Permit Deletion*/
161 if(isset($_POST['goFonPermitDel'])){
162 if(isset($_POST['goFonPermitS'])){
163 if(is_string($this->goFonPermit)){
164 $this->goFonPermit=array();
165 }
166 $new = $_POST['goFonPermitS'];
167 $tmp = array_flip($this->goFonPermit);
168 unset($tmp[$new]);
169 $this->goFonPermit=array();
170 foreach(array_flip($tmp) as $tm){
171 $this->goFonPermit[]=$tm;
172 }
173 }
174 }
177 /* Handle Permit Deletion*/
178 if(isset($_POST['goFonDenyDel'])){
179 if(isset($_POST['goFonDenyS'])){
180 if(is_string($this->goFonDeny)){
181 $this->goFonDeny=array();
182 }
183 $new = $_POST['goFonDenyS'];
184 $tmp = array_flip($this->goFonDeny);
185 unset($tmp[$new]);
186 $this->goFonDeny=array();
187 foreach(array_flip($tmp) as $tm){
188 $this->goFonDeny[]=$tm;
189 }
190 }
191 }
193 /* Fill templating stuff */
194 $smarty= get_smarty();
195 $smarty->assign("bases", $this->config->idepartments);
197 /* Create Arrays for samrty select boxes */
198 $smarty->assign("categories", array("SIP","IAX","CAPI"));
199 $smarty->assign("goFonTypes", array("peer" =>"peer" ,"user" =>"user" ,"friend" =>"friend"));
200 $smarty->assign("goFonDmtfModes", array("inband" =>"inband" ,"rfc2833"=>"rfc2833" ,"info" =>"info"));
201 $smarty->assign("goFonAuths", array("plaintext" =>"plaintext" ,"md5" =>"md5" /*,"rsa" =>"rsa"*/));
202 $smarty->assign("goFonTrunks", array("yes" =>_("yes") ,"no" => _("no")));
204 /* deativate all fields that are not used by the specified type */
205 foreach($this->attributes as $att){
206 if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
207 $smarty->assign($att."USED", "disabled" );
208 $smarty->assign($att, "");
209 }else{
210 $smarty->assign($att."USED", "" );
211 $smarty->assign($att, $this->$att);
212 }
213 }
215 $smarty->assign("selected_categorie",$this->selected_categorie);
217 /* Assign attributes */
218 foreach ($this->attributes as $attr){
219 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
220 }
221 $smarty->assign("base_select", $this->base);
223 $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
225 /* Show main page */
226 $smarty->assign("netconfig", $this->netConfigDNS->execute());
227 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
228 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
229 }
231 function remove_from_parent()
232 {
233 $ldap= $this->config->get_ldap_link();
234 $ldap->cd($this->config->current['BASE']);
236 $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
237 while ($attr = $ldap->fetch()){
238 print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
239 ($attr['uid'][0]." - ".$attr['cn'][0])));
240 return;
241 }
243 $this->netConfigDNS->remove_from_parent();
244 $ldap->rmdir($this->dn);
245 show_ldap_error($ldap->get_error());
246 $this->handle_post_events("remove");
248 /* Delete references to object groups */
249 $ldap->cd ($this->config->current['BASE']);
250 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
251 while ($ldap->fetch()){
252 $og= new ogroup($this->config, $ldap->getDN());
253 unset($og->member[$this->dn]);
254 $og->save ();
255 }
256 }
259 /* Save data to object */
260 function save_object()
261 {
262 plugin::save_object();
263 $this->netConfigDNS->save_object();
264 /* Save base, since this is no LDAP attribute */
265 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
266 $this->base= $_POST['base'];
267 }
268 }
271 /* Check supplied data */
272 function check()
273 {
274 $message= $this->netConfigDNS->check();
275 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
277 /* To check for valid ip*/
278 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
280 // if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
281 // if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
282 // $message[]= _("Wrong IP format in field goFonDefaultIP.");
283 // }
284 // }
286 // /* Check for valid number */
287 // if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
288 // if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
289 // $message[]= _("The given value for 'Response timeout' is not a valid number.");
290 // }
291 // }
293 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
294 $message[]= _("The required field 'Phone name' is not set.");
295 }
296 if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
297 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
298 }
300 $ui= get_userinfo();
301 $acl= get_permissions ($this->dn, $ui->subtreeACL);
302 $acl= get_module_permission($acl, "phone", $this->dn);
303 if (chkacl($acl, "create") != ""){
304 $message[]= _("You have no permissions to create a phone on this 'Base'.");
305 }
307 if ($this->orig_dn != $this->dn){
308 $ldap= $this->config->get_ldap_link();
309 $ldap->cd ($this->base);
310 $ldap->search ("(cn=".$this->cn.")", array("cn"));
311 if ($ldap->count() != 0){
312 while ($attrs= $ldap->fetch()){
313 if ($attrs['dn'] != $this->orig_dn){
314 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
315 break;
316 }
317 }
318 }
319 }
321 return ($message);
322 }
325 /* Save to LDAP */
326 function save()
327 {
328 plugin::save();
331 /* only to define which attrs to save*/
332 $mode = $this->attrs['selected_categorie'];
334 /* Remove all unwanted attrs */
335 foreach($this->attributes as $att){
337 /* Check all attributes, if they are needed for this type of phone */
338 if(!in_array($att,$this->usedattrs[$mode])){
339 $this->attrs[$att] = array();
340 }
342 /* Test rights of this user ... */
343 if(chkacl($this->acl,$att)!=""){
344 unset($this->attrs[$att]);
345 }
346 }
348 /* unset the categorie*/
349 unset($this->attrs['selected_categorie']);
351 /* Remove all empty values */
352 if ($this->orig_dn == 'new'){
353 $attrs= array();
354 foreach ($this->attrs as $key => $val){
355 if (is_array($val) && count($val) == 0){
356 continue;
357 }
358 $attrs[$key]= $val;
359 }
360 $this->attrs= $attrs;
361 }
363 if($this->goFonDefaultIP!="dynamic"){
364 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
365 }
367 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
369 /* Write back to ldap */
370 $ldap= $this->config->get_ldap_link();
371 if ($this->orig_dn == 'new'){
372 $ldap->cd($this->config->current['BASE']);
373 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
374 $ldap->cd($this->dn);
375 $ldap->add($this->attrs);
376 $this->handle_post_events("add");
377 } else {
378 if ($this->orig_dn != $this->dn){
379 $this->move($this->orig_dn, $this->dn);
380 }
382 $ldap->cd($this->dn);
383 $this->cleanup();
384 $ldap->modify ($this->attrs);
386 // $user_phone_reload
387 $ldap->cd ($this->config->current['BASE']);
388 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
389 if($user_phone_assignment){
390 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
391 $usertab->by_object['phoneAccount']->is_modified = true;
392 $usertab->save();
393 unset($usertab);
394 }
395 $this->handle_post_events("modify");
396 }
397 $this->netConfigDNS->cn = $this->cn;
398 $this->netConfigDNS->save($this->dn);
400 show_ldap_error($ldap->get_error());
401 /* Optionally execute a command after we're done */
402 $this->postcreate();
403 }
405 }
407 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
408 ?>