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 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);
196 $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
198 /* Create Arrays for samrty select boxes */
199 $smarty->assign("categories", array("SIP","IAX","CAPI"));
200 $smarty->assign("goFonTypes", array("peer" =>"peer" ,"user" =>"user" ,"friend" =>"friend"));
201 $smarty->assign("goFonDmtfModes", array("inband" =>"inband" ,"rfc2833"=>"rfc2833" ,"info" =>"info"));
202 $smarty->assign("goFonAuths", array("plaintext" =>"plaintext" ,"md5" =>"md5" /*,"rsa" =>"rsa"*/));
203 $smarty->assign("goFonTrunks", array("yes" =>_("yes") ,"no" => _("no")));
205 /* deativate all fields that are not used by the specified type */
206 foreach($this->attributes as $att){
207 if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
208 $smarty->assign($att."USED", "disabled" );
209 $smarty->assign($att, "");
210 }else{
211 $smarty->assign($att."USED", "" );
212 $smarty->assign($att, $this->$att);
213 }
214 }
216 $smarty->assign("selected_categorie",$this->selected_categorie);
218 /* Assign attributes */
219 foreach ($this->attributes as $attr){
220 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
221 }
222 $smarty->assign("base_select", $this->base);
223 $smarty->assign("baseACL", chkacl($this->acl,"base"));
225 $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
227 /* Show main page */
228 $this->netConfigDNS->cn= $this->cn;
229 $smarty->assign("netconfig", $this->netConfigDNS->execute());
230 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
231 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
232 }
234 function remove_from_parent()
235 {
236 $ldap= $this->config->get_ldap_link();
237 $ldap->cd($this->config->current['BASE']);
239 $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
240 while ($attr = $ldap->fetch()){
241 print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
242 ($attr['uid'][0]." - ".$attr['cn'][0])));
243 return;
244 }
246 $this->netConfigDNS->remove_from_parent();
247 $ldap->rmdir($this->dn);
248 show_ldap_error($ldap->get_error(), _("Removing phone failed"));
249 $this->handle_post_events("remove");
251 /* Delete references to object groups */
252 $ldap->cd ($this->config->current['BASE']);
253 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
254 while ($ldap->fetch()){
255 $og= new ogroup($this->config, $ldap->getDN());
256 unset($og->member[$this->dn]);
257 $og->save ();
258 }
259 }
262 /* Save data to object */
263 function save_object()
264 {
265 plugin::save_object();
266 $this->netConfigDNS->save_object();
267 /* Save base, since this is no LDAP attribute */
268 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
269 $this->base= $_POST['base'];
270 }
271 }
274 /* Check supplied data */
275 function check()
276 {
277 /* Call common method to give check the hook */
278 $message= plugin::check();
279 $message= array_merge($message, $this->netConfigDNS->check());
281 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
283 /* To check for valid ip*/
284 if($this->netConfigDNS->ipHostNumber == ""){
285 $message[]= _("The required field IP address is empty.");
286 } else {
287 if (!is_ip($this->netConfigDNS->ipHostNumber)){
288 $message[]= _("The field IP address contains an invalid address.");
289 }
290 }
292 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
293 $message[]= _("The required field 'Phone name' is not set.");
294 }
295 if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
296 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
297 }
299 $ui= get_userinfo();
300 $acl= get_permissions ($this->dn, $ui->subtreeACL);
301 $acl= get_module_permission($acl, "phone", $this->dn);
302 if (chkacl($acl, "create") != ""){
303 $message[]= _("You have no permissions to create a phone on this 'Base'.");
304 }
306 if ($this->orig_dn != $this->dn){
307 $ldap= $this->config->get_ldap_link();
308 $ldap->cd ($this->base);
309 $ldap->search ("(cn=".$this->cn.")", array("cn"));
310 if ($ldap->count() != 0){
311 while ($attrs= $ldap->fetch()){
312 if ($attrs['dn'] != $this->orig_dn){
313 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
314 break;
315 }
316 }
317 }
318 }
320 return ($message);
321 }
324 /* Save to LDAP */
325 function save()
326 {
327 plugin::save();
330 /* only to define which attrs to save*/
331 $mode = $this->attrs['selected_categorie'];
333 /* Remove all unwanted attrs */
334 foreach($this->attributes as $att){
336 /* Check all attributes, if they are needed for this type of phone */
337 if(!in_array($att,$this->usedattrs[$mode])){
338 $this->attrs[$att] = array();
339 }
341 /* Test rights of this user ... */
342 if(chkacl($this->acl,$att)!=""){
343 unset($this->attrs[$att]);
344 }
345 }
347 /* unset the categorie*/
348 unset($this->attrs['selected_categorie']);
350 /* Remove all empty values */
351 if ($this->orig_dn == 'new'){
352 $attrs= array();
353 foreach ($this->attrs as $key => $val){
354 if (is_array($val) && count($val) == 0){
355 continue;
356 }
357 $attrs[$key]= $val;
358 }
359 $this->attrs= $attrs;
360 }
362 if($this->goFonDefaultIP!="dynamic"){
363 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
364 }
366 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
368 /* Write back to ldap */
369 $ldap= $this->config->get_ldap_link();
370 if ($this->orig_dn == 'new'){
371 $ldap->cd($this->config->current['BASE']);
372 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
373 $ldap->cd($this->dn);
374 $ldap->add($this->attrs);
375 $this->handle_post_events("add");
376 } else {
377 if ($this->orig_dn != $this->dn){
378 $this->move($this->orig_dn, $this->dn);
379 }
381 $ldap->cd($this->dn);
382 $this->cleanup();
383 $ldap->modify ($this->attrs);
385 // $user_phone_reload
386 $ldap->cd ($this->config->current['BASE']);
387 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
388 if($user_phone_assignment){
389 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
390 $usertab->by_object['phoneAccount']->is_modified = true;
391 $usertab->save();
392 unset($usertab);
393 }
394 $this->handle_post_events("modify");
395 }
396 $this->netConfigDNS->cn = $this->cn;
397 $this->netConfigDNS->save($this->dn);
398 show_ldap_error($ldap->get_error(), _("Saving phone failed"));
400 /* Optionally execute a command after we're done */
401 $this->postcreate();
402 }
404 }
406 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
407 ?>