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 /* plugin specific values - scalixUserClass */
20 /* MUST */
21 var $scalixScalixObject= TRUE; //TRUE or FALSE
22 var $scalixMailnode= "";
23 /* INDIRECT MUST, scalix will complain if no email was specified */
25 /* MAY */
26 var $scalixAdministrator= FALSE;
27 var $scalixMailboxAdministrator = FALSE;
28 var $scalixServerLanguage= "";
29 var $scalixEmailAddress= array();
30 var $scalixLimitMailboxSize= "";
31 var $scalixLimitOutboundMail= FALSE;
32 var $scalixLimitInboundMail= FALSE;
33 var $scalixLimitNotifyUser= FALSE;
34 var $scalixHideUserEntry= FALSE;
35 var $scalixMailboxClass= "";
37 var $mail ="";
38 var $uid ="";
39 var $default_permissions= "none";
40 var $member_permissions= "post";
41 var $members= array();
42 var $admins= array();
43 var $vacations= array();
44 var $perms= array( "lrs" => "read",
45 "lrsp" => "post",
46 "lrsip" => "append",
47 "lrswipcd" => "write",
48 "lrswipcda" => "all" );
50 /* attribute list for save action */
51 var $attributes = array("scalixMailnode", "scalixServerLanguage", "scalixLimitMailboxSize", "scalixMailboxClass");
52 var $fakeBooleanAttributes= array("scalixAdministrator", "scalixMailboxAdministrator", "scalixLimitOutboundMail",
53 "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry");
54 var $objectclasses = array("scalixUserClass");
57 /* constructor, if 'dn' is set, the node loads the given
58 'dn' from LDAP */
59 function scalixAccount (&$config, $dn= NULL)
60 {
61 /* dn shouldn't be NULL */
62 if($dn === NULL){
63 trigger_error("Initialising scalixAccount without valid dn.");
64 }
66 /* Load bases attributes */
67 plugin::plugin($config, $dn);
69 /* Load attributes */
70 if ($dn != "new"){
72 /* Set user id */
73 if(isset($this->attrs['uid'][0])){
74 $this->uid = $this->attrs['uid'][0];
75 }
77 foreach ($this->fakeBooleanAttributes as $val){
78 if (isset($this->attrs["$val"][0])&&$this->attrs["$val"][0]=="TRUE"){
79 $this->$val = TRUE;
80 } else {
81 $this->$val = FALSE;
82 }
83 }
85 /* Load attributes containing arrays */
86 foreach (array("scalixEmailAddress") as $val){
87 if (isset($this->attrs["$val"]["count"])){
88 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
89 array_push($this->$val, $this->attrs["$val"][$i]);
90 }
91 }
92 }
93 }
95 /* Save initial account state */
96 $this->initially_was_account= $this->is_account;
97 }
100 function execute()
101 {
102 /* Call parent execute */
103 plugin::execute();
105 /* Load templating engine */
106 $smarty= get_smarty();
107 $display= "";
109 /* Do we need to flip is_account state? */
110 if (isset($_POST['modify_state'])){
111 $this->is_account= !$this->is_account;
112 }
114 /* Show main page */
115 $mailboxClasses = array("", "LIMITED", "FULL");
116 $serverLanguages= array("", "GERMAN", "ENGLISH");
118 /* Do we represent a valid account? */
119 if (!$this->is_account && $this->parent === NULL){
120 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
121 _("This account has no scalix extensions.")."</b>";
123 $display.= back_to_main();
124 return ($display);
125 }
127 /* Show tab dialog headers */
128 if ($this->parent !== NULL){
129 if ($this->is_account){
130 $display= $this->show_disable_header(_("Remove scalix account"),
131 _("This account has scalix synchronization enabled. You can disable it by clicking below."));
132 } else {
133 $display= $this->show_enable_header(_("Create scalix account"), _("This account has scalix synchronization disabled. You can enable it by clicking below."));
134 return ($display);
135 }
136 }
138 /* Trigger forward add dialog? */
139 if (isset($_POST['add_local_forwarder'])){
140 $this->forward_dialog= TRUE;
141 $this->dialog= TRUE;
142 }
144 /* Cancel forward add dialog? */
145 if (isset($_POST['add_locals_cancel'])){
146 $this->forward_dialog= FALSE;
147 $this->dialog= FALSE;
148 }
151 $smarty->assign("mailboxClasses", $mailboxClasses);
152 $smarty->assign("serverLanguages", $serverLanguages);
153 foreach(array("perms", "scalixScalixObject", "scalixMailnode", "scalixAdministrator", "scalixMailboxAdministrator",
154 "scalixServerLanguage", "scalixLimitMailboxSize", "scalixLimitOutboundMail", "scalixEmailAddress",
155 "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry", "scalixMailboxClass") as $val){
157 $smarty->assign("$val", $this->$val);
158 $smarty->assign("$val"."ACL", $this->getacl($val));
159 }
161 /* Fill checkboxes */
162 if ($this->scalixAdministrator) {
163 $smarty->assign("scalixAdministrator", "checked");
164 } else {
165 $smarty->assign("scalixAdministrator", "");
166 }
167 if ($this->scalixMailboxAdministrator) {
168 $smarty->assign("scalixMailboxAdministrator", "checked");
169 } else {
170 $smarty->assign("scalixMailboxAdministrator", "");
171 }
172 if ($this->scalixLimitOutboundMail) {
173 $smarty->assign("scalixLimitOutboundMail", "checked");
174 } else {
175 $smarty->assign("scalixLimitOutboundMail", "");
176 }
177 if ($this->scalixLimitInboundMail) {
178 $smarty->assign("scalixLimitInboundMail", "checked");
179 } else {
180 $smarty->assign("scalixLimitInboundMail", "");
181 }
182 if ($this->scalixLimitNotifyUser) {
183 $smarty->assign("scalixLimitNotifyUser", "checked");
184 } else {
185 $smarty->assign("scalixLimitNotifyUser", "");
186 }
187 if ($this->scalixHideUserEntry) {
188 $smarty->assign("scalixHideUserEntry", "checked");
189 } else {
190 $smarty->assign("scalixHideUserEntry", "");
191 }
193 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
194 return ($display);
195 }
198 /* remove object from parent */
199 function remove_from_parent()
200 {
201 /* Cancel if there's nothing to do here */
202 if (!$this->initially_was_account){
203 return;
204 }
206 /* include global link_info */
207 $ldap= $this->config->get_ldap_link();
209 /* Remove and write to LDAP */
210 plugin::remove_from_parent();
212 /* Zero arrays */
213 $this->attrs['scalixEmailAddress']= array();
215 /* Unset fake boolean attributes from entry */
216 foreach ($this->fakeBooleanAttributes as $val){
217 $this->attrs["$val"]= array();
218 }
220 /*unset scalixScalixObject*/
221 $this->attrs['scalixScalixObject']=array();
223 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
224 $ldap->cd($this->dn);
225 $ldap->modify($this->attrs);
226 if (!$ldap->success()){
227 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
228 }
230 /* Optionally execute a command after we're done */
231 $this->handle_post_events("remove");
232 }
235 /* Save data to object */
236 function save_object()
237 {
238 if (isset($_POST['scalixTab'])){
240 /* Save ldap attributes */
241 plugin::save_object();
243 /* Check if given value is not empty */
244 if ($_POST['email_address'] != ""){
245 $valid= FALSE;
247 /* Valid mail address */
248 if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) ||
249 (!$this->is_template && !tests::is_email($_POST['email_address'])) ){
250 msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
251 }else{
252 $valid = TRUE;
253 }
255 /* Check write access */
256 if(!$this->acl_is_writeable("scalixEmailAddress")){
257 msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
258 }else{
260 /* Mail address already in use ? */
261 if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
262 $ui= get_userinfo();
263 if ($user != $ui->username){
264 msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user),
265 ERROR_DIALOG);
266 }
267 }
268 }
269 }
271 /* Delete email addresses */
272 if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
273 if(!$this->acl_is_writeable("scalixEmailAddress")){
274 msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
275 }else{
276 if (count($_POST['emails_list'])){
277 $this->delEmail ($_POST['emails_list']);
278 }
279 }
280 }
282 /* Save fakeBooleanAttributes*/
283 foreach ($this->fakeBooleanAttributes as $val){
284 if ($this->acl_is_writeable($val)) {
285 if( isset ($_POST["$val"])) {
286 $data = TRUE;
287 } else {
288 $data = FALSE;
289 }
290 if ($this->$val != $data){
291 $this->is_modified= TRUE;
292 }
294 $this->$val= $data;
295 }
296 }
297 }
298 }
301 /* Save data to LDAP, depending on is_account we save or delete */
302 function save()
303 {
304 $ldap= $this->config->get_ldap_link();
306 /* Call parents save to prepare $this->attrs */
307 plugin::save();
309 /* Save arrays */
310 $this->attrs['scalixEmailAddress']= $this->scalixEmailAddress;
312 /* Save boolean vars ... the scalix schema expects them as strings */
313 $this->attrs['scalixScalixObject'] = $this->scalixScalixObject?"TRUE":"FALSE";
314 $this->attrs['scalixAdministrator'] = $this->scalixAdministrator?"TRUE":"FALSE";
315 $this->attrs['scalixMailboxAdministrator'] = $this->scalixMailboxAdministrator?"TRUE":"FALSE";
316 $this->attrs['scalixLimitOutboundMail'] = $this->scalixLimitOutboundMail?"TRUE":"FALSE";
317 $this->attrs['scalixLimitInboundMail'] = $this->scalixLimitInboundMail?"TRUE":"FALSE";
318 $this->attrs['scalixLimitNotifyUser'] = $this->scalixLimitNotifyUser?"TRUE":"FALSE";
319 $this->attrs['scalixHideUserEntry'] = $this->scalixHideUserEntry?"TRUE":"FALSE";
321 /* Remove Mailquota if = "" */
322 if((isset($this->attrs['scalixLimitMailboxSize']))&&($this->attrs['scalixLimitMailboxSize']=="")) {
323 $this->attrs['scalixLimitMailboxSize']=array();
324 }
326 /* Save data to LDAP */
327 $ldap->cd($this->dn);
328 $ldap->modify($this->attrs);
329 if (!$ldap->success()){
330 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
331 }
333 /* Optionally execute a command after we're done */
334 if ($this->initially_was_account == $this->is_account){
335 if ($this->is_modified){
336 $this->handle_post_events("modify");
337 }
338 } else {
339 $this->handle_post_events("add");
340 }
342 }
344 /* Check formular input */
345 function check()
346 {
347 $ldap= $this->config->get_ldap_link();
349 $message= array();
351 if(empty($this->scalixMailnode)){
352 $message[]= msgPool::required(_("Mailnode"));
353 }
354 if(empty($this->scalixScalixObject)){
355 $message[]= msgPool::required(_("Scalix object"));
356 }
358 $ldap->cd($this->config->current['BASE']);
359 foreach($this->scalixEmailAddress as $k => $mail) {
360 $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=scalixUserClass)(scalixEmailAddress=".
361 $mail.")(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
363 if ($ldap->count() != 0){
364 $message[]= msgPool::duplicated(_("Email address"));
365 }
366 }
368 /* Check quota */
369 if ($this->scalixLimitMailboxSize != '' && $this->acl_is_writeable("scalixLimitMailboxSize")){
370 if (!is_numeric($this->scalixLimitMailboxSize)) {
371 $message[]= msgPool::invalid(_("Limit Mailbox"));
372 } else {
373 $this->scalixLimitMailboxSize= (int) $this->scalixLimitMailboxSize;
374 }
375 }
377 return ($message);
378 }
380 /* Adapt from template, using 'dn' */
381 function adapt_from_template($dn, $skip= array())
382 {
383 plugin::adapt_from_template($dn, $skip);
385 foreach (array("scalixEmailAddress") as $val){
387 if (in_array($val, $skip)){
388 continue;
389 }
391 $this->$val= array();
392 if (isset($this->attrs["$val"]["count"])){
393 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
394 $value= $this->attrs["$val"][$i];
395 foreach (array("sn", "givenName", "uid") as $repl){
396 if (preg_match("/%$repl/i", $value)){
397 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
398 }
399 }
400 array_push($this->$val, strtolower(rewrite($value)));
401 }
402 }
403 }
404 $this->mail= strtolower(rewrite($this->mail));
405 }
407 function addEmail($address)
408 {
409 $ldap= $this->config->get_ldap_link();
411 $address= strtolower($address);
413 /* Is this address already assigned in LDAP? */
414 $ldap->cd ($this->config->current['BASE']);
415 $ldap->search ("(&(objectClass=scalixUserClass)(|(scalixEmailAddress=$address)".
416 "(scalixEmailAddress=$address)))", array("uid"));
418 if ($ldap->count() > 0){
419 $attrs= $ldap->fetch ();
420 return ($attrs["uid"][0]);
421 }
423 /* Add to list of alternates */
424 if (!in_array($address, $this->scalixEmailAddress)){
425 $this->scalixEmailAddress[]= $address;
426 $this->is_modified= TRUE;
427 }
429 //sort ($this->scalixEmailAddress); //scalix makes the first email the destination, all others can receive
430 reset ($this->scalixEmailAddress);
432 return ("");
433 }
436 function delEmail($addresses)
437 {
438 $this->scalixEmailAddress= array_remove_entries ($addresses, $this->scalixEmailAddress);
439 $this->is_modified= TRUE;
440 }
443 static function plInfo()
444 {
445 return (array(
446 "plDescription" => _("Scalix account"),
447 "plSelfModify" => TRUE,
448 "plDepends" => array("user"),
449 "plPriority" => 2,
450 "plSection" => array("personal" => _("My account")),
451 "plCategory" => array("users"),
452 "plOptions" => array(),
454 "plProvidedAcls" => array(
456 "scalixMailnode" => _("Scalix Mail node"),
457 "scalixMailboxClass" => _("Mailbox class"),
458 "scalixServerLanguage" => _("Server language"),
459 "scalixAdministrator" => _("Administrator"),
460 "scalixMailboxAdministrator"=> _("Mailbox administrator"),
461 "scalixHideUserEntry" => _("Hide user entry in Scalix"),
462 "scalixLimitMailboxSize" => _("Mailbox size limitations"),
463 "scalixLimitOutboundMail" => _("Limit outbound"),
464 "scalixLimitInboundMail" => _("Limit inbound"),
465 "scalixLimitNotifyUser" => _("Notify user"),
466 "scalixEmailAddress" => _("Scalix email addresses"))
468 ));
469 }
472 function saveCopyDialog()
473 {
474 if (isset($_POST['scalixTab'])){
476 /* Add email addresses */
477 if (isset($_POST['add_email'])){
479 /* Check if given value is not empty */
480 if ($_POST['email_address'] != ""){
481 $valid= FALSE;
483 /* Valid mail address */
484 if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) ||
485 (!$this->is_template && !tests::is_email($_POST['email_address'])) ){
486 msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
487 }else{
488 $valid = TRUE;
489 }
491 /* Mail address already in use ? */
492 if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
493 $ui= get_userinfo();
494 if ($user != $ui->username){
495 msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user), ERROR_DIALOG);
496 }
497 }
498 }
499 }
501 /* Delete email addresses */
502 if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
503 if (count($_POST['emails_list'])){
504 $this->delEmail ($_POST['emails_list']);
505 }
506 }
507 }
508 }
511 function PrepareForCopyPaste($source)
512 {
513 plugin::PrepareForCopyPaste($source);
514 foreach ($this->fakeBooleanAttributes as $val){
515 if (isset($source["$val"][0])&&$source["$val"][0]=="TRUE"){
516 $this->$val = TRUE;
517 } else {
518 $this->$val = FALSE;
519 }
520 }
522 /* Load attributes containing arrays */
523 foreach (array("scalixEmailAddress") as $val){
524 if (isset($source["$val"]["count"])){
525 for ($i= 0; $i<$source["$val"]["count"]; $i++){
526 array_push($this->$val, $source["$val"][$i]);
527 }
528 }
529 }
530 }
533 /* Create the posix dialog part for copy & paste */
534 function getCopyDialog()
535 {
536 $smarty = get_smarty();
537 $smarty->assign("scalixEmailAddress",$this->scalixEmailAddress);
538 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
539 $ret = array();
540 $ret['string'] = $str;
541 $ret['status'] = "";
542 return($ret);
544 }
545 }
547 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler
548 ?>