Code

Added empty lines
[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         /* Call parent execute */
88         plugin::execute();
90     /* Load smarty stuff */
91     $smarty= get_smarty();
93     /* Check if mail account is active. We'll display an additional
94        mail address field if this is not the case. Some people may
95        want goFax, but have a mailserver which is not managed with
96        GOsa */
97     if (!@isset($this->parent->by_object['mailAccount'])) {
98       $smarty->assign("has_mailaccount", $this->has_mailAccount?"true":"false");
99     } elseif ( !$this->parent->by_object['mailAccount']->is_account){
100       $smarty->assign("has_mailaccount", "false");
101       $this->has_mailAccount= false;
102     } else {
103       $smarty->assign("has_mailaccount", "true");
104     }
106     /* Do we need to flip is_account state? */
107     if (isset($_POST['modify_state'])){
108       $this->is_account= !$this->is_account;
109     }
111     /* Do we represent a valid account? */
112     if (!$this->is_account && $this->parent == NULL){
113       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
114         _("This account has no fax extensions.")."</b>";
115       $display.= back_to_main();
116       return ($display);
117     }
119     /* Show tab dialog headers */
120     $display= "";
121     if ($this->parent != NULL){
122       if ($this->is_account){
123         $display= $this->show_header(_("Remove fax account"),
124             _("This account has fax features enabled. You can disable them by clicking below."));
125       } else {
126         $display= $this->show_header(_("Create fax account"),
127             _("This account has fax features disabled. You can enable them by clicking below."));
128         return ($display);
129       }
130     }
132     /* Trigger Add local fax alternatives dialog */
133     if (isset($_POST['add_local_alternate'])){
134       $this->locals_dialog= TRUE;       
135       $this->dialog= TRUE;      
136     }
138     /* Add alternatives from dialog */
139     if (isset($_POST['add_locals_finish'])){
140       if (isset($_POST['local_list']) &&
141           chkacl ($this->acl, "facsimileAlternateTelephoneNumber") == ""){
143         foreach ($_POST['local_list'] as $val){
144           $this->addAlternate($val);
145           $this->is_modified= TRUE;
146         }
147       }
148     }
150     /* Add alternatives */
151     if (isset($_POST['add_alternate'])){
152       if ($_POST['forward_address'] != "" &&
153           is_phone_nr($_POST['forward_address']) &&
154           chkacl ($this->acl, "facsimileAlternateTelephoneNumber") == ""){
156         $this->addAlternate($_POST['forward_address']);
157       }
158     }
160     /* Delete alternate fax number */
161     if (isset($_POST['delete_alternate'])){
162       if (isset($_POST['alternate_list']) && count($_POST['alternate_list']) &&
163           chkacl ($this->acl, "facsimileAlternateTelephoneNumber") == ""){
165         $this->delAlternate ($_POST['alternate_list']);
166       }
167     }
170     /* Edit incoming blocklists */
171     if (isset($_POST['edit_incoming'])){
172       $this->current_blocklist= array_merge($this->goFaxRBlocklist,
173           $this->goFaxRBlockgroups);
174       sort($this->current_blocklist);
175       reset($this->current_blocklist);
177       $this->in_blocklist_dialog= TRUE;
178       $this->dialog= TRUE;
179     }
181     /* Edit outgoing blocklists */
182     if (isset($_POST['edit_outgoing'])){
183       $this->current_blocklist= array_merge($this->goFaxSBlocklist,
184           $this->goFaxSBlockgroups);
185       sort($this->current_blocklist);
186       reset($this->current_blocklist);
188       $this->out_blocklist_dialog= TRUE;
189       $this->dialog= TRUE;
190     }
192     /* Add number to blocklist (dialog) */
193     if (isset($_POST['add_blocklist_number']) && $_POST['block_number'] != ""){
194       if (!is_phone_nr($_POST['block_number'])){
195         print_red (_("You're trying to add an invalid phone number."));
196       } else {
197         array_push($this->current_blocklist, $_POST['block_number']);
198         $this->current_blocklist= array_unique($this->current_blocklist);
199         sort($this->current_blocklist);
200         reset($this->current_blocklist);
201       }
202     }
204     /* Add list to blocklist */
205     if (isset($_POST['add_blocklist']) && isset($_POST['predefined_list'])){
206       $this->current_blocklist= array_merge($this->current_blocklist,
207           $_POST['predefined_list']);
208       $this->current_blocklist= array_unique($this->current_blocklist);
209       sort($this->current_blocklist);
210       reset($this->current_blocklist);
211     }
213     /* Delete from blocklist */
214     if (isset($_POST['delete_blocklist_number']) && isset($_POST['block_list'])){
215       $tmp= array();
216       foreach($this->current_blocklist as $val){
217         if (!in_array($val, $_POST['block_list'])){
218           $tmp[]= $val;
219         }
220       }
221       $this->current_blocklist= $tmp;
222     }
224     /* Blocklist edit finished */
225     if (isset($_POST['edit_blocklists_finish'])){
227       /* Incoming or outgoing? */
228       if ($this->in_blocklist_dialog){
229         $destlist="goFaxRBlocklist";
230         $destgroup="goFaxRBlockgroups";
231       } else {
232         $destlist="goFaxSBlocklist";
233         $destgroup="goFaxSBlockgroups";
234       }
236       /* Transfer values to ourself */
237       $this->$destlist= array();
238       $this->$destgroup= array();
240       foreach ($this->current_blocklist as $val){
241         if (is_phone_nr($val)){
242           array_push($this->$destlist, $val);
243         } else {
244           array_push($this->$destgroups, $val);
245         }
246       }
248       $this->is_modified= TRUE;
249     }
251     /* Set departments */
252     if ($this->locals_dialog ||
253         $this->in_blocklist_dialog ||
254         $this->out_blocklist_dialog){
256       $list= array ();
257       $ldap= $this->config->get_ldap_link();
258       if (isset ($_POST['department'])){
259         $ldap->cd ($_POST['department']);
260       } else {
261         $ldap->cd ($this->config->current['BASE']);
262       }
263     }
265     /* Cancel  dialogs */
266     if (isset($_POST['add_locals_cancel']) || isset($_POST['edit_blocklists_finish']) ||
267         isset($_POST['edit_blocklists_cancel']) || isset($_POST['add_locals_finish'])){
269       $this->locals_dialog= FALSE;
270       $this->in_blocklist_dialog= FALSE;
271       $this->out_blocklist_dialog= FALSE;
272       $this->dialog= FALSE;
273     }
275     /* Manage locals dialog */
276     if ($this->locals_dialog){
278       /* Save data */
279       $faxfilter= get_global("faxfilter");
280       foreach( array("depselect", "fuser", "regex") as $type){
281         if (isset($_POST[$type])){
282           $faxfilter[$type]= $_POST[$type];
283         }
284       }
285       if (isset($_GET['search'])){
286         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
287         if ($s == "**"){
288           $s= "*";
289         }
290         $faxfilter['fuser']= $s;
291       }
292       register_global("faxfilter", $faxfilter);
294       if ($faxfilter['regex'] != '*' && $faxfilter['regex'] != ""){
295         $regex= $faxfilter['regex'];
296         $filter= "(facimileTelephoneNumber=$regex)";
297       } else {
298         $filter= "";
299       }
300       if ($faxfilter['fuser'] != ""){
301         $user= $faxfilter['fuser'];
302         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
303       }
305       $base= $faxfilter['depselect'];
306       $acl= array($this->config->current['BASE'] => ":all");
307       $res= get_list($acl, "(&(objectClass=goFaxAccount)$filter)", TRUE, $base, array("sn", "givenName", "facsimileTelephoneNumber"), TRUE);
309       foreach ($res as $attrs){
310         $list[$attrs['facsimileTelephoneNumber'][0]]=
311           $attrs['sn'][0].", ".
312           $attrs['givenName'][0]." [".
313           $attrs['facsimileTelephoneNumber'][0]."]";
314       }
316       /* Show dialog */
317       $smarty->assign("search_image", get_template_path('images/search.png'));
318       $smarty->assign("tree_image", get_template_path('images/tree.png'));
319       $smarty->assign("infoimage", get_template_path('images/info.png'));
320       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
321       $smarty->assign("departments", $this->config->idepartments);
322       $smarty->assign("list", $list);
323       if (isset($_POST['depselect'])){
324         $smarty->assign("depselect", $_POST['depselect']);
325       }
326       $smarty->assign("deplist", $this->config->idepartments);
327       $smarty->assign("apply", apply_filter());
328       $smarty->assign("alphabet", generate_alphabet());
329       $smarty->assign("hint", print_sizelimit_warning());
330       foreach( array("depselect", "fuser", "regex") as $type){
331         $smarty->assign("$type", $faxfilter[$type]);
332       }
333       $smarty->assign("hint", print_sizelimit_warning());
335       $display.= $smarty->fetch (get_template_path('locals.tpl', TRUE, dirname(__FILE__)));
336       return ($display);
337     }
339     /* Manage incoming blocklists */
340     if ($this->in_blocklist_dialog){
341       $ldap->search ("(objectClass=goFaxRBlock)");
342       while ($attrs= $ldap->fetch()){
343         $list[$attrs['cn'][0]]=
344           $attrs['description'][0].
345           " [".$attrs['cn'][0]."]";
346       }
348       /* Show dialog */
349       $smarty->assign("cblocklist", $this->current_blocklist);
350       $smarty->assign("goFaxBlockListACL", chkacl($this->acl, "goFaxBlockList"));
351       $smarty->assign("departments", $this->config->idepartments);
352       $smarty->assign("list", $list);
353       
354       if (isset($_POST['department'])){
355         $smarty->assign("department", $_POST['department']);
356       } else {
357         $smarty->assign("department", "");
358       }
362       $display.= $smarty->fetch (get_template_path('lists.tpl', TRUE, dirname(__FILE__)));
363       return ($display);
364     }
366     /* Manage outgoing blocklists */
367     if ($this->out_blocklist_dialog){
368       $ldap->search ("(objectClass=goFaxSBlock)");
369       while ($attrs= $ldap->fetch()){
370         if (isset($attrs['description'][0])){
371           $list[$attrs['cn'][0]]= $attrs['cn'][0].
372             " [".$attrs['description'][0]."]";
373         } else {
374           $list[$attrs['cn'][0]]= $attrs['cn'][0];
375         }
376       }
378       /* Show dialog */
379       $smarty->assign("cblocklist", $this->current_blocklist);
380       $smarty->assign("goFaxBlockListACL", chkacl($this->acl, "goFaxBlockList"));
381       $smarty->assign("departments", $this->config->idepartments);
382       $smarty->assign("list", $list);
383       if (isset($_POST['department'])){
384         $smarty->assign("department", $_POST['department']);
385       } else {
386         $smarty->assign("department", "");
387       }
388  
390       $display.= $smarty->fetch (get_template_path('lists.tpl', TRUE, dirname(__FILE__)));
391       return ($display);
392     }
395     /* Show main page */
396     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
397     $smarty->assign("formats", $this->config->data['MAIN']['FAXFORMATS']);
398     $smarty->assign("printers", $this->printerList);
400     /* Load attributes */
401     foreach(array("goFaxIsEnabled", "goFaxDeliveryMode", "facsimileTelephoneNumber",
402           "goFaxPrinter", "goFaxLanguage", "goFaxFormat", 
403           "facsimileAlternateTelephoneNumber", "mail") as $val){
405       $smarty->assign("$val", $this->$val);
406       $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
407     }
408     $smarty->assign("goFaxRBlockgroupsACL", chkacl($this->acl, "goFaxRBlockgroups"));
409     $smarty->assign("goFaxSBlockgroupsACL", chkacl($this->acl, "goFaxSBlockgroups"));
411     /* Load checkboxes */
412     if ($this->goFaxIsEnabled == "1"){
413       $smarty->assign("goFaxIsEnabled", "");
414     } else {
415       $smarty->assign("goFaxIsEnabled", "checked");
416     }
417     /* goFaxAccount has "mail" as must! Block if no mailaddress is specified... */
418     if ($this->goFaxDeliveryMode & 32) {
419       $smarty->assign("fax_to_mail", "checked");
420     } else {
421       $smarty->assign("fax_to_mail", "");
422     }
423     if ($this->goFaxDeliveryMode & 64) {
424       $smarty->assign("fax_to_printer", "checked");
425     } else {
426       $smarty->assign("fax_to_printer", "");
427     }
430     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
431     return ($display);
432   }
434   function remove_from_parent()
435   {
436     /* Cancel if there's nothing to do here */
437     if (!$this->initially_was_account){
438       return;
439     }
441     plugin::remove_from_parent();
443     /* Zero out arrays */
444     foreach (array("goFaxRBlocklist", "goFaxRBlockgroups", "goFaxSBlocklist",
445           "goFaxSBlockgroups", "facsimileAlternateTelephoneNumber") as $val){
447       $this->attrs[$val]= array();
448     }
450     /* Adapt mail settings if needed */
451     if ($this->parent->by_object['mailAccount']->is_account){
452       unset($this->attrs['mail']);
453     }
455     $ldap= $this->config->get_ldap_link();
456     $ldap->cd($this->dn);
457     $ldap->modify($this->attrs);
458     show_ldap_error($ldap->get_error());
460     /* Optionally execute a command after we're done */
461     $this->handle_post_events('remove');
462   }
465   /* Check formular input */
466   function check()
467   {
468     /* Reset message array */
469     $message= array();
471     /* must: facsimileTelephoneNumber */
472     if ($this->facsimileTelephoneNumber == ""){
473       $message[]= _("The required field 'Fax' is not set.");
474     }
476     if (!is_phone_nr($this->facsimileTelephoneNumber)){
477       $message[]= _("Please enter a valid telephone number in the 'Fax' field.");
478     }
480     /* IF mail is specified (which is only the case if there's no mail account
481        present), check if it's valid..  */
482     if (@isset($this->parent->by_object['mailAccount']) &&
483         $this->goFaxDeliveryMode && 32){
484       if ($this->mail == ""){
485         $message[]= _("Mail delivery is checked, but no address has been specified.");
486       } elseif (!is_email($this->mail)){
487         $message[]= _("The mail address you've entered is invalid.");
488       }
489     }
490  
491     // IE Fix, IE lets you choose disabled option, stupid browser ... 
492     if((empty($this->goFaxPrinter))&&($this->goFaxDeliveryMode & 64)){
493       $message[]= _("Deliver fax to printer, is only possible if valid printer is given. Please correct your choice.");
494     }
496     return ($message);
497   }
499   /* Save data to object */
500   function save_object()
501   {
502     if (isset($_POST['faxTab'])){
503       plugin::save_object();
505       /* Adapt combobox values */
506       $tmp= 0;
507       if (isset($_POST["fax_to_mail"]) && $_POST["fax_to_mail"] == 1){
508         $tmp+= 32;
509       }
510       if (isset($_POST["fax_to_printer"]) && $_POST["fax_to_printer"] == 1){
511         $tmp+= 64;
512       }
513       if (chkacl ($this->acl, "goFaxIsEnabled") == ""){
514         if (isset($_POST["goFaxIsEnabled"]) && $_POST["goFaxIsEnabled"] == "1"){
515           $this->goFaxIsEnabled= "0";
516         } else {
517           $this->goFaxIsEnabled= "1";
518         }
519       }
521       if (isset($_POST['facsimileTelephoneNumber'])){
522         if ($_POST['facsimileTelephoneNumber'] != $this->facsimileTelephoneNumber){
523           $this->is_modified= TRUE;
524         }
525         $this->facsimileTelephoneNumber= $_POST['facsimileTelephoneNumber'];
526       }
528       if (isset($_POST['mail'])){
529         if ($this->mail != $_POST['mail']){
530           $this->is_modified= TRUE;
531         }
532         $this->mail= $_POST['mail'];
533       }
535       /* Write to object */
536       if (chkacl ($this->acl, "goFaxDeliveryMode") == ""){
537         if ($tmp != $this->goFaxDeliveryMode){
538             $this->is_modified= TRUE;
539         }
540         $this->goFaxDeliveryMode= "$tmp";
541       }
543       /* Check if mail account is active and correct the internal
544          reference to represent the current status. */
545       if ($this->parent->by_object['mailAccount']->is_account){
546         $this->has_mailAccount= TRUE;
547       }
548     }
550   }
553   /* Save to LDAP */
554   function save()
555   {
556     plugin::save();
558     /* Save arrays */
559     foreach (array("goFaxRBlocklist", "goFaxRBlockgroups", "goFaxSBlocklist",
560           "goFaxSBlockgroups", "facsimileAlternateTelephoneNumber") as $val){
562       $this->attrs[$val]= $this->$val;
563     }
565     /* Adapt mail settings if needed */
566     unset($this->attrs['mail']);
567     if (!$this->has_mailAccount && $this->goFaxDeliveryMode && 32){
568       $this->attrs['mail']= $this->mail;
569     }
571     /* Write back to ldap */
572     $ldap= $this->config->get_ldap_link();
573     $ldap->cd($this->dn);
574     $ldap->modify($this->attrs);
575     show_ldap_error($ldap->get_error());
577     /* Optionally execute a command after we're done */
578     if ($this->initially_was_account == $this->is_account){
579       if ($this->is_modified){
580         $this->handle_post_events("mofify");
581       }
582     } else {
583       $this->handle_post_events("add");
584     }
586   }
589   /* Adapt from template, using 'dn' */
590   function adapt_from_template($dn)
591   {
592     plugin::adapt_from_template($dn);
594     foreach (array("goFaxRBlocklist", "goFaxRBlockgroups", "goFaxSBlocklist",
595           "goFaxSBlockgroups", "facsimileAlternateTelephoneNumber") as $val){
597       if (isset($this->attrs[$val])){
598         $this->$val= $this->attrs[$val];
599       }
600     }
601   }
605   /* Add alternate fax recipient */
606   function addAlternate($number)
607   {
608     $this->facsimileAlternateTelephoneNumber[]= "$number";
609     $this->facsimileAlternateTelephoneNumber=
610       array_unique ($this->facsimileAlternateTelephoneNumber);
612     sort ($this->facsimileAlternateTelephoneNumber);
613     reset ($this->facsimileAlternateTelephoneNumber);
614   }
616   function delAlternate($numbers)
617   {
618     $this->facsimileAlternateTelephoneNumber= array_remove_entries ($numbers,
619         $this->facsimileAlternateTelephoneNumber);
620   }
625 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
626 ?>