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])";
171 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->goFonDefaultIP)){
172 $message[]= _("Wrong IP format in field goFonDefaultIP\\n");
173 }
175 if((strlen($this->goFonQualify))!=(strlen((int)($this->goFonQualify)))){
176 $message[]= _("The given value for goFonQualify is not a valid number.");
177 }
179 /* must: cn, macAddress, ipHostNumber */
180 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
181 $message[]= _("The required field 'Phone name' is not set.");
182 }
183 if ($this->cn == "0" && chkacl ($this->acl, "cn") == ""){
184 $message[]= _("The 'Phone name' '0' is reserved and cannot be used.");
185 }
186 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
187 $message[]= _("The required field 'MAC-address' is not set.");
188 }
189 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
190 $message[]= _("The required field 'IP-address' is not set.");
191 }
193 $ui= get_userinfo();
194 $acl= get_permissions ($this->dn, $ui->subtreeACL);
195 $acl= get_module_permission($acl, "phone", $this->dn);
196 if (chkacl($acl, "create") != ""){
197 $message[]= _("You have no permissions to create a phone on this 'Base'.");
198 }
200 if ($this->orig_dn != $this->dn){
201 $ldap= $this->config->get_ldap_link();
202 $ldap->cd ($this->base);
203 $ldap->search ("(cn=".$this->cn.")", array("cn"));
204 if ($ldap->count() != 0){
205 while ($attrs= $ldap->fetch()){
206 if ($attrs['dn'] != $this->orig_dn){
207 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
208 break;
209 }
210 }
211 }
212 }
214 return ($message);
215 }
218 /* Save to LDAP */
219 function save()
220 {
221 plugin::save();
224 /* only to define which attrs to save*/
225 $mode = $this->attrs['selected_categorie'];
227 /* Remove all unwanted attrs */
228 foreach($this->attributes as $att){
230 /* Check all attributes, if they are needed for this type of phone */
231 if(!in_array($att,$this->usedattrs[$mode])){
232 $this->attrs[$att] = array();
233 }
235 /* Test rights of this user ... */
236 if(chkacl($this->acl,$att)!=""){
237 unset($this->attrs[$att]);
238 }
239 }
241 /* unset the categorie*/
242 unset($this->attrs['selected_categorie']);
244 /* Remove all empty values */
245 if ($this->orig_dn == 'new'){
246 $attrs= array();
247 foreach ($this->attrs as $key => $val){
248 if (is_array($val) && count($val) == 0){
249 continue;
250 }
251 $attrs[$key]= $val;
252 }
253 $this->attrs= $attrs;
254 }
256 /* Write back to ldap */
257 $ldap= $this->config->get_ldap_link();
258 if ($this->orig_dn == 'new'){
259 $ldap->cd($this->config->current['BASE']);
260 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
261 $ldap->cd($this->dn);
262 $ldap->add($this->attrs);
263 $this->handle_post_events("add");
264 } else {
265 if ($this->orig_dn != $this->dn){
266 $this->move($this->orig_dn, $this->dn);
267 }
269 $ldap->cd($this->dn);
270 $ldap->modify($this->attrs);
271 $this->handle_post_events("modify");
272 }
273 show_ldap_error($ldap->get_error());
275 /* Optionally execute a command after we're done */
276 $this->postcreate();
277 }
279 }
281 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
282 ?>