1 <?php
2 /*! \brief scalix plugin
3 \author Jörn Dreyer <gosa.jfd@butonic.de>,
4 based on work by Cajus Pollmeier <pollmeier@gonicus.de>
5 \version 0.01
6 \date 20.10.2006
8 This class provides the functionality to read and write all attributes
9 relevant for scalixAccounts from/to the LDAP. It does syntax checking
10 and displays the formulars required.
11 */
13 class scalixAccount extends plugin
14 {
15 /* Definitions */
16 var $plHeadline = "Scalix";
17 var $plDescription= "This does something";
19 /* CLI vars */
20 var $cli_summary = "Manage users scalix account";
21 var $cli_description= "Some longer text\nfor help";
22 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
24 /* plugin specific values - scalixUserClass */
25 /* MUST */
26 var $scalixScalixObject= TRUE; //TRUE or FALSE
27 var $scalixMailnode= "";
28 /* INDIRECT MUST, scalix will complain if no email was specified */
30 /* MAY */
31 var $scalixAdministrator= FALSE;
32 var $scalixMailboxAdministrator = FALSE;
33 var $scalixServerLanguage= "";
34 var $scalixEmailAddress= array();
35 var $scalixLimitMailboxSize= "";
36 var $scalixLimitOutboundMail= FALSE;
37 var $scalixLimitInboundMail= FALSE;
38 var $scalixLimitNotifyUser= FALSE;
39 var $scalixHideUserEntry= FALSE;
40 var $scalixMailboxClass= "";
42 var $uid ="";
43 var $default_permissions= "none";
44 var $member_permissions= "post";
45 var $members= array();
46 var $admins= array();
47 var $vacations= array();
48 var $perms= array( "lrs" => "read",
49 "lrsp" => "post",
50 "lrsip" => "append",
51 "lrswipcd" => "write",
52 "lrswipcda" => "all" );
54 /* attribute list for save action */
55 var $attributes = array("scalixMailnode", "scalixServerLanguage", "scalixLimitMailboxSize", "scalixMailboxClass");
56 var $fakeBooleanAttributes= array("scalixAdministrator", "scalixMailboxAdministrator", "scalixLimitOutboundMail",
57 "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry");
58 var $objectclasses = array("scalixUserClass");
61 /* constructor, if 'dn' is set, the node loads the given
62 'dn' from LDAP */
63 function scalixAccount ($config, $dn= NULL)
64 {
65 /* dn shouldn't be NULL */
66 if($dn == NULL){
67 trigger_error("Initialising scalixAccount without valid dn.");
68 }
70 /* Load bases attributes */
71 plugin::plugin($config, $dn);
73 /* Load attributes */
74 if ($dn != "new"){
76 /* Set user id */
77 if(isset($this->attrs['uid'][0])){
78 $this->uid = $this->attrs['uid'][0];
79 }
81 foreach ($this->fakeBooleanAttributes as $val){
82 if (isset($this->attrs["$val"][0])&&$this->attrs["$val"][0]=="TRUE"){
83 $this->$val = TRUE;
84 } else {
85 $this->$val = FALSE;
86 }
87 }
89 /* Load attributes containing arrays */
90 foreach (array("scalixEmailAddress") as $val){
91 if (isset($this->attrs["$val"]["count"])){
92 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
93 array_push($this->$val, $this->attrs["$val"][$i]);
94 }
95 }
96 }
97 }
99 /* Save initial account state */
100 $this->initially_was_account= $this->is_account;
101 }
104 function execute()
105 {
106 /* Call parent execute */
107 plugin::execute();
109 /* Load templating engine */
110 $smarty= get_smarty();
111 $display= "";
113 /* Do we need to flip is_account state? */
114 if (isset($_POST['modify_state'])){
115 $this->is_account= !$this->is_account;
116 }
118 /* Show main page */
119 $mailboxClasses = array("", "LIMITED", "FULL");
120 $serverLanguages= array("", "GERMAN", "ENGLISH");
122 /* Do we represent a valid account? */
123 if (!$this->is_account && $this->parent == NULL){
124 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
125 _("This account has no scalix extensions.")."</b>";
127 $display.= back_to_main();
128 return ($display);
129 }
131 /* Show tab dialog headers */
132 if ($this->parent != NULL){
133 if ($this->is_account){
134 $display= $this->show_header(_("Remove scalix account"),
135 _("This account has scalix synchronization enabled. You can disable it by clicking below."));
136 } else {
137 $display= $this->show_header(_("Create scalix account"), _("This account has scalix synchronization disabled. You can enable it by clicking below."));
138 return ($display);
139 }
140 }
142 /* Trigger forward add dialog? */
143 if (isset($_POST['add_local_forwarder'])){
144 $this->forward_dialog= TRUE;
145 $this->dialog= TRUE;
146 }
148 /* Cancel forward add dialog? */
149 if (isset($_POST['add_locals_cancel'])){
150 $this->forward_dialog= FALSE;
151 $this->dialog= FALSE;
152 }
154 /* Add email addresses */
155 if (isset($_POST['add_email'])){
156 if ($_POST['email_address'] != "" &&
157 chkacl ($this->acl, "scalixEmailAddress") == ""){
159 $valid= FALSE;
160 if (!is_email($_POST['email_address'])){
161 if ($this->is_template){
162 if (!(is_email($_POST['email_address'], TRUE))){
163 print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
164 } else {
165 $valid= TRUE;
166 }
167 } else {
168 print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
169 }
171 } else {
172 $valid= TRUE;
173 }
175 if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
176 $ui= get_userinfo();
177 if ($user != $ui->username){
178 print_red (_("The address you're trying to add is already used by user")." '$user'.");
179 }
180 }
181 }
182 }
184 /* Delete email addresses */
185 if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
186 if (count($_POST['emails_list']) &&
187 chkacl ($this->acl, "scalixEmailAddress") == ""){
189 $this->delEmail ($_POST['emails_list']);
190 }
191 }
193 $smarty->assign("mailboxClasses", $mailboxClasses);
194 $smarty->assign("serverLanguages", $serverLanguages);
195 foreach(array("perms", "scalixScalixObject", "scalixMailnode", "scalixAdministrator", "scalixMailboxAdministrator",
196 "scalixServerLanguage", "scalixLimitMailboxSize", "scalixLimitOutboundMail", "scalixEmailAddress",
197 "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry", "scalixMailboxClass") as $val){
199 $smarty->assign("$val", $this->$val);
200 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
201 }
203 /* Fill checkboxes */
204 if ($this->scalixAdministrator) {
205 $smarty->assign("scalixAdministrator", "checked");
206 } else {
207 $smarty->assign("scalixAdministrator", "");
208 }
209 if ($this->scalixMailboxAdministrator) {
210 $smarty->assign("scalixMailboxAdministrator", "checked");
211 } else {
212 $smarty->assign("scalixMailboxAdministrator", "");
213 }
214 if ($this->scalixLimitOutboundMail) {
215 $smarty->assign("scalixLimitOutboundMail", "checked");
216 } else {
217 $smarty->assign("scalixLimitOutboundMail", "");
218 }
219 if ($this->scalixLimitInboundMail) {
220 $smarty->assign("scalixLimitInboundMail", "checked");
221 } else {
222 $smarty->assign("scalixLimitInboundMail", "");
223 }
224 if ($this->scalixLimitNotifyUser) {
225 $smarty->assign("scalixLimitNotifyUser", "checked");
226 } else {
227 $smarty->assign("scalixLimitNotifyUser", "");
228 }
229 if ($this->scalixHideUserEntry) {
230 $smarty->assign("scalixHideUserEntry", "checked");
231 } else {
232 $smarty->assign("scalixHideUserEntry", "");
233 }
235 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
236 return ($display);
237 }
240 /* remove object from parent */
241 function remove_from_parent()
242 {
243 /* Cancel if there's nothing to do here */
244 if (!$this->initially_was_account){
245 return;
246 }
248 /* include global link_info */
249 $ldap= $this->config->get_ldap_link();
251 /* Remove and write to LDAP */
252 plugin::remove_from_parent();
254 /* Zero arrays */
255 $this->attrs['scalixEmailAddress']= array();
257 /* Unset fake boolean attributes from entry */
258 foreach ($this->fakeBooleanAttributes as $val){
259 $this->attrs["$val"]= array();
260 }
262 /*unset scalixScalixObject*/
263 $this->attrs['scalixScalixObject']=array();
265 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
266 $ldap->cd($this->dn);
267 $ldap->modify($this->attrs);
268 show_ldap_error($ldap->get_error());
270 /* Optionally execute a command after we're done */
271 $this->handle_post_events("remove");
272 }
275 /* Save data to object */
276 function save_object()
277 {
278 if (isset($_POST['scalixTab'])){
280 /* Save ldap attributes */
281 plugin::save_object();
283 /* Save fakeBooleanAttributes*/
284 foreach ($this->fakeBooleanAttributes as $val){
285 if (chkacl ($this->acl, "$val") == "") {
286 if( isset ($_POST["$val"])) {
287 $data = TRUE;
288 } else {
289 $data = FALSE;
290 }
291 if ($this->$val != $data){
292 $this->is_modified= TRUE;
293 }
295 $this->$val= $data;
296 }
297 }
298 }
299 }
302 /* Save data to LDAP, depending on is_account we save or delete */
303 function save()
304 {
305 $ldap= $this->config->get_ldap_link();
307 /* Call parents save to prepare $this->attrs */
308 plugin::save();
310 /* Save arrays */
311 $this->attrs['scalixEmailAddress']= $this->scalixEmailAddress;
313 /* Save boolean vars ... the scalix schema expects them as strings */
314 $this->attrs['scalixScalixObject'] = $this->scalixScalixObject?"TRUE":"FALSE";
315 $this->attrs['scalixAdministrator'] = $this->scalixAdministrator?"TRUE":"FALSE";
316 $this->attrs['scalixMailboxAdministrator'] = $this->scalixMailboxAdministrator?"TRUE":"FALSE";
317 $this->attrs['scalixLimitOutboundMail'] = $this->scalixLimitOutboundMail?"TRUE":"FALSE";
318 $this->attrs['scalixLimitInboundMail'] = $this->scalixLimitInboundMail?"TRUE":"FALSE";
319 $this->attrs['scalixLimitNotifyUser'] = $this->scalixLimitNotifyUser?"TRUE":"FALSE";
320 $this->attrs['scalixHideUserEntry'] = $this->scalixHideUserEntry?"TRUE":"FALSE";
322 /* Remove Mailquota if = "" */
323 if((isset($this->attrs['scalixLimitMailboxSize']))&&($this->attrs['scalixLimitMailboxSize']=="")) {
324 $this->attrs['scalixLimitMailboxSize']=array();
325 }
327 /* Save data to LDAP */
328 $ldap->cd($this->dn);
329 $ldap->modify($this->attrs);
330 show_ldap_error($ldap->get_error());
332 /* Optionally execute a command after we're done */
333 if ($this->initially_was_account == $this->is_account){
334 if ($this->is_modified){
335 $this->handle_post_events("modify");
336 }
337 } else {
338 $this->handle_post_events("add");
339 }
341 }
343 /* Check formular input */
344 function check()
345 {
346 $ldap= $this->config->get_ldap_link();
348 $message= array();
350 if(empty($this->scalixMailnode)){
351 $message[]= _("There is no scalix mailnode specified.");
352 }
353 if(empty($this->scalixScalixObject)){
354 $message[]= _("scalixScalixObject must be set!");
355 }
357 $ldap->cd($this->config->current['BASE']);
358 foreach($this->scalixEmailAddress as $k => $mail) {
359 $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=scalixUserClass)(scalixEmailAddress=".
360 $mail.")(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
362 if ($ldap->count() != 0){
363 $message[]= _("The email address '$mail' you've entered is already in use.");
364 }
365 }
367 /* Check quota */
368 if ($this->scalixLimitMailboxSize != '' && chkacl ($this->acl, "scalixLimitMailboxSize") == ""){
369 if (!is_numeric($this->scalixLimitMailboxSize)) {
370 $message[]= _("Value in 'Limit Mailbox size' is not valid.");
371 } else {
372 $this->scalixLimitMailboxSize= (int) $this->scalixLimitMailboxSize;
373 }
374 }
376 return ($message);
377 }
379 /* Adapt from template, using 'dn' */
380 function adapt_from_template($dn)
381 {
382 plugin::adapt_from_template($dn);
384 foreach (array("scalixEmailAddress") as $val){
385 $this->$val= array();
386 if (isset($this->attrs["$val"]["count"])){
387 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
388 $value= $this->attrs["$val"][$i];
389 foreach (array("sn", "givenName", "uid") as $repl){
390 if (preg_match("/%$repl/i", $value)){
391 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
392 }
393 }
394 array_push($this->$val, strtolower(rewrite($value)));
395 }
396 }
397 }
398 $this->mail= strtolower(rewrite($this->mail));
399 }
401 function addEmail($address)
402 {
403 $ldap= $this->config->get_ldap_link();
405 $address= strtolower($address);
407 /* Is this address already assigned in LDAP? */
408 $ldap->cd ($this->config->current['BASE']);
409 $ldap->search ("(&(objectClass=scalixUserClass)(|(scalixEmailAddress=$address)".
410 "(scalixEmailAddress=$address)))", array("uid"));
412 if ($ldap->count() > 0){
413 $attrs= $ldap->fetch ();
414 return ($attrs["uid"][0]);
415 }
417 /* Add to list of alternates */
418 if (!in_array($address, $this->scalixEmailAddress)){
419 $this->scalixEmailAddress[]= $address;
420 $this->is_modified= TRUE;
421 }
423 //sort ($this->scalixEmailAddress); //scalix makes the first email the destination, all others can receive
424 reset ($this->scalixEmailAddress);
426 return ("");
427 }
430 function delEmail($addresses)
431 {
432 $this->scalixEmailAddress= array_remove_entries ($addresses, $this->scalixEmailAddress);
433 $this->is_modified= TRUE;
434 }
435 }
437 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
438 ?>