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 $macAddress = "";
18 var $ipHostNumber = "";
19 var $description = "";
20 var $orig_dn = "";
21 var $goFonType = "";
22 var $goFonDmtfMode = "";
23 var $goFonHost = "";
24 var $goFonDefaultIP = "";
25 var $goFonQualify = "";
26 var $goFonAuth = "";
27 var $goFonSecret = "";
28 var $goFonInkeys = "";
29 var $goFonPermit = array();
30 var $goFonDeny = array();
31 var $goFonOutkey = "";
32 var $goFonTrunk = "";
33 var $goFonAccountCode = "";
34 var $goFonMSN = "";
35 var $selected_categorie = 0;
37 /* attribute list for save action */
38 var $attributes= array("cn", "description", "macAddress", "ipHostNumber"
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", "macAddress", "ipHostNumber",
46 "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
47 "goFonQualify"),
48 "1"=>array("cn", "description", "macAddress", "ipHostNumber",
49 "goFonType","goFonHost","goFonDefaultIP",
50 "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
51 "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
52 "2"=>array("cn", "description", "macAddress", "ipHostNumber","goFonMSN"));
55 var $objectclasses= array("top", "goFonHardware");
57 function phonegeneric ($config, $dn= NULL)
58 {
59 plugin::plugin ($config, $dn);
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 /* Save dn for later references */
90 $this->orig_dn= $this->dn;
91 }
93 function execute()
94 {
97 /* Do we represent a valid phone? */
98 if (!$this->is_account && $this->parent == NULL){
99 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
100 _("This 'dn' has no phone features.")."</b>";
101 return($display);
102 }
104 /* handle Permit Add*/
105 if(isset($_POST['goFonPermitAdd'])){
106 if(isset($_POST['goFonPermitNew'])){
107 if(is_string($this->goFonPermit)){
108 $this->goFonPermit=array();
109 }
110 $new = $_POST['goFonPermitNew'];
111 if(strlen($new)> 1) {
112 $this->goFonPermit[]= $new;
113 }
114 }
115 }
117 /* handle Deny Add*/
118 if(isset($_POST['goFonDenyAdd'])){
119 if(isset($_POST['goFonDenyNew'])){
120 if(is_string($this->goFonDeny)){
121 $this->goFonDeny=array();
122 }
123 $new = $_POST['goFonDenyNew'];
124 if(strlen($new)> 1) {
125 $this->goFonDeny[]= $new;
126 }
127 }
128 }
130 /* Handle Permit Deletion*/
131 if(isset($_POST['goFonPermitDel'])){
132 if(isset($_POST['goFonPermitS'])){
133 if(is_string($this->goFonPermit)){
134 $this->goFonPermit=array();
135 }
136 $new = $_POST['goFonPermitS'];
137 $tmp = array_flip($this->goFonPermit);
138 unset($tmp[$new]);
139 $this->goFonPermit=array();
140 foreach(array_flip($tmp) as $tm){
141 $this->goFonPermit[]=$tm;
142 }
143 }
144 }
147 /* Handle Permit Deletion*/
148 if(isset($_POST['goFonDenyDel'])){
149 if(isset($_POST['goFonDenyS'])){
150 if(is_string($this->goFonDeny)){
151 $this->goFonDeny=array();
152 }
153 $new = $_POST['goFonDenyS'];
154 $tmp = array_flip($this->goFonDeny);
155 unset($tmp[$new]);
156 $this->goFonDeny=array();
157 foreach(array_flip($tmp) as $tm){
158 $this->goFonDeny[]=$tm;
159 }
160 }
161 }
163 /* Fill templating stuff */
164 $smarty= get_smarty();
165 $smarty->assign("bases", $this->config->idepartments);
167 /* Create Arrays for samrty select boxes */
168 $smarty->assign("categories", array("SIP - phones","IAX - phones","CAPI - phones"));
169 $smarty->assign("goFonTypes", array("peer" =>"peer" ,"user" =>"user" ,"friend" =>"friend"));
170 $smarty->assign("goFonDmtfModes", array("inband" =>"inband" ,"rfc2833"=>"rfc2833" ,"info" =>"info"));
171 $smarty->assign("goFonAuths", array("plaintext" =>"plaintext" ,"md5" =>"md5" ,"rsa" =>"rsa"));
172 $smarty->assign("goFonTrunks", array("yes" =>_("yes") ,"no" => _("no")));
174 /* deativate all fields that are not used by the specified type */
175 foreach($this->attributes as $att){
176 if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
177 $smarty->assign($att."USED", "disabled" );
178 $smarty->assign($att, "");
179 }else{
180 $smarty->assign($att."USED", "" );
181 $smarty->assign($att, $this->$att);
182 }
183 }
185 $smarty->assign("selected_categorie",$this->selected_categorie);
187 /* Assign attributes */
188 foreach ($this->attributes as $attr){
189 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
190 }
191 $smarty->assign("base_select", $this->base);
193 /* Show Asterisk for required attribute ipHostNumber and macAddress */
194 $smarty->assign("staticAddress", "<font class=\"must\">*</font>");
196 /* Show main page */
197 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
198 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
199 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
200 }
202 function remove_from_parent()
203 {
204 $ldap= $this->config->get_ldap_link();
205 $ldap->rmdir($this->dn);
206 show_ldap_error($ldap->get_error());
207 $this->handle_post_events("remove");
209 /* Delete references to object groups */
210 $ldap->cd ($this->config->current['BASE']);
211 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
212 while ($ldap->fetch()){
213 $og= new ogroup($this->config, $ldap->getDN());
214 unset($og->member[$this->dn]);
215 $og->save ();
216 }
218 }
221 /* Save data to object */
222 function save_object()
223 {
224 plugin::save_object();
226 /* Save base, since this is no LDAP attribute */
227 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
228 $this->base= $_POST['base'];
229 }
230 }
233 /* Check supplied data */
234 function check()
235 {
236 $message= array();
237 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
239 /* To check for valid ip*/
240 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
242 if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
243 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
244 $message[]= _("Wrong IP format in field goFonDefaultIP.");
245 }
246 }
248 /* Check for valid number */
249 if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
250 if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
251 $message[]= _("The given value for goFonQualify is not a valid number.");
252 }
253 }
255 /* must: cn, macAddress, ipHostNumber */
256 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
257 $message[]= _("The required field 'Phone name' is not set.");
258 }
259 if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
260 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
261 }
262 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
263 $message[]= _("The required field 'MAC-address' is not set.");
264 }
265 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
266 $message[]= _("The required field 'IP-address' is not set.");
267 }
268 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
269 $message[]= _("Wrong IP format in field IP-address.");
270 }
272 $ui= get_userinfo();
273 $acl= get_permissions ($this->dn, $ui->subtreeACL);
274 $acl= get_module_permission($acl, "phone", $this->dn);
275 if (chkacl($acl, "create") != ""){
276 $message[]= _("You have no permissions to create a phone on this 'Base'.");
277 }
279 if ($this->orig_dn != $this->dn){
280 $ldap= $this->config->get_ldap_link();
281 $ldap->cd ($this->base);
282 $ldap->search ("(cn=".$this->cn.")", array("cn"));
283 if ($ldap->count() != 0){
284 while ($attrs= $ldap->fetch()){
285 if ($attrs['dn'] != $this->orig_dn){
286 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
287 break;
288 }
289 }
290 }
291 }
293 return ($message);
294 }
297 /* Save to LDAP */
298 function save()
299 {
300 plugin::save();
303 /* only to define which attrs to save*/
304 $mode = $this->attrs['selected_categorie'];
306 /* Remove all unwanted attrs */
307 foreach($this->attributes as $att){
309 /* Check all attributes, if they are needed for this type of phone */
310 if(!in_array($att,$this->usedattrs[$mode])){
311 $this->attrs[$att] = array();
312 }
314 /* Test rights of this user ... */
315 if(chkacl($this->acl,$att)!=""){
316 unset($this->attrs[$att]);
317 }
318 }
320 /* unset the categorie*/
321 unset($this->attrs['selected_categorie']);
323 /* Remove all empty values */
324 if ($this->orig_dn == 'new'){
325 $attrs= array();
326 foreach ($this->attrs as $key => $val){
327 if (is_array($val) && count($val) == 0){
328 continue;
329 }
330 $attrs[$key]= $val;
331 }
332 $this->attrs= $attrs;
333 }
335 /* Write back to ldap */
336 $ldap= $this->config->get_ldap_link();
337 if ($this->orig_dn == 'new'){
338 $ldap->cd($this->config->current['BASE']);
339 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
340 $ldap->cd($this->dn);
341 $ldap->add($this->attrs);
342 $this->handle_post_events("add");
343 } else {
344 if ($this->orig_dn != $this->dn){
345 $this->move($this->orig_dn, $this->dn);
346 }
348 $ldap->cd($this->dn);
349 $ldap->modify($this->attrs);
350 $this->handle_post_events("modify");
351 }
352 show_ldap_error($ldap->get_error());
353 /* Optionally execute a command after we're done */
354 $this->postcreate();
355 }
357 }
359 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
360 ?>