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 $uid ="";
38 var $default_permissions= "none";
39 var $member_permissions= "post";
40 var $members= array();
41 var $admins= array();
42 var $vacations= array();
43 var $perms= array( "lrs" => "read",
44 "lrsp" => "post",
45 "lrsip" => "append",
46 "lrswipcd" => "write",
47 "lrswipcda" => "all" );
49 /* attribute list for save action */
50 var $attributes = array("scalixMailnode", "scalixServerLanguage", "scalixLimitMailboxSize", "scalixMailboxClass");
51 var $fakeBooleanAttributes= array("scalixAdministrator", "scalixMailboxAdministrator", "scalixLimitOutboundMail",
52 "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry");
53 var $objectclasses = array("scalixUserClass");
56 /* constructor, if 'dn' is set, the node loads the given
57 'dn' from LDAP */
58 function scalixAccount (&$config, $dn= NULL)
59 {
60 /* dn shouldn't be NULL */
61 if($dn === NULL){
62 trigger_error("Initialising scalixAccount without valid dn.");
63 }
65 /* Load bases attributes */
66 plugin::plugin($config, $dn);
68 /* Load attributes */
69 if ($dn != "new"){
71 /* Set user id */
72 if(isset($this->attrs['uid'][0])){
73 $this->uid = $this->attrs['uid'][0];
74 }
76 foreach ($this->fakeBooleanAttributes as $val){
77 if (isset($this->attrs["$val"][0])&&$this->attrs["$val"][0]=="TRUE"){
78 $this->$val = TRUE;
79 } else {
80 $this->$val = FALSE;
81 }
82 }
84 /* Load attributes containing arrays */
85 foreach (array("scalixEmailAddress") as $val){
86 if (isset($this->attrs["$val"]["count"])){
87 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
88 array_push($this->$val, $this->attrs["$val"][$i]);
89 }
90 }
91 }
92 }
94 /* Save initial account state */
95 $this->initially_was_account= $this->is_account;
96 }
99 function execute()
100 {
101 /* Call parent execute */
102 plugin::execute();
104 /* Load templating engine */
105 $smarty= get_smarty();
106 $display= "";
108 /* Do we need to flip is_account state? */
109 if (isset($_POST['modify_state'])){
110 $this->is_account= !$this->is_account;
111 }
113 /* Show main page */
114 $mailboxClasses = array("", "LIMITED", "FULL");
115 $serverLanguages= array("", "GERMAN", "ENGLISH");
117 /* Do we represent a valid account? */
118 if (!$this->is_account && $this->parent === NULL){
119 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
120 _("This account has no scalix extensions.")."</b>";
122 $display.= back_to_main();
123 return ($display);
124 }
126 /* Show tab dialog headers */
127 if ($this->parent !== NULL){
128 if ($this->is_account){
129 $display= $this->show_disable_header(_("Remove scalix account"),
130 _("This account has scalix synchronization enabled. You can disable it by clicking below."));
131 } else {
132 $display= $this->show_enable_header(_("Create scalix account"), _("This account has scalix synchronization disabled. You can enable it by clicking below."));
133 return ($display);
134 }
135 }
137 /* Trigger forward add dialog? */
138 if (isset($_POST['add_local_forwarder'])){
139 $this->forward_dialog= TRUE;
140 $this->dialog= TRUE;
141 }
143 /* Cancel forward add dialog? */
144 if (isset($_POST['add_locals_cancel'])){
145 $this->forward_dialog= FALSE;
146 $this->dialog= FALSE;
147 }
150 $smarty->assign("mailboxClasses", $mailboxClasses);
151 $smarty->assign("serverLanguages", $serverLanguages);
152 foreach(array("perms", "scalixScalixObject", "scalixMailnode", "scalixAdministrator", "scalixMailboxAdministrator",
153 "scalixServerLanguage", "scalixLimitMailboxSize", "scalixLimitOutboundMail", "scalixEmailAddress",
154 "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry", "scalixMailboxClass") as $val){
156 $smarty->assign("$val", $this->$val);
157 $smarty->assign("$val"."ACL", $this->getacl($val));
158 }
160 /* Fill checkboxes */
161 if ($this->scalixAdministrator) {
162 $smarty->assign("scalixAdministrator", "checked");
163 } else {
164 $smarty->assign("scalixAdministrator", "");
165 }
166 if ($this->scalixMailboxAdministrator) {
167 $smarty->assign("scalixMailboxAdministrator", "checked");
168 } else {
169 $smarty->assign("scalixMailboxAdministrator", "");
170 }
171 if ($this->scalixLimitOutboundMail) {
172 $smarty->assign("scalixLimitOutboundMail", "checked");
173 } else {
174 $smarty->assign("scalixLimitOutboundMail", "");
175 }
176 if ($this->scalixLimitInboundMail) {
177 $smarty->assign("scalixLimitInboundMail", "checked");
178 } else {
179 $smarty->assign("scalixLimitInboundMail", "");
180 }
181 if ($this->scalixLimitNotifyUser) {
182 $smarty->assign("scalixLimitNotifyUser", "checked");
183 } else {
184 $smarty->assign("scalixLimitNotifyUser", "");
185 }
186 if ($this->scalixHideUserEntry) {
187 $smarty->assign("scalixHideUserEntry", "checked");
188 } else {
189 $smarty->assign("scalixHideUserEntry", "");
190 }
192 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
193 return ($display);
194 }
197 /* remove object from parent */
198 function remove_from_parent()
199 {
200 /* Cancel if there's nothing to do here */
201 if (!$this->initially_was_account){
202 return;
203 }
205 /* include global link_info */
206 $ldap= $this->config->get_ldap_link();
208 /* Remove and write to LDAP */
209 plugin::remove_from_parent();
211 /* Zero arrays */
212 $this->attrs['scalixEmailAddress']= array();
214 /* Unset fake boolean attributes from entry */
215 foreach ($this->fakeBooleanAttributes as $val){
216 $this->attrs["$val"]= array();
217 }
219 /*unset scalixScalixObject*/
220 $this->attrs['scalixScalixObject']=array();
222 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
223 $ldap->cd($this->dn);
224 $ldap->modify($this->attrs);
225 if (!$ldap->success()){
226 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
227 }
229 /* Optionally execute a command after we're done */
230 $this->handle_post_events("remove");
231 }
234 /* Save data to object */
235 function save_object()
236 {
237 if (isset($_POST['scalixTab'])){
239 /* Save ldap attributes */
240 plugin::save_object();
242 /* Check if given value is not empty */
243 if ($_POST['email_address'] != ""){
244 $valid= FALSE;
246 /* Valid mail address */
247 if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) ||
248 (!$this->is_template && !tests::is_email($_POST['email_address'])) ){
249 msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
250 }else{
251 $valid = TRUE;
252 }
254 /* Check write access */
255 if(!$this->acl_is_writeable("scalixEmailAddress")){
256 msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
257 }else{
259 /* Mail address already in use ? */
260 if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
261 $ui= get_userinfo();
262 if ($user != $ui->username){
263 msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user),
264 ERROR_DIALOG);
265 }
266 }
267 }
268 }
270 /* Delete email addresses */
271 if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
272 if(!$this->acl_is_writeable("scalixEmailAddress")){
273 msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
274 }else{
275 if (count($_POST['emails_list'])){
276 $this->delEmail ($_POST['emails_list']);
277 }
278 }
279 }
281 /* Save fakeBooleanAttributes*/
282 foreach ($this->fakeBooleanAttributes as $val){
283 if ($this->acl_is_writeable($val)) {
284 if( isset ($_POST["$val"])) {
285 $data = TRUE;
286 } else {
287 $data = FALSE;
288 }
289 if ($this->$val != $data){
290 $this->is_modified= TRUE;
291 }
293 $this->$val= $data;
294 }
295 }
296 }
297 }
300 /* Save data to LDAP, depending on is_account we save or delete */
301 function save()
302 {
303 $ldap= $this->config->get_ldap_link();
305 /* Call parents save to prepare $this->attrs */
306 plugin::save();
308 /* Save arrays */
309 $this->attrs['scalixEmailAddress']= $this->scalixEmailAddress;
311 /* Save boolean vars ... the scalix schema expects them as strings */
312 $this->attrs['scalixScalixObject'] = $this->scalixScalixObject?"TRUE":"FALSE";
313 $this->attrs['scalixAdministrator'] = $this->scalixAdministrator?"TRUE":"FALSE";
314 $this->attrs['scalixMailboxAdministrator'] = $this->scalixMailboxAdministrator?"TRUE":"FALSE";
315 $this->attrs['scalixLimitOutboundMail'] = $this->scalixLimitOutboundMail?"TRUE":"FALSE";
316 $this->attrs['scalixLimitInboundMail'] = $this->scalixLimitInboundMail?"TRUE":"FALSE";
317 $this->attrs['scalixLimitNotifyUser'] = $this->scalixLimitNotifyUser?"TRUE":"FALSE";
318 $this->attrs['scalixHideUserEntry'] = $this->scalixHideUserEntry?"TRUE":"FALSE";
320 /* Remove Mailquota if = "" */
321 if((isset($this->attrs['scalixLimitMailboxSize']))&&($this->attrs['scalixLimitMailboxSize']=="")) {
322 $this->attrs['scalixLimitMailboxSize']=array();
323 }
325 /* Save data to LDAP */
326 $ldap->cd($this->dn);
327 $ldap->modify($this->attrs);
328 if (!$ldap->success()){
329 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
330 }
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[]= msgPool::required(_("Mailnode"));
352 }
353 if(empty($this->scalixScalixObject)){
354 $message[]= msgPool::required(_("Scalix object"));
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[]= msgPool::duplicated(_("Email address"));
364 }
365 }
367 /* Check quota */
368 if ($this->scalixLimitMailboxSize != '' && $this->acl_is_writeable("scalixLimitMailboxSize")){
369 if (!is_numeric($this->scalixLimitMailboxSize)) {
370 $message[]= msgPool::invalid(_("Limit Mailbox"));
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, $skip= array())
381 {
382 plugin::adapt_from_template($dn, $skip);
384 foreach (array("scalixEmailAddress") as $val){
386 if (in_array($val, $skip)){
387 continue;
388 }
390 $this->$val= array();
391 if (isset($this->attrs["$val"]["count"])){
392 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
393 $value= $this->attrs["$val"][$i];
394 foreach (array("sn", "givenName", "uid") as $repl){
395 if (preg_match("/%$repl/i", $value)){
396 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
397 }
398 }
399 array_push($this->$val, strtolower(rewrite($value)));
400 }
401 }
402 }
403 $this->mail= strtolower(rewrite($this->mail));
404 }
406 function addEmail($address)
407 {
408 $ldap= $this->config->get_ldap_link();
410 $address= strtolower($address);
412 /* Is this address already assigned in LDAP? */
413 $ldap->cd ($this->config->current['BASE']);
414 $ldap->search ("(&(objectClass=scalixUserClass)(|(scalixEmailAddress=$address)".
415 "(scalixEmailAddress=$address)))", array("uid"));
417 if ($ldap->count() > 0){
418 $attrs= $ldap->fetch ();
419 return ($attrs["uid"][0]);
420 }
422 /* Add to list of alternates */
423 if (!in_array($address, $this->scalixEmailAddress)){
424 $this->scalixEmailAddress[]= $address;
425 $this->is_modified= TRUE;
426 }
428 //sort ($this->scalixEmailAddress); //scalix makes the first email the destination, all others can receive
429 reset ($this->scalixEmailAddress);
431 return ("");
432 }
435 function delEmail($addresses)
436 {
437 $this->scalixEmailAddress= array_remove_entries ($addresses, $this->scalixEmailAddress);
438 $this->is_modified= TRUE;
439 }
442 static function plInfo()
443 {
444 return (array(
445 "plDescription" => _("Scalix account"),
446 "plSelfModify" => TRUE,
447 "plDepends" => array("user"),
448 "plPriority" => 2,
449 "plSection" => array("personal" => _("My account")),
450 "plCategory" => array("users"),
451 "plOptions" => array(),
453 "plProvidedAcls" => array(
455 "scalixMailnode" => _("Scalix Mail node"),
456 "scalixMailboxClass" => _("Mailbox class"),
457 "scalixServerLanguage" => _("Server language"),
458 "scalixAdministrator" => _("Administrator"),
459 "scalixMailboxAdministrator"=> _("Mailbox administrator"),
460 "scalixHideUserEntry" => _("Hide user entry in Scalix"),
461 "scalixLimitMailboxSize" => _("Mailbox size limitations"),
462 "scalixLimitOutboundMail" => _("Limit outbound"),
463 "scalixLimitInboundMail" => _("Limit inbound"),
464 "scalixLimitNotifyUser" => _("Notify user"),
465 "scalixEmailAddress" => _("Scalix email addresses"))
467 ));
468 }
471 function saveCopyDialog()
472 {
473 if (isset($_POST['scalixTab'])){
475 /* Add email addresses */
476 if (isset($_POST['add_email'])){
478 /* Check if given value is not empty */
479 if ($_POST['email_address'] != ""){
480 $valid= FALSE;
482 /* Valid mail address */
483 if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) ||
484 (!$this->is_template && !tests::is_email($_POST['email_address'])) ){
485 msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
486 }else{
487 $valid = TRUE;
488 }
490 /* Mail address already in use ? */
491 if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
492 $ui= get_userinfo();
493 if ($user != $ui->username){
494 msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user), ERROR_DIALOG);
495 }
496 }
497 }
498 }
500 /* Delete email addresses */
501 if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
502 if (count($_POST['emails_list'])){
503 $this->delEmail ($_POST['emails_list']);
504 }
505 }
506 }
507 }
510 function PrepareForCopyPaste($source)
511 {
512 plugin::PrepareForCopyPaste($source);
513 foreach ($this->fakeBooleanAttributes as $val){
514 if (isset($source["$val"][0])&&$source["$val"][0]=="TRUE"){
515 $this->$val = TRUE;
516 } else {
517 $this->$val = FALSE;
518 }
519 }
521 /* Load attributes containing arrays */
522 foreach (array("scalixEmailAddress") as $val){
523 if (isset($source["$val"]["count"])){
524 for ($i= 0; $i<$source["$val"]["count"]; $i++){
525 array_push($this->$val, $source["$val"][$i]);
526 }
527 }
528 }
529 }
532 /* Create the posix dialog part for copy & paste */
533 function getCopyDialog()
534 {
535 $smarty = get_smarty();
536 $smarty->assign("scalixEmailAddress",$this->scalixEmailAddress);
537 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
538 $ret = array();
539 $ret['string'] = $str;
540 $ret['status'] = "";
541 return($ret);
543 }
544 }
546 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler
547 ?>