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