1 <?php
2 class kolabAccount extends plugin
3 {
4 /* Definitions */
5 var $plHeadline= "Kolab";
6 var $plDescription= "This does something";
8 /* CLI vars */
9 var $cli_summary= "Manage users Kolab account";
10 var $cli_description= "Some longer text\nfor help";
11 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
13 /* Kolab attributes */
14 var $kolabInvitationPolicy= array();
15 var $kolabFreeBusyFuture= 60;
16 var $unrestrictedMailSize= 0;
17 var $calFBURL= "";
18 var $kolabDelegate= array();
20 /* attribute list for save action */
21 var $attributes= array( "kolabFreeBusyFuture", "unrestrictedMailSize", "calFBURL");
22 var $objectclasses= array();
24 /* Helper */
25 var $imapping= array();
27 function kolabAccount ($config, $dn= NULL)
28 {
29 plugin::plugin ($config, $dn);
31 /* Pull arrays */
32 foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
33 if (isset($this->attrs["$attr"]["count"])){
34 for ($i= 0; $i<$this->attrs["$attr"]["count"]; $i++){
35 array_push($this->$attr, $this->attrs["$attr"][$i]);
36 }
37 }
38 }
40 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
41 if (count ($this->kolabInvitationPolicy) == 0){
42 $this->kolabInvitationPolicy= array("ACT_MANUAL");
43 }
45 }
47 function execute()
48 {
49 /* Call parent execute */
50 plugin::execute();
51 /* Show tab dialog headers */
52 $display= "";
54 /* Show main page */
55 $smarty= get_smarty();
57 /* Load attributes */
58 foreach($this->attributes as $val){
59 $smarty->assign("$val", $this->$val);
60 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
61 }
63 /* Check for invitation action */
64 $nr= 0;
65 while (isset($_POST["policy$nr"])){
66 if (isset($_POST["add$nr"])){
67 $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
68 }
69 if (isset($_POST["remove$nr"])){
70 $new= array();
71 foreach ($this->kolabInvitationPolicy as $entry){
72 if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
73 $new[]= $entry;
74 }
75 }
76 $this->kolabInvitationPolicy= $new;
77 }
78 $nr++;
79 }
81 /* Unify addresses */
82 $new= array();
83 foreach($this->kolabInvitationPolicy as $value){
84 if (preg_match('/^:/', $value)){
85 continue;
86 }
87 $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
88 $new[$address]= $value;
89 }
90 $this->kolabInvitationPolicy= array();
91 foreach($new as $value){
92 $this->kolabInvitationPolicy[]= $value;
93 }
95 /* Add delegation */
96 if (isset($_POST['add_delegation'])){
97 if ($_POST['delegate_address'] != ""){
99 /* Valid email address specified? */
100 $address= $_POST['delegate_address'];
101 $valid= FALSE;
102 if (!is_email($address)){
103 if (!is_email($address, TRUE)){
104 print_red (_("You're trying to add an invalid email address to the list of delegations."));
105 }
106 } else {
108 $ldap= $this->config->get_ldap_link();
109 $ldap->cd ($this->config->current['BASE']);
110 $ldap->search('(mail='.$address.')');
111 if ($ldap->count() == 0){
112 print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
113 } else {
114 $valid= TRUE;
115 }
116 }
118 if ($valid){
119 /* Add it */
120 if (chkacl ($this->acl, "kolabDelegate") == ""){
121 $this->addDelegate ($address);
122 $this->is_modified= TRUE;
123 }
125 }
126 }
127 }
129 /* Delete forward email addresses */
130 if (isset($_POST['delete_delegation'])){
131 if (count($_POST['delegate_list'])
132 && chkacl ($this->acl, "kolabDelegate") == ""){
134 $this->delDelegate ($_POST['delegate_list']);
135 }
136 }
138 /* Assemble policies */
139 $policies= array( 'ACT_ALWAYS_ACCEPT' => _("Always accept"),
140 'ACT_ALWAYS_REJECT' => _("Always reject"),
141 'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
142 'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
143 'ACT_MANUAL' => _("Manual"));
144 $smarty->assign('policies', $policies);
146 /* Adjust checkbox */
147 if ($this->unrestrictedMailSize){
148 $smarty->assign('unrestrictedMailSizeState', "checked");
149 } else {
150 $smarty->assign('unrestrictedMailSizeState', "");
151 }
153 /* Transfer account states for this union */
154 if (isset($this->parent) && $this->parent->by_object['mailAccount']->is_account){
155 $smarty->assign('is_account', 'true');
156 } else {
157 $smarty->assign('is_account', '');
158 }
160 /* Transfer delegation list */
161 if (!count($this->kolabDelegate)){
162 /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
163 $smarty->assign("kolabDelegate", array());
164 } else {
165 $smarty->assign("kolabDelegate", $this->kolabDelegate);
166 }
167 $smarty->assign("kolabDelegateACL", chkacl($this->acl, $this->kolabDelegate));
169 /* Create InvitationPolicy table */
170 $invitation= "";
171 $this->imapping= array();
172 $nr= 0;
173 $acl= chkacl($this->acl, "kolabInvitationPolicy");
174 foreach ($this->kolabInvitationPolicy as $entry){
175 $invitation.= "<tr><td>";
177 /* The default entry does not have colons... */
178 if (!preg_match('/:/', $entry)){
179 $invitation.= _("Anonymous");
180 $name= "";
181 $mode= $entry;
182 } else {
183 $name= preg_replace('/:.*$/', '', $entry);
184 $mode= preg_replace('/^[^:]*: */', '', $entry);
185 $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 $acl value=\"$name\">";
186 }
187 $invitation.= "</td>";
189 /* Add mode switch */
190 $invitation.= "<td><select size=\"1\" name=\"policy$nr\" $acl>";
191 foreach($policies as $key => $value){
192 if ($key == $mode){
193 $invitation.= "<option value=\"$key\" selected>$value</option>";
194 } else {
195 $invitation.= "<option value=\"$key\">$value</option>";
196 }
197 }
199 /* Assign buttons */
200 $button= "";
201 if ($nr == count($this->kolabInvitationPolicy)-1){
202 $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\">";
203 }
204 if ($nr != 0) {
205 $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\">";
206 }
208 $invitation.= "</select> $button</td></tr>\n";
209 $this->imapping[$nr]= $name;
210 $nr++;
211 }
212 $smarty->assign("invitation", $invitation);
214 $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
215 return ($display);
216 }
218 function remove_from_parent()
219 {
220 /* Optionally execute a command after we're done */
221 $this->handle_post_events('remove');
222 }
225 function check()
226 {
227 $message= array();
229 /* FBFuture is in days... */
230 if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
231 $message[]= _("The value specified as Free Busy future needs to be an integer.");
232 }
234 /* Check for URL scheme... */
235 if ($this->calFBURL != "" && !preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
236 $message[]= _("The value specified as Free Busy Information URL is invalid.");
237 }
239 /* Check invitation policy for existing mail addresses */
240 foreach($this->kolabInvitationPolicy as $policy){
242 /* Ignore anonymous string */
243 if (!preg_match('/:/', $policy)){
244 continue;
245 }
247 $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
248 if (!is_email($address)){
249 if (!is_email($address, TRUE)){
250 $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
251 }
252 } else {
254 $ldap= $this->config->get_ldap_link();
255 $ldap->cd ($this->config->current['BASE']);
256 $ldap->search('(mail='.$address.')');
257 if ($ldap->count() == 0){
258 $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
259 } else {
260 $valid= TRUE;
261 }
262 }
263 }
265 return ($message);
266 }
268 /* Save data to object */
269 function save_object()
270 {
271 /* Do we need to flip is_account state? */
272 if (isset($_POST['connectivityTab'])){
273 if (chkacl('unrestrictedMailSize', $this->acl == "")){
274 if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
275 $this->unrestrictedMailSize= 1;
276 } else {
277 $this->unrestrictedMailSize= 0;
278 }
279 }
280 }
282 plugin::save_object();
284 /* Save changes done in invitation policies */
285 $nr= 0;
286 $this->kolabInvitationPolicy= array();
287 while (isset($_POST["policy$nr"])){
289 /* Anonymous? */
290 if (!isset($_POST["address$nr"])){
291 $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
292 } else {
293 $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
294 }
296 $nr++;
297 }
299 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
300 if (count ($this->kolabInvitationPolicy) == 0){
301 $this->kolabInvitationPolicy= array("ACT_MANUAL");
302 }
304 }
307 /* Save to LDAP */
308 function save()
309 {
310 /* Check mailmethod before doing something useful */
311 plugin::save();
313 /* Transfer arrays */
314 $this->attrs['kolabDelegate']= $this->kolabDelegate;
315 $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
317 /* Write back to ldap */
318 $ldap= $this->config->get_ldap_link();
319 $ldap->cd($this->dn);
320 $ldap->modify($this->attrs);
321 show_ldap_error($ldap->get_error());
323 /* Optionally execute a command after we're done */
324 if ($this->initially_was_account == $this->is_account){
325 if ($this->is_modified){
326 $this->handle_post_events("mofify");
327 }
328 } else {
329 $this->handle_post_events("add");
330 }
331 }
334 /* Add entry to delegation list */
335 function addDelegate($address)
336 {
337 $this->kolabDelegate[]= $address;
338 $this->kolabDelegate= array_unique ($this->kolabDelegate);
340 sort ($this->kolabDelegate);
341 reset ($this->kolabDelegate);
342 $this->is_modified= TRUE;
343 }
345 function delDelegate($addresses)
346 {
347 $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
348 $this->is_modified= TRUE;
349 }
351 }
353 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
354 ?>