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