Code

w3c
[gosa.git] / plugins / gofax / faxaccount / class_gofaxAccount.inc
1 <?php
3 class gofaxAccount extends plugin
4 {
5   /* Definitions */
6   var $plHeadline= "FAX";
7   var $plDescription= "This does something";
9   /* CLI vars */
10   var $cli_summary= "Manage users fax account";
11   var $cli_description= "Some longer text\nfor help";
12   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
14   /* Fax attributes */
15   var $goFaxDeliveryMode= "";
16   var $facsimileTelephoneNumber= "";
17   var $goFaxIsEnabled= 1;
18   var $goFaxPrinter= "";
19   var $goFaxDivertNumber= "";
20   var $goFaxLanguage= "";
21   var $goFaxFormat= "";
22   var $goFaxRBlocklist = array();
23   var $goFaxRBlockgroups= array();
24   var $goFaxSBlocklist= array();
25   var $goFaxSBlockgroups= array();
26   var $mail= "";
27   var $facsimileAlternateTelephoneNumber= array();
29   /* Internal variables */
30   var $printerList= array();
31   var $has_mailAccount= FALSE;
32   var $locals_dialog= FALSE;
33   var $in_blocklist_dialog= FALSE;
34   var $out_blocklist_dialog= FALSE;
35   var $current_blocklist= array();
37   /* attribute list for save action */
38   var $attributes= array("goFaxDeliveryMode", "goFaxIsEnabled",
39       "goFaxPrinter", "goFaxDivertNumber", "goFaxLanguage", "goFaxFormat", "mail");
40   var $objectclasses= array("goFaxAccount");
42   function gofaxAccount ($config, $dn= NULL)
43   {
44     /* General initialization */
45     plugin::plugin ($config, $dn);
47     if ($dn != "new"){
48       /* Get arrays */
49       foreach (array("goFaxRBlocklist", "goFaxRBlockgroups", "goFaxSBlocklist",
50             "goFaxSBlockgroups", "facsimileAlternateTelephoneNumber") as $val){
52         if (isset($this->attrs["$val"]["count"])){
53           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
54             array_push($this->$val, $this->attrs["$val"][$i]);
55           }
56         }
57       }
59       /* Set up has_mailAccount */
60       if (in_array("gosaMailAccount", $this->attrs['objectClass'])){
61         $this->has_mailAccount= TRUE;
62       }
63       if (isset($this->attrs["facsimileTelephoneNumber"][0])){
64         $this->facsimileTelephoneNumber= $this->attrs["facsimileTelephoneNumber"][0];
65       }
66     }
68     /* Load printer list */
69     if (isset($this->config->data['SERVERS']['CUPS'])){
70       $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
71       asort ($this->printerList);
72     }
74     /* Get global filter config */
75     if (!is_global("faxfilter")){
76       $ui= get_userinfo();
77       $base= get_base_from_people($ui->dn);
78       $faxfilter= array( "depselect"       => $base,
79           "fuser"            => "*",
80           "regex"           => "");
81       register_global("faxfilter", $faxfilter);
82     }
83   }
85   function execute()
86   {
87     /* Load smarty stuff */
88     $smarty= get_smarty();
90     /* Check if mail account is active. We'll display an additional
91        mail address field if this is not the case. Some people may
92        want goFax, but have a mailserver which is not managed with
93        GOsa */
94     if (!@isset($this->parent->by_object['mailAccount'])) {
95       $smarty->assign("has_mailaccount", $this->has_mailAccount?"true":"false");
96     } elseif ( !$this->parent->by_object['mailAccount']->is_account){
97       $smarty->assign("has_mailaccount", "false");
98       $this->has_mailAccount= false;
99     } else {
100       $smarty->assign("has_mailaccount", "true");
101     }
103     /* Do we need to flip is_account state? */
104     if (isset($_POST['modify_state'])){
105       $this->is_account= !$this->is_account;
106     }
108     /* Do we represent a valid account? */
109     if (!$this->is_account && $this->parent == NULL){
110       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
111         _("This account has no fax extensions.")."</b>";
112       $display.= back_to_main();
113       return ($display);
114     }
116     /* Show tab dialog headers */
117     $display= "";
118     if ($this->parent != NULL){
119       if ($this->is_account){
120         $display= $this->show_header(_("Remove fax account"),
121             _("This account has fax features enabled. You can disable them by clicking below."));
122       } else {
123         $display= $this->show_header(_("Create fax account"),
124             _("This account has fax features disabled. You can enable them by clicking below."));
125         return ($display);
126       }
127     }
129     /* Trigger Add local fax alternatives dialog */
130     if (isset($_POST['add_local_alternate'])){
131       $this->locals_dialog= TRUE;       
132       $this->dialog= TRUE;      
133     }
135     /* Add alternatives from dialog */
136     if (isset($_POST['add_locals_finish'])){
137       if (isset($_POST['local_list']) &&
138           chkacl ($this->acl, "facsimileAlternateTelephoneNumber") == ""){
140         foreach ($_POST['local_list'] as $val){
141           $this->addAlternate($val);
142           $this->is_modified= TRUE;
143         }
144       }
145     }
147     /* Add alternatives */
148     if (isset($_POST['add_alternate'])){
149       if ($_POST['forward_address'] != "" &&
150           is_phone_nr($_POST['forward_address']) &&
151           chkacl ($this->acl, "facsimileAlternateTelephoneNumber") == ""){
153         $this->addAlternate($_POST['forward_address']);
154       }
155     }
157     /* Delete alternate fax number */
158     if (isset($_POST['delete_alternate'])){
159       if (isset($_POST['alternate_list']) && count($_POST['alternate_list']) &&
160           chkacl ($this->acl, "facsimileAlternateTelephoneNumber") == ""){
162         $this->delAlternate ($_POST['alternate_list']);
163       }
164     }
167     /* Edit incoming blocklists */
168     if (isset($_POST['edit_incoming'])){
169       $this->current_blocklist= array_merge($this->goFaxRBlocklist,
170           $this->goFaxRBlockgroups);
171       sort($this->current_blocklist);
172       reset($this->current_blocklist);
174       $this->in_blocklist_dialog= TRUE;
175       $this->dialog= TRUE;
176     }
178     /* Edit outgoing blocklists */
179     if (isset($_POST['edit_outgoing'])){
180       $this->current_blocklist= array_merge($this->goFaxSBlocklist,
181           $this->goFaxSBlockgroups);
182       sort($this->current_blocklist);
183       reset($this->current_blocklist);
185       $this->out_blocklist_dialog= TRUE;
186       $this->dialog= TRUE;
187     }
189     /* Add number to blocklist (dialog) */
190     if (isset($_POST['add_blocklist_number']) && $_POST['block_number'] != ""){
191       if (!is_phone_nr($_POST['block_number'])){
192         print_red (_("You're trying to add an invalid phone number."));
193       } else {
194         array_push($this->current_blocklist, $_POST['block_number']);
195         $this->current_blocklist= array_unique($this->current_blocklist);
196         sort($this->current_blocklist);
197         reset($this->current_blocklist);
198       }
199     }
201     /* Add list to blocklist */
202     if (isset($_POST['add_blocklist']) && isset($_POST['predefined_list'])){
203       $this->current_blocklist= array_merge($this->current_blocklist,
204           $_POST['predefined_list']);
205       $this->current_blocklist= array_unique($this->current_blocklist);
206       sort($this->current_blocklist);
207       reset($this->current_blocklist);
208     }
210     /* Delete from blocklist */
211     if (isset($_POST['delete_blocklist_number']) && isset($_POST['block_list'])){
212       $tmp= array();
213       foreach($this->current_blocklist as $val){
214         if (!in_array($val, $_POST['block_list'])){
215           $tmp[]= $val;
216         }
217       }
218       $this->current_blocklist= $tmp;
219     }
221     /* Blocklist edit finished */
222     if (isset($_POST['edit_blocklists_finish'])){
224       /* Incoming or outgoing? */
225       if ($this->in_blocklist_dialog){
226         $destlist="goFaxRBlocklist";
227         $destgroup="goFaxRBlockgroups";
228       } else {
229         $destlist="goFaxSBlocklist";
230         $destgroup="goFaxSBlockgroups";
231       }
233       /* Transfer values to ourself */
234       $this->$destlist= array();
235       $this->$destgroup= array();
237       foreach ($this->current_blocklist as $val){
238         if (is_phone_nr($val)){
239           array_push($this->$destlist, $val);
240         } else {
241           array_push($this->$destgroups, $val);
242         }
243       }
245       $this->is_modified= TRUE;
246     }
248     /* Set departments */
249     if ($this->locals_dialog ||
250         $this->in_blocklist_dialog ||
251         $this->out_blocklist_dialog){
253       $list= array ();
254       $ldap= $this->config->get_ldap_link();
255       if (isset ($_POST['department'])){
256         $ldap->cd ($_POST['department']);
257       } else {
258         $ldap->cd ($this->config->current['BASE']);
259       }
260     }
262     /* Cancel  dialogs */
263     if (isset($_POST['add_locals_cancel']) || isset($_POST['edit_blocklists_finish']) ||
264         isset($_POST['edit_blocklists_cancel']) || isset($_POST['add_locals_finish'])){
266       $this->locals_dialog= FALSE;
267       $this->in_blocklist_dialog= FALSE;
268       $this->out_blocklist_dialog= FALSE;
269       $this->dialog= FALSE;
270     }
272     /* Manage locals dialog */
273     if ($this->locals_dialog){
275       /* Save data */
276       $faxfilter= get_global("faxfilter");
277       foreach( array("depselect", "fuser", "regex") as $type){
278         if (isset($_POST[$type])){
279           $faxfilter[$type]= $_POST[$type];
280         }
281       }
282       if (isset($_GET['search'])){
283         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
284         if ($s == "**"){
285           $s= "*";
286         }
287         $faxfilter['fuser']= $s;
288       }
289       register_global("faxfilter", $faxfilter);
291       if ($faxfilter['regex'] != '*' && $faxfilter['regex'] != ""){
292         $regex= $faxfilter['regex'];
293         $filter= "(facimileTelephoneNumber=$regex)";
294       } else {
295         $filter= "";
296       }
297       if ($faxfilter['fuser'] != ""){
298         $user= $faxfilter['fuser'];
299         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
300       }
302       $base= $faxfilter['depselect'];
303       $acl= array($this->config->current['BASE'] => ":all");
304       $res= get_list($acl, "(&(objectClass=goFaxAccount)$filter)", TRUE, $base, array("sn", "givenName", "facsimileTelephoneNumber"), TRUE);
306       foreach ($res as $attrs){
307         $list[$attrs['facsimileTelephoneNumber'][0]]=
308           $attrs['sn'][0].", ".
309           $attrs['givenName'][0]." [".
310           $attrs['facsimileTelephoneNumber'][0]."]";
311       }
313       /* Show dialog */
314       $smarty->assign("search_image", get_template_path('images/search.png'));
315       $smarty->assign("tree_image", get_template_path('images/tree.png'));
316       $smarty->assign("infoimage", get_template_path('images/info.png'));
317       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
318       $smarty->assign("departments", $this->config->idepartments);
319       $smarty->assign("list", $list);
320       if (isset($_POST['depselect'])){
321         $smarty->assign("depselect", $_POST['depselect']);
322       }
323       $smarty->assign("deplist", $this->config->idepartments);
324       $smarty->assign("apply", apply_filter());
325       $smarty->assign("alphabet", generate_alphabet());
326       $smarty->assign("hint", print_sizelimit_warning());
327       foreach( array("depselect", "fuser", "regex") as $type){
328         $smarty->assign("$type", $faxfilter[$type]);
329       }
330       $smarty->assign("hint", print_sizelimit_warning());
332       $display.= $smarty->fetch (get_template_path('locals.tpl', TRUE, dirname(__FILE__)));
333       return ($display);
334     }
336     /* Manage incoming blocklists */
337     if ($this->in_blocklist_dialog){
338       $ldap->search ("(objectClass=goFaxRBlock)");
339       while ($attrs= $ldap->fetch()){
340         $list[$attrs['cn'][0]]=
341           $attrs['description'][0].
342           " [".$attrs['cn'][0]."]";
343       }
345       /* Show dialog */
346       $smarty->assign("cblocklist", $this->current_blocklist);
347       $smarty->assign("goFaxBlockListACL", chkacl($this->acl, "goFaxBlockList"));
348       $smarty->assign("departments", $this->config->idepartments);
349       $smarty->assign("list", $list);
350       
351       if (isset($_POST['department'])){
352         $smarty->assign("department", $_POST['department']);
353       } else {
354         $smarty->assign("department", "");
355       }
357       if(count($this->current_blocklist))   {
358         $smarty->assign("SELECT_cblocklist",true);
359       }else {
360         $smarty->assign("SELECT_cblocklist",false);
361       }
363       if(count($list))   {
364         $smarty->assign("SELECT_list",true);
365       }else {
366         $smarty->assign("SELECT_list",false);
367       }
372       $display.= $smarty->fetch (get_template_path('lists.tpl', TRUE, dirname(__FILE__)));
373       return ($display);
374     }
376     /* Manage outgoing blocklists */
377     if ($this->out_blocklist_dialog){
378       $ldap->search ("(objectClass=goFaxSBlock)");
379       while ($attrs= $ldap->fetch()){
380         if (isset($attrs['description'][0])){
381           $list[$attrs['cn'][0]]= $attrs['cn'][0].
382             " [".$attrs['description'][0]."]";
383         } else {
384           $list[$attrs['cn'][0]]= $attrs['cn'][0];
385         }
386       }
388       /* Show dialog */
389       $smarty->assign("cblocklist", $this->current_blocklist);
390       $smarty->assign("goFaxBlockListACL", chkacl($this->acl, "goFaxBlockList"));
391       $smarty->assign("departments", $this->config->idepartments);
392       $smarty->assign("list", $list);
393       if (isset($_POST['department'])){
394         $smarty->assign("department", $_POST['department']);
395       } else {
396         $smarty->assign("department", "");
397       }
398  
399       if(count($this->current_blocklist))   {
400         $smarty->assign("SELECT_cblocklist",true);
401       }else {
402         $smarty->assign("SELECT_cblocklist",false);
403       }
405       if(count($list))   {
406         $smarty->assign("SELECT_list",true);
407       }else {
408         $smarty->assign("SELECT_list",false);
409       }
411       $display.= $smarty->fetch (get_template_path('lists.tpl', TRUE, dirname(__FILE__)));
412       return ($display);
413     }
416     /* Show main page */
417     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
418     $smarty->assign("formats", $this->config->data['MAIN']['FAXFORMATS']);
419     $smarty->assign("printers", $this->printerList);
421     /* Load attributes */
422     foreach(array("goFaxIsEnabled", "goFaxDeliveryMode", "facsimileTelephoneNumber",
423           "goFaxPrinter", "goFaxLanguage", "goFaxFormat", 
424           "facsimileAlternateTelephoneNumber", "mail") as $val){
426       $smarty->assign("$val", $this->$val);
427       $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
428     }
429     $smarty->assign("goFaxRBlockgroupsACL", chkacl($this->acl, "goFaxRBlockgroups"));
430     $smarty->assign("goFaxSBlockgroupsACL", chkacl($this->acl, "goFaxSBlockgroups"));
432     /* Load checkboxes */
433     if ($this->goFaxIsEnabled == "1"){
434       $smarty->assign("goFaxIsEnabled", "");
435     } else {
436       $smarty->assign("goFaxIsEnabled", "checked");
437     }
438     /* goFaxAccount has "mail" as must! Block if no mailaddress is specified... */
439     if ($this->goFaxDeliveryMode & 32) {
440       $smarty->assign("fax_to_mail", "checked");
441     } else {
442       $smarty->assign("fax_to_mail", "");
443     }
444     if ($this->goFaxDeliveryMode & 64) {
445       $smarty->assign("fax_to_printer", "checked");
446     } else {
447       $smarty->assign("fax_to_printer", "");
448     }
450     if(count($this->facsimileAlternateTelephoneNumber))   {
451       $smarty->assign("SELECT_facsimileAlternateTelephoneNumber",true);
452     }else {
453       $smarty->assign("SELECT_facsimileAlternateTelephoneNumber",false);
454     }
457     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
458     return ($display);
459   }
461   function remove_from_parent()
462   {
463     /* Cancel if there's nothing to do here */
464     if (!$this->initially_was_account){
465       return;
466     }
468     plugin::remove_from_parent();
470     /* Zero out arrays */
471     foreach (array("goFaxRBlocklist", "goFaxRBlockgroups", "goFaxSBlocklist",
472           "goFaxSBlockgroups", "facsimileAlternateTelephoneNumber") as $val){
474       $this->attrs[$val]= array();
475     }
477     /* Adapt mail settings if needed */
478     if ($this->parent->by_object['mailAccount']->is_account){
479       unset($this->attrs['mail']);
480     }
482     $ldap= $this->config->get_ldap_link();
483     $ldap->cd($this->dn);
484     $ldap->modify($this->attrs);
485     show_ldap_error($ldap->get_error());
487     /* Optionally execute a command after we're done */
488     $this->handle_post_events('remove');
489   }
492   /* Check formular input */
493   function check()
494   {
495     /* Reset message array */
496     $message= array();
498     /* must: facsimileTelephoneNumber */
499     if ($this->facsimileTelephoneNumber == ""){
500       $message[]= _("The required field 'Fax' is not set.");
501     }
503     if (!is_phone_nr($this->facsimileTelephoneNumber)){
504       $message[]= _("Please enter a valid telephone number in the 'Fax' field.");
505     }
507     /* IF mail is specified (which is only the case if there's no mail account
508        present), check if it's valid..  */
509     if (@isset($this->parent->by_object['mailAccount']) &&
510         $this->goFaxDeliveryMode && 32){
511       if ($this->mail == ""){
512         $message[]= _("Mail delivery is checked, but no address has been specified.");
513       } elseif (!is_email($this->mail)){
514         $message[]= _("The mail address you've entered is invalid.");
515       }
516     }
518     return ($message);
519   }
521   /* Save data to object */
522   function save_object()
523   {
524     if (isset($_POST['faxTab'])){
525       plugin::save_object();
527       /* Adapt combobox values */
528       $tmp= 0;
529       if (isset($_POST["fax_to_mail"]) && $_POST["fax_to_mail"] == 1){
530         $tmp+= 32;
531       }
532       if (isset($_POST["fax_to_printer"]) && $_POST["fax_to_printer"] == 1){
533         $tmp+= 64;
534       }
535       if (chkacl ($this->acl, "goFaxIsEnabled") == ""){
536         if (isset($_POST["goFaxIsEnabled"]) && $_POST["goFaxIsEnabled"] == "1"){
537           $this->goFaxIsEnabled= "0";
538         } else {
539           $this->goFaxIsEnabled= "1";
540         }
541       }
543       if (isset($_POST['facsimileTelephoneNumber'])){
544         if ($_POST['facsimileTelephoneNumber'] != $this->facsimileTelephoneNumber){
545           $this->is_modified= TRUE;
546         }
547         $this->facsimileTelephoneNumber= $_POST['facsimileTelephoneNumber'];
548       }
550       if (isset($_POST['mail'])){
551         if ($this->mail != $_POST['mail']){
552           $this->is_modified= TRUE;
553         }
554         $this->mail= $_POST['mail'];
555       }
557       /* Write to object */
558       if (chkacl ($this->acl, "goFaxDeliveryMode") == ""){
559         if ($tmp != $this->goFaxDeliveryMode){
560             $this->is_modified= TRUE;
561         }
562         $this->goFaxDeliveryMode= "$tmp";
563       }
565       /* Check if mail account is active and correct the internal
566          reference to represent the current status. */
567       if ($this->parent->by_object['mailAccount']->is_account){
568         $this->has_mailAccount= TRUE;
569       }
570     }
572   }
575   /* Save to LDAP */
576   function save()
577   {
578     plugin::save();
580     /* Save arrays */
581     foreach (array("goFaxRBlocklist", "goFaxRBlockgroups", "goFaxSBlocklist",
582           "goFaxSBlockgroups", "facsimileAlternateTelephoneNumber") as $val){
584       $this->attrs[$val]= $this->$val;
585     }
587     /* Adapt mail settings if needed */
588     unset($this->attrs['mail']);
589     if (!$this->has_mailAccount && $this->goFaxDeliveryMode && 32){
590       $this->attrs['mail']= $this->mail;
591     }
593     /* Write back to ldap */
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     if ($this->initially_was_account == $this->is_account){
601       if ($this->is_modified){
602         $this->handle_post_events("mofify");
603       }
604     } else {
605       $this->handle_post_events("add");
606     }
608   }
611   /* Adapt from template, using 'dn' */
612   function adapt_from_template($dn)
613   {
614     plugin::adapt_from_template($dn);
616     foreach (array("goFaxRBlocklist", "goFaxRBlockgroups", "goFaxSBlocklist",
617           "goFaxSBlockgroups", "facsimileAlternateTelephoneNumber") as $val){
619       if (isset($this->attrs[$val])){
620         $this->$val= $this->attrs[$val];
621       }
622     }
623   }
627   /* Add alternate fax recipient */
628   function addAlternate($number)
629   {
630     $this->facsimileAlternateTelephoneNumber[]= "$number";
631     $this->facsimileAlternateTelephoneNumber=
632       array_unique ($this->facsimileAlternateTelephoneNumber);
634     sort ($this->facsimileAlternateTelephoneNumber);
635     reset ($this->facsimileAlternateTelephoneNumber);
636   }
638   function delAlternate($numbers)
639   {
640     $this->facsimileAlternateTelephoneNumber= array_remove_entries ($numbers,
641         $this->facsimileAlternateTelephoneNumber);
642   }
647 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
648 ?>