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("kolab");
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();
52 /* Show tab dialog headers */
53 $display= "";
55 /* Show main page */
56 $smarty= get_smarty();
58 /* Load attributes */
59 foreach($this->attributes as $val){
60 $smarty->assign("$val", $this->$val);
61 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
62 }
64 /* Check for invitation action */
65 $nr= 0;
66 while (isset($_POST["policy$nr"])){
67 if (isset($_POST["add$nr"])){
68 $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
69 }
70 if (isset($_POST["remove$nr"])){
71 $new= array();
72 foreach ($this->kolabInvitationPolicy as $entry){
73 if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
74 $new[]= $entry;
75 }
76 }
77 $this->kolabInvitationPolicy= $new;
78 }
79 $nr++;
80 }
82 /* Unify addresses */
83 $new= array();
84 foreach($this->kolabInvitationPolicy as $value){
85 if (preg_match('/^:/', $value)){
86 continue;
87 }
88 $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
89 $new[$address]= $value;
90 }
91 $this->kolabInvitationPolicy= array();
92 foreach($new as $value){
93 $this->kolabInvitationPolicy[]= $value;
94 }
96 /* Add delegation */
97 if (isset($_POST['add_delegation'])){
98 if ($_POST['delegate_address'] != ""){
100 /* Valid email address specified? */
101 $address= $_POST['delegate_address'];
102 $valid= FALSE;
103 if (!is_email($address)){
104 if (!is_email($address, TRUE)){
105 print_red (_("You're trying to add an invalid email address to the list of delegations."));
106 }
107 } else {
109 $ldap= $this->config->get_ldap_link();
110 $ldap->cd ($this->config->current['BASE']);
111 $ldap->search('(mail='.$address.')',array("mail"));
112 if ($ldap->count() == 0){
113 print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
114 } else {
115 $valid= TRUE;
116 }
117 }
119 if ($valid){
120 /* Add it */
121 if (chkacl ($this->acl, "kolabDelegate") == ""){
122 $this->addDelegate ($address);
123 $this->is_modified= TRUE;
124 }
126 }
127 }
128 }
130 /* Delete forward email addresses */
131 if (isset($_POST['delete_delegation'])){
132 if (count($_POST['delegate_list'])
133 && chkacl ($this->acl, "kolabDelegate") == ""){
135 $this->delDelegate ($_POST['delegate_list']);
136 }
137 }
139 /* Assemble policies */
140 $policies= array( 'ACT_ALWAYS_ACCEPT' => _("Always accept"),
141 'ACT_ALWAYS_REJECT' => _("Always reject"),
142 'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
143 'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
144 'ACT_MANUAL' => _("Manual"));
145 $smarty->assign('policies', $policies);
147 /* Adjust checkbox */
148 if ($this->unrestrictedMailSize){
149 $smarty->assign('unrestrictedMailSizeState', "checked");
150 } else {
151 $smarty->assign('unrestrictedMailSizeState', "");
152 }
154 /* Transfer account states for this union */
155 if (isset($this->parent) && $this->parent->by_object['mailAccount']->is_account){
156 $smarty->assign('is_account', 'true');
157 } else {
158 $smarty->assign('is_account', '');
159 }
161 /* Transfer delegation list */
162 if (!count($this->kolabDelegate)){
163 /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
164 $smarty->assign("kolabDelegate", array());
165 } else {
166 $smarty->assign("kolabDelegate", $this->kolabDelegate);
167 }
168 $smarty->assign("kolabDelegateACL", chkacl($this->acl, $this->kolabDelegate));
170 /* Create InvitationPolicy table */
171 $invitation= "";
172 $this->imapping= array();
173 $nr= 0;
174 $acl= chkacl($this->acl, "kolabInvitationPolicy");
175 foreach ($this->kolabInvitationPolicy as $entry){
176 $invitation.= "<tr><td>";
178 /* The default entry does not have colons... */
179 if (!preg_match('/:/', $entry)){
180 $invitation.= _("Anonymous");
181 $name= "";
182 $mode= $entry;
183 } else {
184 $name= preg_replace('/:.*$/', '', $entry);
185 $mode= preg_replace('/^[^:]*: */', '', $entry);
186 $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 $acl value=\"$name\">";
187 }
188 $invitation.= "</td>";
190 /* Add mode switch */
191 $invitation.= "<td><select size=\"1\" name=\"policy$nr\" $acl>";
192 foreach($policies as $key => $value){
193 if ($key == $mode){
194 $invitation.= "<option value=\"$key\" selected>$value</option>";
195 } else {
196 $invitation.= "<option value=\"$key\">$value</option>";
197 }
198 }
200 /* Assign buttons */
201 $button= "";
202 if ($nr == count($this->kolabInvitationPolicy)-1){
203 $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\">";
204 }
205 if ($nr != 0) {
206 $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\">";
207 }
209 $invitation.= "</select> $button</td></tr>\n";
210 $this->imapping[$nr]= $name;
211 $nr++;
212 }
213 $smarty->assign("invitation", $invitation);
215 $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
216 return ($display);
217 }
219 function remove_from_parent()
220 {
221 /* Optionally execute a command after we're done */
222 $this->handle_post_events('remove');
223 }
226 function check()
227 {
228 /* Call common method to give check the hook */
229 $message= plugin::check();
231 /* FBFuture is in days... */
232 if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
233 $message[]= _("The value specified as Free Busy future needs to be an integer.");
234 }
236 /* Check for URL scheme... */
237 if ($this->calFBURL != "" && !@preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
238 $message[]= _("The value specified as Free Busy Information URL is invalid.");
239 }
241 /* Check invitation policy for existing mail addresses */
242 foreach($this->kolabInvitationPolicy as $policy){
244 /* Ignore anonymous string */
245 if (!preg_match('/:/', $policy)){
246 continue;
247 }
249 $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
250 if (!is_email($address)){
251 if (!is_email($address, TRUE)){
252 $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
253 }
254 } else {
256 $ldap= $this->config->get_ldap_link();
257 $ldap->cd ($this->config->current['BASE']);
258 $ldap->search('(mail='.$address.')',array("mail"));
259 if ($ldap->count() == 0){
260 $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
261 } else {
262 $valid= TRUE;
263 }
264 }
265 }
267 return ($message);
268 }
270 /* Save data to object */
271 function save_object()
272 {
273 /* Do we need to flip is_account state? */
274 if (isset($_POST['connectivityTab'])){
275 if (chkacl('unrestrictedMailSize', $this->acl == "")){
276 if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
277 $this->unrestrictedMailSize= 1;
278 } else {
279 $this->unrestrictedMailSize= 0;
280 }
281 }
282 }
284 plugin::save_object();
286 /* Save changes done in invitation policies */
287 $nr= 0;
288 $this->kolabInvitationPolicy= array();
289 while (isset($_POST["policy$nr"])){
291 /* Anonymous? */
292 if (!isset($_POST["address$nr"])){
293 $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
294 } else {
295 $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
296 }
298 $nr++;
299 }
301 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
302 if (count ($this->kolabInvitationPolicy) == 0){
303 $this->kolabInvitationPolicy= array("ACT_MANUAL");
304 }
306 }
309 /* Save to LDAP */
310 function save()
311 {
312 /* Check mailmethod before doing something useful */
313 plugin::save();
315 /* Transfer arrays */
316 $this->attrs['kolabDelegate']= $this->kolabDelegate;
317 $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
319 /* Write back to ldap */
320 $ldap= $this->config->get_ldap_link();
321 $ldap->cd($this->dn);
322 $this->cleanup();
323 $ldap->modify ($this->attrs);
325 show_ldap_error($ldap->get_error(), _("Saving Kolab account failed"));
327 /* Optionally execute a command after we're done */
328 if ($this->initially_was_account == $this->is_account){
329 if ($this->is_modified){
330 $this->handle_post_events("mofify");
331 }
332 } else {
333 $this->handle_post_events("add");
334 }
335 }
338 /* Add entry to delegation list */
339 function addDelegate($address)
340 {
341 $this->kolabDelegate[]= $address;
342 $this->kolabDelegate= array_unique ($this->kolabDelegate);
344 sort ($this->kolabDelegate);
345 reset ($this->kolabDelegate);
346 $this->is_modified= TRUE;
347 }
349 function delDelegate($addresses)
350 {
351 $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
352 $this->is_modified= TRUE;
353 }
355 }
357 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
358 ?>