1 <?php
3 class phoneAccount extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "Phone";
7 var $plDescription= "This does something";
8 var $has_mailAccount= FALSE;
10 /* Attributes */
11 var $telephoneNumber = "";
12 var $goFonHardware = "";
13 var $goFonForwarding = "";
14 var $goFonFormat = "";
15 var $goFonPIN = "";
16 var $goFonDeliveryMode = "";
17 var $phoneNumbers = array();
18 var $forwarders = array();
19 var $mail = "";
20 var $hardware_list = array();
21 var $used_hardware = array();
22 var $goFonMacro = "";
23 var $macro = 0; // Selected Macor
24 var $macros = array(); // List of macros for smarty select box
25 var $macroarray = array(); // All needed macro informations
26 var $macrostillavailable = false;
28 /* CLI vars */
29 var $cli_summary = "Manage users phone account";
30 var $cli_description = "Some longer text\nfor help";
31 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
33 /* attribute list for save action */
34 var $attributes = array("goFonDeliveryMode", "goFonForwarding", "goFonFormat",
35 "goFonHardware", "goFonPIN", "telephoneNumber", "goFonMacro","macro");
36 var $objectclasses= array("goFonAccount");
38 function phoneAccount ($config, $dn= NULL)
39 {
40 plugin::plugin ($config, $dn);
42 /* Set phone hardware */
43 if (!isset($this->attrs['goFonHardware'])){
44 $this->goFonHardware= "automatic";
45 }
47 /* Preset voice format */
48 if (!isset($this->attrs['goFonFormat'])){
49 $this->goFonFormat= "wav";
50 }
52 /* Assemble phone numbers */
53 if (isset($this->attrs['telephoneNumber'])){
54 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
55 $number= $this->attrs['telephoneNumber'][$i];
56 $this->phoneNumbers[$number]= $number;
57 }
58 }
60 /* Assemble forwarders */
61 if (isset($this->attrs['goFonForwarding'])){
62 for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
63 list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
64 $this->forwarders[$num]= "$v1;$v2";
65 }
66 } else {
67 $this->forwarders= array("");
68 }
70 /* Set up has_mailAccount */
71 if (isset($this->attrs['objectClass'])){
72 if (in_array("gosaMailAccount", $this->attrs['objectClass'])){
73 $this->has_mailAccount= TRUE;
74 }
75 }
77 /* Load hardware list */
78 $ldap= $this->config->get_ldap_link();
79 $ldap->cd($this->config->current['BASE']);
80 $ldap->search("(objectClass=goFonHardware)", array('cn', 'description'));
81 while ($attrs= $ldap->fetch()){
82 $cn= $attrs['cn'][0];
83 if (isset($attrs['description'])){
84 $description= " - ".$attrs['description'][0];
85 } else {
86 $description= "";
87 }
88 $this->hardware_list[$cn]= "$cn$description";
90 }
92 /* Prepare templating */
93 $smarty= get_smarty();
96 /* Perform search, to get Macro Parameters,Name,Dn,Displayname etc*/
97 $ldap->search("(objectClass=goFonMacro)", array("*"));
99 /* Add none for no macro*/
100 $this->macros['none']=_("none");
101 $this->macro ="none";
104 /* Fetch all Macros*/
105 while ($attrs= $ldap->fetch()){
107 /* Only visisble */
108 if((isset($attrs['goFonMacroVisible'][0]))&&($attrs['goFonMacroVisible'][0] ==1)){
110 /* unset Count, we don't need that here */
111 unset($attrs['displayName']['count']);
113 /* fill Selectfield variable with Macros */
114 if(isset($attrs['displayName'][0])){
115 $this->macros[$attrs['dn']] = $attrs['displayName'][0]." (".$attrs['cn'][0].")";
116 }else{
117 $this->macros[$attrs['dn']] = _("undefined");
118 }
120 /* Parse macro data, unset count for parameterarrays */
121 unset($attrs['goFonMacroParameter']['count']);
123 /* Go through available parameters and parse all attributes, like parametername, type, default ...*/
124 if((isset($attrs['goFonMacroParameter']))&&(is_array($attrs['goFonMacroParameter']))){
126 foreach($attrs['goFonMacroParameter'] as $pkey=>$pval){
128 /* Split Data in readable values, by delimiter ! */
129 $data = split("!",$attrs['goFonMacroParameter'][$pkey]);
131 /* Set all attrs */
132 $id = $data[0];
133 $this->macroarray[$attrs['dn']][$id]['var'] ="var".$id;
134 $this->macroarray[$attrs['dn']][$id]['choosen']="N/A";
135 $this->macroarray[$attrs['dn']][$id]['id'] = $id;
136 $this->macroarray[$attrs['dn']][$id]['name'] =$data[1];
137 $this->macroarray[$attrs['dn']][$id]['type'] =$data[2];
138 $this->macroarray[$attrs['dn']][$id]['default']=$data[3];
139 }//foreach
140 }//is_array
141 }//visible = 1
142 }//while
143 /* Go through already saved values, for a parameter */
144 $tmp = split("!",$this->goFonMacro);
146 /* it is possible that nothing has been saved yet */
147 if(is_array($tmp)){
149 /* First value is the macroname */
150 $this->macro = $tmp[0];
152 /* Macroname saved, delete that index */
153 unset($tmp[0]);
155 /* Check if makro has been removed */
156 if(!isset($this->macroarray[$this->macro])){
157 $this->macrostillavailable = false;
158 }else{
159 $this->macrostillavailable = true;
160 }
162 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
163 foreach($tmp as $var){
165 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
166 $varar = split("#",$var);
168 /* Only insert if the parameter still exists */
169 if(isset($this->macroarray[$this->macro][$varar[0]])){
171 /* Assign value */
172 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
173 }
174 }
175 }
177 /* Eventually colorize phones */
178 $ldap->cd($this->config->current['BASE']);
179 foreach ($this->hardware_list as $cn => $desc){
180 $ldap->search("(goFonHardware=$cn)", array('cn'));
181 if ($ldap->count() > 0){
182 $ldap->fetch();
183 if ($ldap->getDN() != $this->dn){
184 $this->used_hardware[$cn]= $ldap->getDN();
185 }
186 }
187 }
189 $this->hardware_list["automatic"]= _("automatic");
190 ksort($this->hardware_list);
191 }
194 function execute()
195 {
196 /* Do we need to flip is_account state? */
197 if (isset($_POST['modify_state'])){
198 $this->is_account= !$this->is_account;
199 }
201 /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
202 if(empty($this->macro)){
203 $this->macro ="none";
204 }
206 /* tell user that the pluging selected is no longer available*/
207 if((!$this->macrostillavailable)&&($this->macro!="none")){
208 print_red(_("The macro you selected in the past, is no longer available for you, please choose another one."));
209 }
211 /* Prepare templating */
212 $smarty= get_smarty();
214 /* Assing macroselectbox values */
215 $smarty->assign("macros",$this->macros);
216 $smarty->assign("macro", $this->macro);
218 /* Create parameter table, skip if no parameters given */
219 if(!isset($this->macroarray[$this->macro])){
220 $macrotab="";
221 }else{
223 $macrotab ="<table>";
224 /* for every single parameter-> display textfile,combo, or true false switch*/
225 foreach($this->macroarray[$this->macro] as $paras){
227 /* get al vars */
228 $var = $paras['var'];
229 $name = $paras['name'];
230 $default = $paras['default'];
231 $type = $paras['type'];
232 $choosen = $paras['choosen'] ;
233 $str = $default;
235 /* in case of a combo box display a combobox with selected attr */
236 if($type == "combo"){
237 $str="<select name='".$var."' >";
238 foreach(split(":",$default) as $choice){
239 if($choosen==$choice){
240 $str.="\n<option name='".$var."' value='".$choice."' selected>".$choice."</option>";
241 }else{
242 $str.="\n<option name='".$var."' value='".$choice."'>".$choice."</option>";
243 }
244 }
245 $str.="</select>";
246 }
248 /* Display switch for true false*/
249 if($type == "bool"){
251 $str="<select name='".$var."'>";
253 if($choosen){
254 $str.="<option name='".$var."' value='0'>"._("false")."</option>";
255 $str.="<option name='".$var."' value='1' selected>"._("true")."</option>";
256 }else{
257 $str.="<option name='".$var."' value='0' selected>"._("false")."</option>";
258 $str.="<option name='".$var."' value='1'>"._("true")."</option>";
259 }
260 $str.="<select name='".$var."'>";
261 }
263 /* display simple textfield */
264 if($type=="string"){
265 $str="<input name='".$var."' value='".$choosen."'>";
266 }
268 /* create table entry*/
269 $macrotab.= "\n<tr><td>".$name."</td><td>".$str."</td></tr>";
271 }
272 $macrotab.="</table>";
273 }//is_array()
275 /* Give smarty the table */
276 $smarty->assign("macrotab",$macrotab);
278 /* Do we represent a valid account? */
279 if (!$this->is_account && $this->parent == NULL){
280 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
281 _("This account has no phone extensions.")."</b>";
282 $display.= back_to_main();
283 return($display);
284 }
286 $display= "";
288 /* Show tab dialog headers */
289 if ($this->parent != NULL){
290 if ($this->is_account){
291 $display= $this->show_header(_("Remove phone account"),
292 _("This account has phone features enabled. You can disable them by clicking below."));
293 } else {
294 $display= $this->show_header(_("Create phone account"),
295 _("This account has phone features disabled. You can enable them by clicking below."));
296 return ($display);
297 }
298 }
300 /* Add phone number */
301 if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
302 if (is_phone_nr($_POST['phonenumber'])){
303 $number= $_POST["phonenumber"];
304 $this->phoneNumbers[$number]= $number;
305 $this->is_modified= TRUE;
306 } else {
307 print_red(_("Please enter a valid phone number!"));
308 }
309 }
311 /* Remove phone number */
312 if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
313 foreach ($_POST['phonenumber_list'] as $number){
314 unset($this->phoneNumbers[$number]);
315 $this->is_modified= TRUE;
316 }
317 }
319 /* Check for forwarding action */
320 foreach ($this->forwarders as $nr => $fw){
322 /* Buttons pressed? */
323 if (isset($_POST["add_fw$nr"])){
324 $this->forwarders= $this->insert_after("", $nr, $this->forwarders);
325 }
326 if (isset($_POST["remove_fw$nr"])){
327 unset($this->forwarders[$nr]);
328 }
329 }
331 /* Transfer ACL's */
332 foreach($this->attributes as $val){
333 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
334 }
336 /* Fill arrays */
337 $smarty->assign ("goFonHardware", $this->goFonHardware);
338 if (!count($this->phoneNumbers)){
339 $smarty->assign ("phoneNumbers", array(""));
340 } else {
341 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
342 }
343 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
344 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
345 foreach ($this->hardware_list as $cn => $description){
346 if ($cn == $this->goFonHardware){
347 $selected= "selected";
348 } else {
349 $selected= "";
350 }
351 if (isset($this->used_hardware[$cn])){
352 $color= "style=\"color:#A0A0A0\"";
353 } else {
354 $color= "";
355 }
356 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
357 }
358 $hl.= "</select>\n";
359 $smarty->assign ("hardware_list", $hl);
361 /* Generate forwarder view */
362 $forwarder_list="";
363 $acl= chkacl($this->acl, "goFonForwaring");
364 foreach ($this->forwarders as $nr => $fw){
365 if ($fw == ""){
366 $number= ""; $timeout= "";
367 } else {
368 list($number, $timeout)= split(";", $fw);
369 }
370 $forwarder_list.= "<tr><td>";
371 $forwarder_list.= "<input name=\"fwn$nr\" size=25 align=\"middle\" maxlength=60 value=\"$number\" $acl>";
372 $forwarder_list.= "</td><td>";
373 $forwarder_list.= "<input name=\"fwt$nr\" size=5 align=\"middle\" maxlength=5 value=\"$timeout\" $acl>";
374 $forwarder_list.= "</td><td>";
375 $forwarder_list.= "<input type=\"submit\" value=\""._("Add")."\" name=\"add_fw$nr\" $acl>";
376 if (count($this->forwarders) > 1){
377 $forwarder_list.= "<input type=\"submit\" value=\""._("Remove")."\" name=\"remove_fw$nr\" $acl>";
378 }
379 $forwarder_list.= "</td></tr>";
380 }
381 $smarty->assign("forwarder_list", $forwarder_list);
385 /* Check box */
386 if ($this->parent->by_object['mailAccount'] &&
387 $this->parent->by_object['mailAccount']->is_account &&
388 preg_match("/M/i", $this->goFonDeliveryMode)){
389 $smarty->assign("fon_to_mail", "checked");
390 } else {
391 $smarty->assign("fon_to_mail", "");
392 }
394 if (!isset($this->parent->by_object['mailAccount'])) {
395 $smarty->assign("has_mailaccount", "false");
396 $this->has_mailAccount= false;
397 } elseif ( !$this->parent->by_object['mailAccount']->is_account){
398 $smarty->assign("has_mailaccount", "false");
399 $this->has_mailAccount= false;
400 } else {
401 $smarty->assign("has_mailaccount", "true");
402 }
404 /* Show main page */
405 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
406 return($display);
407 }
410 function save_object()
411 {
412 if (isset($_POST["phoneTab"])){
413 plugin::save_object();
415 /* Save checkbox */
416 if (isset($_POST['fon_to_mail'])){
417 $tmp= "[M]";
418 } else {
419 $tmp= "[]";
420 }
421 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
422 if ($this->goFonDeliveryMode != $tmp){
423 $this->is_modified= TRUE;
424 }
425 $this->goFonDeliveryMode= $tmp;
426 }
428 /* Save forwarding numbers and timeouts */
429 if (chkacl ($this->acl, "goFonForwarder") == ""){
430 foreach ($this->forwarders as $nr => $fw){
431 $tmp= $_POST["fwn$nr"].";".$_POST["fwt$nr"];
432 if ($this->forwarders[$nr] != $tmp){
433 $this->is_modified= TRUE;
434 }
435 $this->forwarders[$nr]= $tmp;
436 }
437 }
439 /* Check if mail account is active and correct the internal
440 reference to represent the current status. */
441 if ($this->parent->by_object['mailAccount']->is_account){
442 $this->has_mailAccount= TRUE;
443 }
445 /* Every macro in the select box are available */
446 if((isset($_POST['macro']))){
447 $this->macrostillavailable=true;
448 }
450 /* get all Postvars */
451 if(isset($this->macroarray[$this->macro])){
452 foreach($this->macroarray[$this->macro] as $key => $paras){
453 if(isset($_POST[$paras['var']])){
454 $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
455 }
456 }
457 }
458 }
461 }
463 function check()
464 {
465 /* Reset message array */
466 $message= array();
468 /* We need at least one phone number */
469 if (count($this->phoneNumbers) == 0){
470 $message[]= sprintf(_("You need to specify at least one phone number!"));
471 }
473 /* Check timestamps and phonenumbers */
474 foreach ($this->forwarders as $fw){
476 /* Skip empty values */
477 if ($fw == ";"){
478 continue;
479 }
481 /* Check */
482 list($number, $timeout)= split(";", $fw);
483 if (!is_phone_nr($number)){
484 $message[]= sprintf(_("The number '%s' is no valid phone number!"), $number);
485 }
486 if (!is_id($timeout)){
487 $message[]= sprintf(_("The timeout '%s' contains invalid characters!"), $timeout);
488 }
489 }
491 /* check for ! in any parameter setting*/
492 if(isset($this->macroarray[$this->macro])){
493 foreach($this->macroarray[$this->macro] as $val){
494 if(strstr("!",$val['choosen'])){
495 $message[] = sprintf(_("The parameter %s contains invalid char. '!' is used as delimiter"),$val['name']);
496 }
497 }
498 }
499 return ($message);
500 }
504 function save()
505 {
506 plugin::save();
508 /* goFonAccount has "mail" as must! Block if no mailaddress is specified... */
509 if (isset($this->parent->by_object['mailAccount']) &&
510 !$this->parent->by_object['mailAccount']->is_account) {
512 $this->goFonDeliveryMode= preg_replace("/M/i", "", $this->goFonDeliveryMode);
513 }
515 /* Save arrays */
516 $this->attrs['telephoneNumber']= array();
517 foreach ($this->phoneNumbers as $number){
518 $this->attrs['telephoneNumber'][]= $number;
519 }
520 $this->attrs['goFonForwarding']= array();
521 foreach ($this->forwarders as $index => $number){
522 $this->attrs['goFonForwarding'][]= "$index;$number";
523 }
525 /* Save settings, or remove goFonMacro attribute*/
526 if($this->macro!="none"){
527 $this->attrs['goFonMacro']=$this->macro;
528 if(isset($this->macroarray[$this->macro])){
529 foreach($this->macroarray[$this->macro] as $paras) {
530 $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
531 }
532 }
533 }else{
534 $this->attrs['goFonMacro']=array();
535 }
536 unset($this->attrs['macro']) ;
538 /* Write back to ldap */
539 $ldap= $this->config->get_ldap_link();
540 $ldap->cd($this->dn);
541 $ldap->modify($this->attrs);
542 show_ldap_error($ldap->get_error());
544 /* Optionally execute a command after we're done */
545 if ($this->initially_was_account == $this->is_account){
546 if ($this->is_modified){
547 $this->handle_post_events("modify");
548 }
549 } else {
550 $this->handle_post_events("add");
551 }
553 }
556 function insert_after($entry, $nr, $list)
557 {
558 /* Is the entry free? No? Make it free... */
559 if (isset($list[$nr])) {
560 $dest= array();
561 $newidx= 0;
563 foreach ($list as $idx => $contents){
564 $dest[$newidx++]= $contents;
565 if ($idx == $nr){
566 $dest[$newidx++]= $entry;
567 }
568 }
569 } else {
570 $dest= $list;
571 $dest[$nr]= $entry;
572 }
574 return ($dest);
575 }
578 function adapt_from_template($dn)
579 {
580 plugin::adapt_from_template($dn);
582 /* Assemble phone numbers */
583 if (isset($this->attrs['telephoneNumber'])){
584 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
585 $number= $this->attrs['telephoneNumber'][$i];
586 $this->phoneNumbers[$number]= $number;
587 }
588 }
590 /* Assemble forwarders */
591 if (isset($this->attrs['goFonForwarding'])){
592 for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
593 list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
594 $this->forwarders[$num]= "$v1;$v2";
595 }
596 } else {
597 $this->forwarders= array("");
598 }
599 }
602 function remove_from_parent()
603 {
604 /* Cancel if there's nothing to do here */
605 if (!$this->initially_was_account){
606 return;
607 }
609 plugin::remove_from_parent();
611 /* Just keep one phone number */
612 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
613 $this->attrs['telephoneNumber']= $this->telephoneNumber[0];
614 } else {
615 $this->attrs['telephoneNumber']= array();
616 }
618 $ldap= $this->config->get_ldap_link();
619 $ldap->cd($this->dn);
620 $ldap->modify($this->attrs);
621 show_ldap_error($ldap->get_error());
623 /* Optionally execute a command after we're done */
624 $this->handle_post_events('remove');
625 }
628 }
630 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
631 ?>