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, 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 $smarty->assign("netconfig", $this->netConfigDNS->execute());
229 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
230 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
231 }
233 function remove_from_parent()
234 {
235 $ldap= $this->config->get_ldap_link();
236 $ldap->cd($this->config->current['BASE']);
238 $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
239 while ($attr = $ldap->fetch()){
240 print_red(sprintf(_("Can't delete because there are user which are depending on this phone. One of them is user '%s'."),
241 ($attr['uid'][0]." - ".$attr['cn'][0])));
242 return;
243 }
245 $this->netConfigDNS->remove_from_parent();
246 $ldap->rmdir($this->dn);
247 show_ldap_error($ldap->get_error(), _("Removing phone failed"));
248 $this->handle_post_events("remove");
250 /* Delete references to object groups */
251 $ldap->cd ($this->config->current['BASE']);
252 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
253 while ($ldap->fetch()){
254 $og= new ogroup($this->config, $ldap->getDN());
255 unset($og->member[$this->dn]);
256 $og->save ();
257 }
258 }
261 /* Save data to object */
262 function save_object()
263 {
264 plugin::save_object();
265 $this->netConfigDNS->save_object();
266 /* Save base, since this is no LDAP attribute */
267 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
268 $this->base= $_POST['base'];
269 }
270 }
273 /* Check supplied data */
274 function check()
275 {
276 /* Call common method to give check the hook */
277 $message= plugin::check();
278 $message= array_merge($message, $this->netConfigDNS->check());
280 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
282 /* To check for valid ip*/
283 if($this->netConfigDNS->ipHostNumber == ""){
284 $message[]= _("The required field IP address is empty.");
285 } else {
286 if (!is_ip($this->netConfigDNS->ipHostNumber)){
287 $message[]= _("The field IP address contains an invalid address.");
288 }
289 }
291 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
292 $message[]= _("The required field 'Phone name' is not set.");
293 }
294 if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
295 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
296 }
298 $ui= get_userinfo();
299 $acl= get_permissions ($this->dn, $ui->subtreeACL);
300 $acl= get_module_permission($acl, "phone", $this->dn);
301 if (chkacl($acl, "create") != ""){
302 $message[]= _("You have no permissions to create a phone on this 'Base'.");
303 }
305 if ($this->orig_dn != $this->dn){
306 $ldap= $this->config->get_ldap_link();
307 $ldap->cd ($this->base);
308 $ldap->search ("(cn=".$this->cn.")", array("cn"));
309 if ($ldap->count() != 0){
310 while ($attrs= $ldap->fetch()){
311 if ($attrs['dn'] != $this->orig_dn){
312 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
313 break;
314 }
315 }
316 }
317 }
319 return ($message);
320 }
323 /* Save to LDAP */
324 function save()
325 {
326 plugin::save();
329 /* only to define which attrs to save*/
330 $mode = $this->attrs['selected_categorie'];
332 /* Remove all unwanted attrs */
333 foreach($this->attributes as $att){
335 /* Check all attributes, if they are needed for this type of phone */
336 if(!in_array($att,$this->usedattrs[$mode])){
337 $this->attrs[$att] = array();
338 }
340 /* Test rights of this user ... */
341 if(chkacl($this->acl,$att)!=""){
342 unset($this->attrs[$att]);
343 }
344 }
346 /* unset the categorie*/
347 unset($this->attrs['selected_categorie']);
349 /* Remove all empty values */
350 if ($this->orig_dn == 'new'){
351 $attrs= array();
352 foreach ($this->attrs as $key => $val){
353 if (is_array($val) && count($val) == 0){
354 continue;
355 }
356 $attrs[$key]= $val;
357 }
358 $this->attrs= $attrs;
359 }
361 if($this->goFonDefaultIP!="dynamic"){
362 $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
363 }
365 $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
367 /* Write back to ldap */
368 $ldap= $this->config->get_ldap_link();
369 if ($this->orig_dn == 'new'){
370 $ldap->cd($this->config->current['BASE']);
371 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
372 $ldap->cd($this->dn);
373 $ldap->add($this->attrs);
374 $this->handle_post_events("add");
375 } else {
376 if ($this->orig_dn != $this->dn){
377 $this->move($this->orig_dn, $this->dn);
378 }
380 $ldap->cd($this->dn);
381 $this->cleanup();
382 $ldap->modify ($this->attrs);
384 // $user_phone_reload
385 $ldap->cd ($this->config->current['BASE']);
386 $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
387 if($user_phone_assignment){
388 $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
389 $usertab->by_object['phoneAccount']->is_modified = true;
390 $usertab->save();
391 unset($usertab);
392 }
393 $this->handle_post_events("modify");
394 }
395 $this->netConfigDNS->cn = $this->cn;
396 $this->netConfigDNS->save($this->dn);
397 show_ldap_error($ldap->get_error(), _("Saving phone failed"));
399 /* Optionally execute a command after we're done */
400 $this->postcreate();
401 }
403 }
405 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
406 ?>