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","kolabDelegate");
22 var $objectclasses = array("kolabInetOrgPerson");
24 /* Helper */
25 var $imapping= array();
28 function kolabAccount ($config, $dn= NULL)
29 {
30 plugin::plugin ($config, $dn);
32 /* Pull arrays */
33 foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
34 if (isset($this->attrs["$attr"]["count"])){
35 $tmp = array();
36 for ($i= 0; $i<$this->attrs["$attr"]["count"]; $i++){
37 $tmp[]=$this->attrs["$attr"][$i];
38 }
39 $this->$attr = $tmp;
40 }
41 }
43 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
44 if (count ($this->kolabInvitationPolicy) == 0){
45 $this->kolabInvitationPolicy= array("ACT_MANUAL");
46 }
48 /* Check is account state */
49 $this->is_account = false;
50 if(count($this->kolabDelegate)){
51 $this->is_account = true;
52 }
53 foreach(array("calFBURL","unrestrictedMailSize") as $attr){
54 if(!empty($this->$attr)){
55 $this->is_account = true;
56 }
57 }
58 }
61 function execute()
62 {
63 /* Call parent execute */
64 plugin::execute();
66 /* Show tab dialog headers */
67 $display= "";
69 /* Show main page */
70 $smarty= get_smarty();
72 /* Load attributes */
73 foreach($this->attributes as $val){
74 $smarty->assign("$val", $this->$val);
75 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
76 }
78 /* Check for invitation action */
79 $nr= 0;
80 while (isset($_POST["policy$nr"])){
81 if (isset($_POST["add$nr"])){
82 $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
83 }
84 if (isset($_POST["remove$nr"])){
85 $new= array();
86 foreach ($this->kolabInvitationPolicy as $entry){
87 if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
88 $new[]= $entry;
89 }
90 }
91 $this->kolabInvitationPolicy= $new;
92 }
93 $nr++;
94 }
96 /* Unify addresses */
97 $new= array();
98 foreach($this->kolabInvitationPolicy as $value){
99 $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
100 $new[$address]= $value;
101 }
102 $this->kolabInvitationPolicy= array();
103 foreach($new as $value){
104 $this->kolabInvitationPolicy[]= $value;
105 }
107 /* Add delegation */
108 if (isset($_POST['add_delegation'])){
109 if ($_POST['delegate_address'] != ""){
111 /* Valid email address specified? */
112 $address= $_POST['delegate_address'];
113 $valid= FALSE;
114 if (!is_email($address)){
115 if (!is_email($address, TRUE)){
116 print_red (_("You're trying to add an invalid email address to the list of delegations."));
117 }
118 } else {
120 $ldap= $this->config->get_ldap_link();
121 $ldap->cd ($this->config->current['BASE']);
122 $ldap->search('(mail='.$address.')',array("mail"));
123 if ($ldap->count() == 0){
124 print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
125 } else {
126 $valid= TRUE;
127 }
128 }
130 if ($valid){
131 /* Add it */
132 if (chkacl ($this->acl, "kolabDelegate") == ""){
133 $this->addDelegate ($address);
134 $this->is_modified= TRUE;
135 }
137 }
138 }
139 }
141 /* Delete forward email addresses */
142 if (isset($_POST['delete_delegation'])){
143 if (count($_POST['delegate_list'])
144 && chkacl ($this->acl, "kolabDelegate") == ""){
146 $this->delDelegate ($_POST['delegate_list']);
147 }
148 }
150 /* Assemble policies */
151 $policies= array( 'ACT_ALWAYS_ACCEPT' => _("Always accept"),
152 'ACT_ALWAYS_REJECT' => _("Always reject"),
153 'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
154 'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
155 'ACT_MANUAL' => _("Manual"));
156 $smarty->assign('policies', $policies);
158 /* Adjust checkbox */
159 if ($this->unrestrictedMailSize){
160 $smarty->assign('unrestrictedMailSizeState', "checked");
161 } else {
162 $smarty->assign('unrestrictedMailSizeState', "");
163 }
165 /* Transfer account states for this union */
166 if (isset($this->parent) && $this->parent->by_object['mailAccount']->is_account){
167 $smarty->assign('mail_account', 'true');
168 } else {
169 $smarty->assign('mail_account', '');
170 }
172 /* Transfer delegation list */
173 if (!count($this->kolabDelegate)){
174 /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
175 $smarty->assign("kolabDelegate", array());
176 } else {
177 $smarty->assign("kolabDelegate", $this->kolabDelegate);
178 }
179 $smarty->assign("kolabDelegateACL", chkacl($this->acl, $this->kolabDelegate));
181 /* Create InvitationPolicy table */
182 $invitation= "";
183 $this->imapping= array();
184 $nr= 0;
185 $acl= chkacl($this->acl, "kolabInvitationPolicy");
186 $changeState = "";
187 foreach ($this->kolabInvitationPolicy as $entry){
189 $changeState .= "changeState('address".$nr."'); \n changeState('policy".$nr."'); \n
190 changeState('add".$nr."'); \n changeState('remove".$nr."'); \n";
192 $invitation.= "<tr><td>";
193 if($this->is_account){
194 $dis = " ";
195 }else{
196 $dis = " disabled ";
197 }
200 /* The default entry does not have colons... */
201 if (!preg_match('/:/', $entry)){
202 $invitation.= _("Anonymous");
203 $name= "";
204 $mode= $entry;
205 } else {
206 $name= preg_replace('/:.*$/', '', $entry);
207 $mode= preg_replace('/^[^:]*: */', '', $entry);
208 $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 $acl value=\"$name\" id='address".$nr."' ".$dis.">";
209 }
210 $invitation.= "</td>";
212 /* Add mode switch */
213 $invitation.= "<td><select size=\"1\" name=\"policy$nr\" $acl id='policy".$nr."' ".$dis.">";
214 foreach($policies as $key => $value){
215 if ($key == $mode){
216 $invitation.= "<option value=\"$key\" selected>$value</option>";
217 } else {
218 $invitation.= "<option value=\"$key\">$value</option>";
219 }
220 }
222 /* Assign buttons */
223 $button= "";
224 if ($nr == count($this->kolabInvitationPolicy)-1){
225 $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\" id='add".$nr."' ".$dis.">";
226 }
227 if ($nr != 0) {
228 $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\" id='remove".$nr."' ".$dis.">";
229 }
231 $invitation.= "</select> $button</td></tr>\n";
232 $this->imapping[$nr]= $name;
233 $nr++;
234 }
235 $smarty->assign("invitation", $invitation);
236 $smarty->assign("changeState", $changeState);
237 $smarty->assign("kolabState",$this->is_account);
238 $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
241 return ($display);
242 }
244 function remove_from_parent()
245 {
246 /* Optionally execute a command after we're done */
247 plugin::remove_from_parent();
249 if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
250 $this->attrs['objectClass'][] = "kolabInetOrgPerson";
251 }
253 $ldap = $this->config->get_ldap_linK();
254 $ldap->cd($this->config->current['BASE']);
255 $ldap->cd ($this->dn);
256 $ldap->modify($this->attrs);
258 $this->handle_post_events('remove');
259 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/kolab account with dn '%s' failed."),$this->dn));
260 }
263 function check()
264 {
265 /* Call common method to give check the hook */
266 $message= plugin::check();
268 /* FBFuture is in days... */
269 if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
270 $message[]= _("The value specified as Free Busy future needs to be an integer.");
271 }
273 /* Check for URL scheme... */
274 if ($this->calFBURL != "" && !@preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
275 $message[]= _("The value specified as Free Busy Information URL is invalid.");
276 }
278 /* Check invitation policy for existing mail addresses */
279 foreach($this->kolabInvitationPolicy as $policy){
281 /* Ignore anonymous string */
282 if (!preg_match('/:/', $policy)){
283 continue;
284 }
286 $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
287 if (!is_email($address)){
288 if (!is_email($address, TRUE)){
289 $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
290 }
291 } else {
293 $ldap= $this->config->get_ldap_link();
294 $ldap->cd ($this->config->current['BASE']);
295 $ldap->search('(mail='.$address.')',array("mail"));
296 if ($ldap->count() == 0){
297 $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
298 } else {
299 $valid= TRUE;
300 }
301 }
302 }
304 return ($message);
305 }
307 /* Save data to object */
308 function save_object()
309 {
310 /* Do we need to flip is_account state? */
311 if (isset($_POST['connectivityTab'])){
313 if(isset($_POST["kolabState"])){
314 $this->is_account = true;
315 }else{
316 $this->is_account = false;
317 }
319 if (chkacl('unrestrictedMailSize', $this->acl == "")){
320 if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
321 $this->unrestrictedMailSize= 1;
322 } else {
323 $this->unrestrictedMailSize= 0;
324 }
325 }
326 }
328 plugin::save_object();
330 /* Save changes done in invitation policies */
331 $nr= 0;
332 $this->kolabInvitationPolicy= array();
333 while (isset($_POST["policy$nr"])){
335 /* Anonymous? */
336 if (!isset($_POST["address$nr"])){
337 $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
338 } else {
339 $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
340 }
342 $nr++;
343 }
345 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
346 if (count ($this->kolabInvitationPolicy) == 0){
347 $this->kolabInvitationPolicy= array("ACT_MANUAL");
348 }
350 }
353 /* Save to LDAP */
354 function save()
355 {
356 /* Check mailmethod before doing something useful */
357 plugin::save();
359 /* Transfer arrays */
360 $this->attrs['kolabDelegate']= $this->kolabDelegate;
361 $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
363 /* unrestrictedMailSize is boolean */
364 if($this->attrs['unrestrictedMailSize']){
365 $this->attrs['unrestrictedMailSize'] = "TRUE";
366 }else{
367 $this->attrs['unrestrictedMailSize'] = "FALSE";
368 }
370 /* Write back to ldap */
371 $ldap= $this->config->get_ldap_link();
372 $ldap->cd($this->dn);
373 $this->cleanup();
374 $ldap->modify ($this->attrs);
376 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/kolab account with dn '%s' failed."),$this->dn));
378 /* Optionally execute a command after we're done */
379 if ($this->initially_was_account == $this->is_account){
380 if ($this->is_modified){
381 $this->handle_post_events("mofify");
382 }
383 } else {
384 $this->handle_post_events("add");
385 }
386 }
389 /* Add entry to delegation list */
390 function addDelegate($address)
391 {
392 $this->kolabDelegate[]= $address;
393 $this->kolabDelegate= array_unique ($this->kolabDelegate);
395 sort ($this->kolabDelegate);
396 reset ($this->kolabDelegate);
397 $this->is_modified= TRUE;
398 }
400 function delDelegate($addresses)
401 {
402 $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
403 $this->is_modified= TRUE;
404 }
406 }
408 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
409 ?>