14a467dec0c2541260003561f2af9bd439193fae
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 $goFonOutkey = "";
30 var $goFonTrunk = "";
31 var $goFonAccountCode = "";
32 var $goFonMSN = "";
33 var $selected_categorie = 0;
35 /* attribute list for save action */
36 var $attributes= array("cn", "description", "macAddress", "ipHostNumber"
37 ,"goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
38 "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
39 "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie"
40 );
42 /* this array defines which attributes are schown / saved for the different type of phones */
43 var $usedattrs = array( "0"=>array("cn", "description", "macAddress", "ipHostNumber",
44 "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
45 "goFonQualify"),
46 "1"=>array("cn", "description", "macAddress", "ipHostNumber",
47 "goFonType","goFonHost","goFonDefaultIP",
48 "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
49 "goFonTrunk","goFonAccountCode","selected_categorie"),
50 "2"=>array("cn", "description", "macAddress", "ipHostNumber","goFonMSN"));
53 var $objectclasses= array("top", "goFonHardware");
55 function phonegeneric ($config, $dn= NULL)
56 {
57 plugin::plugin ($config, $dn);
59 /* Set base */
60 if ($this->dn == "new"){
61 $ui= get_userinfo();
62 $this->base= dn2base($ui->dn);
63 $this->cn= "";
64 } else {
65 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
66 }
68 if($this->goFonMSN != ""){
69 $this->selected_categorie = 2;
70 }elseif($this->goFonAccountCode != ""){
71 $this->selected_categorie = 1 ;
72 } else {
73 $this->selected_categorie = 0;
74 }
78 /* Save dn for later references */
79 $this->orig_dn= $this->dn;
80 }
82 function execute()
83 {
86 /* Do we represent a valid phone? */
87 if (!$this->is_account && $this->parent == NULL){
88 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
89 _("This 'dn' has no phone features.")."</b>";
90 return($display);
91 }
93 /* Fill templating stuff */
94 $smarty= get_smarty();
95 $smarty->assign("bases", $this->config->idepartments);
97 /* Create Arrays for samrty select boxes */
98 $smarty->assign("categories", array("SIP - phones","IAX - phones","CAPI - phones"));
99 $smarty->assign("goFonTypes", array("peer" =>"peer" ,"user" =>"user" ,"friend" =>"friend"));
100 $smarty->assign("goFonDmtfModes", array("inband" =>"inband" ,"rfc2833"=>"rfc2833" ,"info" =>"info"));
101 $smarty->assign("goFonAuths", array("plaintext" =>"plaintext" ,"md5" =>"md5" ,"rsa" =>"rsa"));
102 $smarty->assign("goFonTrunks", array("yes" =>"yes" ,"no" =>"no"));
104 /* deativate all fields that are not used by the specified type */
105 foreach($this->attributes as $att){
106 if((!in_array($att,$this->usedattrs[$this->selected_categorie]))||(chkacl($this->acl,$att)!="")){
107 $smarty->assign($att."USED", "disabled" );
108 $smarty->assign($att, "");
109 }else{
110 $smarty->assign($att."USED", "" );
111 $smarty->assign($att, $this->$att);
112 }
113 }
115 $smarty->assign("selected_categorie",$this->selected_categorie);
117 /* Assign attributes */
118 foreach ($this->attributes as $attr){
119 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
120 }
121 $smarty->assign("base_select", $this->base);
123 /* Show Asterisk for required attribute ipHostNumber and macAddress */
124 $smarty->assign("staticAddress", "<font class=\"must\">*</font>");
126 /* Show main page */
127 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
128 $smarty->assign("phonesettings", dirname(__FILE__)."/phonesettings.tpl");
129 return($smarty->fetch (get_template_path('phone.tpl', TRUE)));
130 }
132 function remove_from_parent()
133 {
134 $ldap= $this->config->get_ldap_link();
135 $ldap->rmdir($this->dn);
136 show_ldap_error($ldap->get_error());
137 $this->handle_post_events("remove");
139 /* Delete references to object groups */
140 $ldap->cd ($this->config->current['BASE']);
141 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
142 while ($ldap->fetch()){
143 $og= new ogroup($this->config, $ldap->getDN());
144 unset($og->member[$this->dn]);
145 $og->save ();
146 }
148 }
151 /* Save data to object */
152 function save_object()
153 {
154 plugin::save_object();
156 /* Save base, since this is no LDAP attribute */
157 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
158 $this->base= $_POST['base'];
159 }
160 }
163 /* Check supplied data */
164 function check()
165 {
166 $message= array();
167 $this->dn= "cn=".$this->cn.",ou=phones,ou=systems,".$this->base;
169 /* To check for valid ip*/
170 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
172 if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
173 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
174 $message[]= _("Wrong IP format in field goFonDefaultIP.");
175 }
176 }
178 /* Check for valid number */
179 if(in_array("goFonDefaultIP",$this->usedattrs[$this->selected_categorie])){
180 if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
181 $message[]= _("The given value for goFonQualify is not a valid number.");
182 }
183 }
185 /* must: cn, macAddress, ipHostNumber */
186 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
187 $message[]= _("The required field 'Phone name' is not set.");
188 }
189 if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
190 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
191 }
192 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
193 $message[]= _("The required field 'MAC-address' is not set.");
194 }
195 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
196 $message[]= _("The required field 'IP-address' is not set.");
197 }
198 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
199 $message[]= _("Wrong IP format in field IP-address.");
200 }
202 $ui= get_userinfo();
203 $acl= get_permissions ($this->dn, $ui->subtreeACL);
204 $acl= get_module_permission($acl, "phone", $this->dn);
205 if (chkacl($acl, "create") != ""){
206 $message[]= _("You have no permissions to create a phone on this 'Base'.");
207 }
209 if ($this->orig_dn != $this->dn){
210 $ldap= $this->config->get_ldap_link();
211 $ldap->cd ($this->base);
212 $ldap->search ("(cn=".$this->cn.")", array("cn"));
213 if ($ldap->count() != 0){
214 while ($attrs= $ldap->fetch()){
215 if ($attrs['dn'] != $this->orig_dn){
216 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
217 break;
218 }
219 }
220 }
221 }
223 return ($message);
224 }
227 /* Save to LDAP */
228 function save()
229 {
230 plugin::save();
233 /* only to define which attrs to save*/
234 $mode = $this->attrs['selected_categorie'];
236 /* Remove all unwanted attrs */
237 foreach($this->attributes as $att){
239 /* Check all attributes, if they are needed for this type of phone */
240 if(!in_array($att,$this->usedattrs[$mode])){
241 $this->attrs[$att] = array();
242 }
244 /* Test rights of this user ... */
245 if(chkacl($this->acl,$att)!=""){
246 unset($this->attrs[$att]);
247 }
248 }
250 /* unset the categorie*/
251 unset($this->attrs['selected_categorie']);
253 /* Remove all empty values */
254 if ($this->orig_dn == 'new'){
255 $attrs= array();
256 foreach ($this->attrs as $key => $val){
257 if (is_array($val) && count($val) == 0){
258 continue;
259 }
260 $attrs[$key]= $val;
261 }
262 $this->attrs= $attrs;
263 }
265 /* Write back to ldap */
266 $ldap= $this->config->get_ldap_link();
267 if ($this->orig_dn == 'new'){
268 $ldap->cd($this->config->current['BASE']);
269 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
270 $ldap->cd($this->dn);
271 $ldap->add($this->attrs);
272 $this->handle_post_events("add");
273 } else {
274 if ($this->orig_dn != $this->dn){
275 $this->move($this->orig_dn, $this->dn);
276 }
278 $ldap->cd($this->dn);
279 $ldap->modify($this->attrs);
280 $this->handle_post_events("modify");
281 }
282 show_ldap_error($ldap->get_error());
284 /* Optionally execute a command after we're done */
285 $this->postcreate();
286 }
288 }
290 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
291 ?>