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']=_("no macro");
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
144 /* Go through already saved values, for a parameter */
145 $tmp = split("!",$this->goFonMacro);
147 /* it is possible that nothing has been saved yet */
148 if(is_array($tmp)){
150 /* First value is the macroname */
151 $this->macro = $tmp[0];
153 /* Macroname saved, delete that index */
154 unset($tmp[0]);
156 /* Check if makro has been removed */
157 if(!isset($this->macroarray[$this->macro])){
158 $this->macrostillavailable = false;
159 }else{
160 $this->macrostillavailable = true;
161 }
163 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
164 foreach($tmp as $var){
166 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
167 $varar = split("#",$var);
169 /* Only insert if the parameter still exists */
170 if(isset($this->macroarray[$this->macro][$varar[0]])){
172 /* Assign value */
173 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
174 }
175 }
176 }
178 /* Eventually colorize phones */
179 $ldap->cd($this->config->current['BASE']);
180 foreach ($this->hardware_list as $cn => $desc){
181 $ldap->search("(goFonHardware=$cn)", array('cn'));
182 if ($ldap->count() > 0){
183 $ldap->fetch();
184 if ($ldap->getDN() != $this->dn){
185 $this->used_hardware[$cn]= $ldap->getDN();
186 }
187 }
188 }
190 $this->hardware_list["automatic"]= _("automatic");
191 ksort($this->hardware_list);
192 }
195 function execute()
196 {
197 /* Do we need to flip is_account state? */
198 if (isset($_POST['modify_state'])){
199 $this->is_account= !$this->is_account;
200 }
202 /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
203 if(empty($this->macro)){
204 $this->macro ="none";
205 }
207 /* tell user that the pluging selected is no longer available*/
208 if((!$this->macrostillavailable)&&($this->macro!="none")){
209 print_red(_("The macro you selected in the past, is no longer available for you, please choose another one."));
210 }
212 /* Prepare templating */
213 $smarty= get_smarty();
215 /* Assing macroselectbox values */
216 $smarty->assign("macros",$this->macros);
217 $smarty->assign("macro", $this->macro);
219 /* Create parameter table, skip if no parameters given */
220 if(!isset($this->macroarray[$this->macro])){
221 $macrotab="";
222 }else{
224 $macrotab ="<table>";
225 /* for every single parameter-> display textfile,combo, or true false switch*/
226 foreach($this->macroarray[$this->macro] as $paras){
228 /* get al vars */
229 $var = $paras['var'];
230 $name = $paras['name'];
231 $default = $paras['default'];
232 $type = $paras['type'];
233 $choosen = $paras['choosen'] ;
234 $str = $default;
236 /* in case of a combo box display a combobox with selected attr */
237 $macrotab.= "<tr>";
238 switch ($type){
240 case "combo":
241 $str= "<select name='".$var."' ".chkacl($this->acl, "goFonMacro").">";
242 foreach(split(":",$default) as $choice){
243 if($choosen==$choice){
244 $str.= "\n<option name='".$var."' value='".$choice."' selected>".$choice."</option>";
245 }else{
246 $str.= "\n<option name='".$var."' value='".$choice."'>".$choice."</option>";
247 }
248 }
249 $str.="</select>";
250 $macrotab.= "<td>$name</td><td>$str";
251 break;
253 case "bool":
254 if(!$choosen){
255 $str="\n<input type='checkbox' name='".$var."' value='1'>";
256 }else{
257 $str="\n<input type='checkbox' name='".$var."' value='1' checked>";
258 }
259 $macrotab.= "<td colspan='2'>$str $name";
260 break;
262 case "string":
263 $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro").">";
264 $macrotab.= "<td>$name</td><td>$str";
265 break;
267 }
268 $macrotab.= "</td</tr>";
270 }
271 $macrotab.="</table>";
272 }//is_array()
274 /* Give smarty the table */
275 $smarty->assign("macrotab",$macrotab);
277 /* Do we represent a valid account? */
278 if (!$this->is_account && $this->parent == NULL){
279 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
280 _("This account has no phone extensions.")."</b>";
281 $display.= back_to_main();
282 return($display);
283 }
285 $display= "";
287 /* Show tab dialog headers */
288 if ($this->parent != NULL){
289 if ($this->is_account){
290 $display= $this->show_header(_("Remove phone account"),
291 _("This account has phone features enabled. You can disable them by clicking below."));
292 } else {
293 $display= $this->show_header(_("Create phone account"),
294 _("This account has phone features disabled. You can enable them by clicking below."));
295 return ($display);
296 }
297 }
299 /* Add phone number */
300 if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
301 if (is_phone_nr($_POST['phonenumber'])){
302 $number= $_POST["phonenumber"];
303 $this->phoneNumbers[$number]= $number;
304 $this->is_modified= TRUE;
305 } else {
306 print_red(_("Please enter a valid phone number!"));
307 }
308 }
310 /* Remove phone number */
311 if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
312 foreach ($_POST['phonenumber_list'] as $number){
313 unset($this->phoneNumbers[$number]);
314 $this->is_modified= TRUE;
315 }
316 }
318 /* Check for forwarding action */
319 foreach ($this->forwarders as $nr => $fw){
321 /* Buttons pressed? */
322 if (isset($_POST["add_fw$nr"])){
323 $this->forwarders= $this->insert_after("", $nr, $this->forwarders);
324 }
325 if (isset($_POST["remove_fw$nr"])){
326 unset($this->forwarders[$nr]);
327 }
328 }
330 /* Transfer ACL's */
331 foreach($this->attributes as $val){
332 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
333 }
335 /* Fill arrays */
336 $smarty->assign ("goFonHardware", $this->goFonHardware);
337 if (!count($this->phoneNumbers)){
338 $smarty->assign ("phoneNumbers", array(""));
339 } else {
340 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
341 }
342 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
343 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
344 foreach ($this->hardware_list as $cn => $description){
345 if ($cn == $this->goFonHardware){
346 $selected= "selected";
347 } else {
348 $selected= "";
349 }
350 if (isset($this->used_hardware[$cn])){
351 $color= "style=\"color:#A0A0A0\"";
352 } else {
353 $color= "";
354 }
355 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
356 }
357 $hl.= "</select>\n";
358 $smarty->assign ("hardware_list", $hl);
360 /* Generate forwarder view */
361 $forwarder_list="";
362 $acl= chkacl($this->acl, "goFonForwaring");
363 foreach ($this->forwarders as $nr => $fw){
364 if ($fw == ""){
365 $number= ""; $timeout= "";
366 } else {
367 list($number, $timeout)= split(";", $fw);
368 }
369 $forwarder_list.= "<tr><td>";
370 $forwarder_list.= "<input name=\"fwn$nr\" size=25 align=\"middle\" maxlength=60 value=\"$number\" $acl>";
371 $forwarder_list.= "</td><td>";
372 $forwarder_list.= "<input name=\"fwt$nr\" size=5 align=\"middle\" maxlength=5 value=\"$timeout\" $acl>";
373 $forwarder_list.= "</td><td>";
374 $forwarder_list.= "<input type=\"submit\" value=\""._("Add")."\" name=\"add_fw$nr\" $acl>";
375 if (count($this->forwarders) > 1){
376 $forwarder_list.= "<input type=\"submit\" value=\""._("Remove")."\" name=\"remove_fw$nr\" $acl>";
377 }
378 $forwarder_list.= "</td></tr>";
379 }
380 $smarty->assign("forwarder_list", $forwarder_list);
382 /* Show main page */
383 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
384 return($display);
385 }
388 function save_object()
389 {
390 if (isset($_POST["phoneTab"])){
391 plugin::save_object();
393 /* Save checkbox */
394 if (isset($_POST['fon_to_mail'])){
395 $tmp= "[M]";
396 } else {
397 $tmp= "[]";
398 }
399 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
400 if ($this->goFonDeliveryMode != $tmp){
401 $this->is_modified= TRUE;
402 }
403 $this->goFonDeliveryMode= $tmp;
404 }
406 /* Save forwarding numbers and timeouts */
407 if (chkacl ($this->acl, "goFonForwarder") == ""){
408 foreach ($this->forwarders as $nr => $fw){
409 $tmp= $_POST["fwn$nr"].";".$_POST["fwt$nr"];
410 if ($this->forwarders[$nr] != $tmp){
411 $this->is_modified= TRUE;
412 }
413 $this->forwarders[$nr]= $tmp;
414 }
415 }
417 /* Every macro in the select box are available */
418 if((isset($_POST['macro']))){
419 $this->macrostillavailable=true;
420 }
422 /* get all Postvars */
423 if(isset($this->macroarray[$this->macro])){
424 foreach($this->macroarray[$this->macro] as $key => $paras){
425 if(isset($_POST[$paras['var']])){
426 $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
427 }
429 /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code
430 We need this code below to read and save checkboxes correct
431 */
432 if($this->macroarray[$this->macro][$key]['type']=="bool"){
433 if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
434 $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
435 }else{
436 $this->macroarray[$this->macro][$key]['choosen']=false;
437 }
438 }
439 }
440 }
441 }
444 }
446 function check()
447 {
448 /* Reset message array */
449 $message= array();
451 /* We need at least one phone number */
452 if (count($this->phoneNumbers) == 0){
453 $message[]= sprintf(_("You need to specify at least one phone number!"));
454 }
456 /* Check timestamps and phonenumbers */
457 foreach ($this->forwarders as $fw){
459 /* Skip empty values */
460 if ($fw == ";"){
461 continue;
462 }
464 /* Check */
465 list($number, $timeout)= split(";", $fw);
466 if (!is_phone_nr($number)){
467 $message[]= sprintf(_("The number '%s' is no valid phone number!"), $number);
468 }
469 if (!is_id($timeout)){
470 $message[]= sprintf(_("The timeout '%s' contains invalid characters!"), $timeout);
471 }
472 }
474 /* check for ! in any parameter setting*/
475 if(isset($this->macroarray[$this->macro])){
476 foreach($this->macroarray[$this->macro] as $val){
477 if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
478 $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
479 }
480 }
481 }
482 return ($message);
483 }
487 function save()
488 {
489 plugin::save();
491 /* Save arrays */
492 $this->attrs['telephoneNumber']= array();
493 foreach ($this->phoneNumbers as $number){
494 $this->attrs['telephoneNumber'][]= $number;
495 }
496 $this->attrs['goFonForwarding']= array();
497 foreach ($this->forwarders as $index => $number){
498 $this->attrs['goFonForwarding'][]= "$index;$number";
499 }
501 /* Save settings, or remove goFonMacro attribute*/
502 if($this->macro!="none"){
503 $this->attrs['goFonMacro']=$this->macro;
504 if(isset($this->macroarray[$this->macro])){
505 foreach($this->macroarray[$this->macro] as $paras) {
506 $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
507 }
508 }
509 }else{
510 $this->attrs['goFonMacro']=array();
511 }
512 unset($this->attrs['macro']) ;
514 /* Write back to ldap */
515 $ldap= $this->config->get_ldap_link();
516 $ldap->cd($this->dn);
517 $ldap->modify($this->attrs);
518 show_ldap_error($ldap->get_error());
520 /* Optionally execute a command after we're done */
521 if ($this->initially_was_account == $this->is_account){
522 if ($this->is_modified){
523 $this->handle_post_events("modify");
524 }
525 } else {
526 $this->handle_post_events("add");
527 }
529 }
532 function insert_after($entry, $nr, $list)
533 {
534 /* Is the entry free? No? Make it free... */
535 if (isset($list[$nr])) {
536 $dest= array();
537 $newidx= 0;
539 foreach ($list as $idx => $contents){
540 $dest[$newidx++]= $contents;
541 if ($idx == $nr){
542 $dest[$newidx++]= $entry;
543 }
544 }
545 } else {
546 $dest= $list;
547 $dest[$nr]= $entry;
548 }
550 return ($dest);
551 }
554 function adapt_from_template($dn)
555 {
556 plugin::adapt_from_template($dn);
558 /* Assemble phone numbers */
559 if (isset($this->attrs['telephoneNumber'])){
560 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
561 $number= $this->attrs['telephoneNumber'][$i];
562 $this->phoneNumbers[$number]= $number;
563 }
564 }
566 /* Assemble forwarders */
567 if (isset($this->attrs['goFonForwarding'])){
568 for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
569 list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
570 $this->forwarders[$num]= "$v1;$v2";
571 }
572 } else {
573 $this->forwarders= array("");
574 }
575 }
578 function remove_from_parent()
579 {
580 /* Cancel if there's nothing to do here */
581 if (!$this->initially_was_account){
582 return;
583 }
585 plugin::remove_from_parent();
587 /* Just keep one phone number */
588 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
589 $this->attrs['telephoneNumber']= $this->telephoneNumber[0];
590 } else {
591 $this->attrs['telephoneNumber']= array();
592 }
594 $ldap= $this->config->get_ldap_link();
595 $ldap->cd($this->dn);
596 $ldap->modify($this->attrs);
597 show_ldap_error($ldap->get_error());
599 /* Optionally execute a command after we're done */
600 $this->handle_post_events('remove');
601 }
604 }
606 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
607 ?>