17 require_once SQ_PACKAGES_PATH.
'/cms/page_templates/page_user_group_manager/page_user_group_manager.inc';
18 require_once SQ_LIB_PATH.
'/html_form/html_form.inc';
19 require_once
'Mail.php';
20 require_once
'Mail/mime.php';
37 var $keywords = Array(
39 'error_messages_list',
40 'success_messages_list',
41 'current_subscriptions',
42 'subscriptions_picker',
43 'unsubscriptions_picker',
47 'subscribe_user_name',
48 'subscribe_email_address',
51 'not_logged_in' => Array(
52 'error_messages_list',
53 'success_messages_list',
54 'subscriptions_picker',
55 'unsubscriptions_picker',
56 'subscribe_user_name',
57 'subscribe_email_address',
71 $this->_ser_attrs = TRUE;
72 parent::__construct($assetid);
87 SQ_LINK_TYPE_2 => Array(
97 SQ_LINK_NOTICE => Array(
100 'exclusive' => FALSE,
102 'paint_layout_page' => Array(
104 'exclusive' => FALSE,
124 if (!parent::_createAdditional($link))
return FALSE;
126 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'folder');
130 'link_type' => SQ_LINK_TYPE_2,
133 'value' =>
'unverified_users',
136 $unverified_users =
new Folder();
137 $unverified_users->setAttrValue(
'name',
'Unverified Users');
138 if (!$unverified_users->create($copy_link)) {
155 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'folder', TRUE,
'unverified_users',
'major',
'1');
156 $folder = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
173 $bind_vars[
'email'] = $value;
175 }
catch (Exception $e) {
176 throw new Exception(
"Unable to get existing bulkmail user due to database error: ".$e->getMessage());
179 if (!$result)
return 0;
195 $locations = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($this->
attr(
'root_node'),
'user_group');
198 $statuses = $this->
attr(
'statuses');
199 if (!empty($statuses)) {
200 $asset_info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo(array_keys($locations));
201 foreach ($asset_info as $assetid => $info) {
202 if (!in_array($info[
'status'], $statuses)) {
203 unset($locations[$assetid]);
209 $available_locations = array_keys($locations);
210 $root_node = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->
attr(
'root_node'));
211 if (($root_node instanceof
User_Group) && (!empty($statuses) && in_array($root_node->status, $statuses))) {
212 $available_locations[] = $root_node->id;
214 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($root_node);
216 return $available_locations;
231 $public_user = $GLOBALS[
'SQ_SYSTEM']->getPublicUser();
232 $email_address = array_get_index($_REQUEST, $this->
getPrefix().
'_email_address',
'');
235 if (!empty($_GET[
'uniqid'])) {
240 if ($GLOBALS[
'SQ_SYSTEM']->currentUser($public_user)) {
245 $current_user = $GLOBALS[
'SQ_SYSTEM']->user;
246 $current_user_email = $current_user->attr(
'email');
249 if (!empty($email_address) && ($email_address != $current_user_email)) {
270 $uniqid = array_get_index($_GET,
'uniqid',
'');
271 if (empty($uniqid))
return;
273 $subscriptions = array_get_index($_GET,
'subscribe', Array());
274 $unsubscriptions = array_get_index($_GET,
'unsubscribe', Array());
275 $subscriptions = empty($subscriptions) ? Array() : explode(
',', $subscriptions);
276 $unsubscriptions = empty($unsubscriptions) ? Array() : explode(
',', $unsubscriptions);
278 if (empty($subscriptions) && empty($unsubscriptions)) {
279 $this->errors[] = translate(
'bm_no_subscription_no_unsubscription');
287 $type_codes = $GLOBALS[
'SQ_SYSTEM']->am->getTypeDescendants(
'user', TRUE);
288 $type_codes[] =
'bulkmail_user';
289 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($unverified_folder->id, NULL, $type_codes, TRUE, $uniqid);
298 $user = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid']);
303 $parents = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getParents($user->id,
'user_group', FALSE));
304 $current_subscriptions = array_intersect($all_mailing_lists, $parents);
308 if ($this->
attr(
'include_root_node')) {
309 if (in_array($this->
attr(
'root_node'), $subscriptions)) $subscriptions = $all_mailing_lists;
310 if (in_array($this->
attr(
'root_node'), $unsubscriptions)) $unsubscriptions = $all_mailing_lists;
313 $bad_subscriptions = array_diff($subscriptions, $all_mailing_lists);
314 $bad_unsubscriptions = array_diff($unsubscriptions, $all_mailing_lists);
316 $good_subscriptions = array_diff($subscriptions, $bad_subscriptions);
317 $good_unsubscriptions = array_diff($unsubscriptions, $bad_unsubscriptions);
319 $curr_plus_new = array_merge($current_subscriptions, $good_subscriptions);
320 $new_minus_old = array_diff($curr_plus_new, $good_unsubscriptions);
321 $new_subscriptions = array_unique($new_minus_old);
323 if (!empty($bad_subscriptions)) {
324 $this->errors[] = translate(
'bsp_bad_subscriptions');
327 if (!empty($bad_unsubscriptions)) {
328 $this->errors[] = translate(
'bsp_bad_unsubscriptions');
333 if (!empty($good_subscriptions) || !empty($good_unsubscriptions)) {
334 $current_subscriptions[] = $unverified_folder->id;
335 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED + SQ_SECURITY_LINK_INTEGRITY);
336 if (!empty($good_subscriptions))
337 $done = $this->
processMinorLinks($link[
'minorid'], $current_subscriptions, $new_subscriptions, $this->
attr(
'link_type'), $uniqid);
339 $done = $this->
processMinorLinks($link[
'minorid'], $current_subscriptions, $new_subscriptions, $this->
attr(
'link_type'));
340 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
344 foreach ($done as $done_type => $done_ids) {
345 foreach ($done_ids as $index => $done_id) {
346 if ($done_id == $unverified_folder->id) {
347 unset($done[$done_type][$index]);
349 if (empty($done[$done_type])) {
350 unset($done[$done_type]);
358 $this->errors[] = translate(
'bsp_no_valid_sub_unsub');
361 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($unverified_folder);
376 $email_address = array_get_index($_REQUEST, $this->
getPrefix().
'_email_address',
'');
377 $user_name = htmlspecialchars(array_get_index($_REQUEST, $this->
getPrefix().
'_user_name',
''));
379 if (empty($email_address)) {
380 if (isset($_REQUEST[$this->
getPrefix().
'_email_address'])) {
381 $this->errors[] = translate(
'bm_email_empty');
386 $subscriptions = array_get_index($_REQUEST, $this->
getPrefix().
'_subscriptions', Array());
387 $unsubscriptions = array_get_index($_REQUEST, $this->
getPrefix().
'_unsubscriptions', Array());
390 $subscription_request = FALSE;
392 if (!empty($subscriptions))
393 $subscription_request = TRUE;
395 if (!is_array($subscriptions)) {
396 if (empty($subscriptions)) {
397 $subscriptions = Array();
399 $subscriptions = Array($subscriptions);
402 if (!is_array($unsubscriptions)) {
403 if (empty($unsubscriptions)) {
404 $unsubscriptions = Array();
406 $unsubscriptions = Array($unsubscriptions);
409 if (empty($subscriptions) && empty($unsubscriptions)) {
410 $this->errors[] = translate(
'bm_no_subscription_no_unsubscription');
414 switch ($this->
attr(
'verify_not_logged_in')) {
416 $sub_verify = !empty($subscriptions);
417 $unsub_verify = !empty($unsubscriptions);
420 $sub_verify = !empty($subscriptions);
421 $unsub_verify = FALSE;
425 $unsub_verify = !empty($unsubscriptions);
429 $sub_verify = $unsub_verify = FALSE;
434 $uniqid = md5(uniqid(
''));
443 $user_parents = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getParents($bulk_userid,
'user_group', FALSE));
444 $currently_subscribed_groups = array_intersect($available_user_groups, $user_parents);
447 if (array_intersect($subscriptions, $currently_subscribed_groups)) {
449 if ($sub_verify === TRUE) {
450 $from_addr = $this->
attr(
'verify_from_address');
451 $from_addr = empty($from_addr) ? SQ_CONF_DEFAULT_EMAIL : $from_addr;
452 if (empty($from_addr)) {
453 trigger_localised_error(
'SYS0315', E_USER_WARNING);
454 trigger_localised_error(
'BML0016', E_USER_WARNING);
459 $this->messages[] = translate(
'bsp_verification_email_sent', $email_address);
464 if ($sub_verify || $unsub_verify) {
465 $from_addr = $this->
attr(
'verify_from_address');
466 $from_addr = empty($from_addr) ? SQ_CONF_DEFAULT_EMAIL : $from_addr;
467 if (empty($from_addr)) {
468 trigger_localised_error(
'SYS0315', E_USER_WARNING);
469 trigger_localised_error(
'BML0016', E_USER_WARNING);
471 require_once SQ_FUDGE_PATH.
'/general/www.inc';
472 $_email_address_trim = trim($email_address);
474 if (valid_email($_email_address_trim) && !empty($_email_address_trim)) {
476 $this->messages[] = translate(
'bsp_verification_email_sent', $email_address);
482 if ($sub_verify && $unsub_verify) {
483 $subscriptions = Array($unverified_folder->id);
484 $unsubscriptions = Array();
487 }
else if ($sub_verify) {
488 $subscriptions = Array($unverified_folder->id);
491 }
else if ($unsub_verify) {
492 $subscriptions[] = $unverified_folder->id;
493 $unsubscriptions = Array();
497 if ((empty($bulk_userid)) && !empty($subscriptions) && $subscription_request) {
500 $unsubscriptions = Array();
502 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'bulkmail_user');
505 $parent_assetid = array_shift($subscriptions);
506 $parent_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($parent_assetid);
507 $link_value = $sub_verify ? $uniqid :
'';
509 'asset' => $parent_asset,
510 'link_type' => $this->
attr(
'link_type'),
511 'value' => $link_value,
514 $creation_succeed = FALSE;
515 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED + SQ_SECURITY_LINK_INTEGRITY);
518 if (!$bulkmail_user->setAttrValue(
'email', $email_address)) {
519 $this->errors[] = translate(
'bm_email_invalid');
523 if (!$bulkmail_user->setAttrValue(
'name', $user_name)) {
524 $this->errors[] = translate(
'bm_username_invalid');
526 $bulkmail_user->create($link);
528 if (!empty($bulkmail_user->id)) {
529 $done = $this->
processMinorLinks($bulkmail_user->id, Array(), $subscriptions, $this->
attr(
'link_type'));
530 $creation_succeed = TRUE;
532 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
534 if ($creation_succeed) {
535 if ($parent_asset->id != $unverified_folder->id) {
536 $done[
'add_success'][] = $parent_assetid;
541 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($parent_asset);
542 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($bulkmail_user);
545 }
else if ($bulk_userid) {
546 $bulkmail_user = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($bulk_userid,
'bulkmail_user');
548 foreach ($subscriptions as $index => $assetid) {
549 if ($assetid == $unverified_folder->id) {
552 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
553 $GLOBALS[
'SQ_SYSTEM']->am->createAssetLink($unverified_folder, $bulkmail_user, $this->
attr(
'link_type'), $uniqid);
554 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
555 unset ($subscriptions[$index]);
560 $parents = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getParents($bulkmail_user->id,
'user_group', FALSE));
561 $current_subscriptions = array_intersect($all_mailing_lists, $parents);
563 $curr_plus_new = array_merge($current_subscriptions, $subscriptions);
564 $new_minus_old = array_diff($curr_plus_new, $unsubscriptions);
565 $new_subscriptions = array_unique($new_minus_old);
567 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED + SQ_SECURITY_LINK_INTEGRITY);
568 $done = $this->
processMinorLinks($bulkmail_user->id, $current_subscriptions, $new_subscriptions, $this->attr(
'link_type'));
569 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
585 $subscriptions = array_get_index($_REQUEST, $this->
getPrefix().
'_subscriptions', Array());
586 $unsubscriptions = array_get_index($_REQUEST, $this->
getPrefix().
'_unsubscriptions', Array());
589 if (!is_array($subscriptions)) {
590 if (empty($subscriptions)) {
591 $subscriptions = Array();
593 $subscriptions = Array($subscriptions);
596 if (!is_array($unsubscriptions)) {
597 if (empty($unsubscriptions)) {
598 $unsubscriptions = Array();
600 $unsubscriptions = Array($unsubscriptions);
603 if (empty($subscriptions) && empty($unsubscriptions)) {
607 switch ($this->
attr(
'verify_logged_in')) {
609 $sub_verify = !empty($subscriptions);
610 $unsub_verify = !empty($unsubscriptions);
613 $sub_verify = !empty($subscriptions);
614 $unsub_verify = FALSE;
618 $unsub_verify = !empty($unsubscriptions);
622 $sub_verify = $unsub_verify = FALSE;
627 $uniqid = md5(uniqid(
''));
629 $current_user = $GLOBALS[
'SQ_SYSTEM']->user;
630 $current_user_email = $current_user->attr(
'email');
631 if (empty($current_user_email)) {
632 $this->errors[] = translate(
'bsp_email_not_configured');
638 $user_parents = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getParents($current_user->id,
'user_group', FALSE));
639 $currently_subscribed_groups = array_intersect($available_user_groups, $user_parents);
642 if (array_intersect($subscriptions, $currently_subscribed_groups)) {
645 if ($sub_verify === TRUE) {
646 $from_addr = $this->
attr(
'verify_from_address');
647 $from_addr = empty($from_addr) ? SQ_CONF_DEFAULT_EMAIL : $from_addr;
648 if (empty($from_addr)) {
649 trigger_localised_error(
'SYS0315', E_USER_WARNING);
650 trigger_localised_error(
'BML0016', E_USER_WARNING);
653 $this->messages[] = translate(
'bsp_verification_email_sent', $current_user_email);
659 if ($sub_verify || $unsub_verify) {
660 $from_addr = $this->
attr(
'verify_from_address');
661 $from_addr = empty($from_addr) ? SQ_CONF_DEFAULT_EMAIL : $from_addr;
662 if (empty($from_addr)) {
663 trigger_localised_error(
'SYS0315', E_USER_WARNING);
664 trigger_localised_error(
'BML0016', E_USER_WARNING);
666 $this->
sendConfirmationEmail($current_user_email, $from_addr, $subscriptions, $unsubscriptions, $uniqid);
667 $this->messages[] = translate(
'bsp_verification_email_sent', $current_user_email);
672 if ($sub_verify && $unsub_verify) {
673 $subscriptions = Array($unverified_folder->id);
674 $unsubscriptions = Array();
677 }
else if ($sub_verify) {
678 $subscriptions = Array($unverified_folder->id);
681 }
else if ($unsub_verify) {
682 $subscriptions[] = $unverified_folder->id;
683 $unsubscriptions = Array();
686 foreach ($subscriptions as $index => $assetid) {
687 if ($assetid == $unverified_folder->id) {
690 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
691 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLinkByAsset($unverified_folder->id, $current_user->id);
693 $GLOBALS[
'SQ_SYSTEM']->am->updateLink($link[
'linkid'], $link[
'link_type'], $uniqid);
695 $GLOBALS[
'SQ_SYSTEM']->am->createAssetLink($unverified_folder, $current_user, $this->
attr(
'link_type'), $uniqid);
697 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
698 unset ($subscriptions[$index]);
703 $parents = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getParents($current_user->id,
'user_group', FALSE));
704 $current_subscriptions = array_intersect($all_mailing_lists, $parents);
706 $curr_plus_new = array_merge($current_subscriptions, $subscriptions);
707 $new_minus_old = array_diff($curr_plus_new, $unsubscriptions);
708 $new_subscriptions = array_unique($new_minus_old);
710 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED + SQ_SECURITY_LINK_INTEGRITY);
711 $done = $this->
processMinorLinks($current_user->id, $current_subscriptions, $new_subscriptions, $this->attr(
'link_type'));
712 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
730 foreach ($fail_success as $message_type => $assetids) {
731 switch ($message_type) {
733 $this->errors[] = translate(
'bsp_add_failed').$this->_getCommaSeparatedNamesByAssetid($assetids);
736 $this->messages[] = translate(
'bsp_add_successful').$this->_getCommaSeparatedNamesByAssetid($assetids);
739 $this->errors[] = translate(
'bsp_remove_failed').$this->_getCommaSeparatedNamesByAssetid($assetids);
741 case 'delete_success':
742 $this->messages[] = translate(
'bsp_remove_successful').$this->_getCommaSeparatedNamesByAssetid($assetids);
765 if (empty($subscriptions) && empty($unsubscriptions)) {
771 'Subject' => translate(
'bsp_verification_email_subject'),
778 'subscribe' => implode(
',', $subscriptions),
779 'unsubscribe' => implode(
',', $unsubscriptions),
783 $url = current_url().
'?';
784 foreach ($get_vars as $key => $value) {
785 $url .=
'&'.$key.
'='.$value;
788 if (!empty($subscriptions) && !empty($unsubscriptions)) {
789 $body .= translate(
'bsp_verification_email_body_sub_unsub',
792 $body .= translate(
'bsp_verification_email_body_part2', $url);
793 }
else if (!empty($subscriptions)) {
795 $body .= translate(
'bsp_verification_email_body_sub_part2', $url);
796 }
else if (!empty($unsubscriptions)) {
798 $body .= translate(
'bsp_verification_email_body_part2', $url);
800 $body .= translate(
'bsp_verification_email_note', $from);
803 $mime =
new Mail_mime(
"\n");
804 $mime->setTXTBody($body);
806 'head_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
807 'text_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
808 'html_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
810 $body = @$mime->get($param);
811 $headers = @$mime->headers($headers);
814 $mail = $mf->factory(
'mail',
"-f$from");
815 $status = @$mail->send($to, $headers, $body);
816 if (PEAR::isError($status)) {
818 trigger_localised_error(
'BML0018', E_USER_WARNING);
839 if (empty($subscriptions)) {
845 'Subject' => translate(
'bsp_verification_email_subject'),
848 $url = current_url();
851 $mime =
new Mail_mime(
"\n");
852 $mime->setTXTBody($body);
854 'head_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
855 'text_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
856 'html_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
858 $body = @$mime->get($param);
859 $headers = @$mime->headers($headers);
862 $mail = $mf->factory(
'mail',
"-f$from");
863 $status = $mail->send($to, $headers, $body);
864 if (PEAR::isError($status)) {
866 trigger_localised_error(
'BML0018', E_USER_WARNING);
883 text_box($this->
getPrefix().
'_email_address',
'', 30);
884 $email_address = ob_get_contents();
886 return $email_address;
900 text_box($this->
getPrefix().
'_user_name',
'', 30);
901 $user_name = ob_get_contents();
942 $current_user = $GLOBALS[
'SQ_SYSTEM']->user;
949 $parents = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getParents($current_user->id,
'user_group', FALSE, NULL, NULL, NULL, 1));
950 $current_subscriptions = array_intersect($all_mailing_lists, $parents);
952 if (!empty($current_subscriptions)) {
954 $info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo(array_values($current_subscriptions), Array(), TRUE,
'name');
955 foreach ($current_subscriptions as $current_subscription) {
956 $string .=
'<li class="'.$this->getPrefix().
'_current_subscriptions">'.$info[$current_subscription].
'</li>';
977 return '<h2>'.translate(
'bsp_default_title_logged_in',
'%user_name%').
'</h2>
978 %success_messages_list% %error_messages_list%
979 <h3>'.translate(
'bsp_default_subtitle_current_subs').
'</h3>
980 %current_subscriptions%
981 <h3>'.translate(
'bsp_default_subtitle_add_sub').
'</h3>
982 %subscriptions_picker%
983 <h3>'.translate(
'bsp_default_subtitle_remove_sub').
'</h3>
984 %unsubscriptions_picker%
985 <br />%commit_button%
987 case 'not_logged_in':
988 return '<h2>'.translate(
'bsp_default_title_not_logged_in').
'</h2>
989 %success_messages_list% %error_messages_list%
990 '.translate(
'username').
': %subscribe_user_name% <br />
991 '.translate(
'email').
': %subscribe_email_address% <br />
992 <h3>'.translate(
'bsp_default_subtitle_add_sub').
'</h3>
993 %subscriptions_picker%
994 <h3>'.translate(
'bsp_default_subtitle_remove_sub').
'</h3>
995 %unsubscriptions_picker%
996 <br />%commit_button%