| 1 |
<?php
|
| 2 |
// $Id: ezmlm.module,v 1.19 2007/12/16 20:43:25 hutch Exp $
|
| 3 |
// $Name: $
|
| 4 |
// for drupal 5.x
|
| 5 |
|
| 6 |
/**
|
| 7 |
* @file
|
| 8 |
* Allows users to subscribe to EZMLM mailing lists via a form in
|
| 9 |
* a block or page. List of mailing lists is defined by adminstrator.
|
| 10 |
* Module sends subscription requests to each list's subscribe address.
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_help().
|
| 15 |
*/
|
| 16 |
function ezmlm_help($section) {
|
| 17 |
switch ($section) {
|
| 18 |
case 'admin/modules#description':
|
| 19 |
return t('Utilities related to ezmlm, such as a box for subscribing to list(s).');
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implementation of hook_link().
|
| 25 |
*/
|
| 26 |
function ezmlm_link($type, $node = NULL, $teaser = FALSE) {
|
| 27 |
$links = array();
|
| 28 |
if ($type == 'page' && user_access('access content')) {
|
| 29 |
$links[] = l(t('mailing %list', array('%list' => format_plural(_ezmlm_get_count(), 'list', 'lists'))), 'ezmlm', array('title' => t('Subscribe to mailing %list', array('%list' => format_plural(_ezmlm_get_count(), 'list', 'lists')))));
|
| 30 |
}
|
| 31 |
return $links;
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Implementation of hook_perm().
|
| 36 |
*/
|
| 37 |
function ezmlm_perm() {
|
| 38 |
return array('administer ezmlm');
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Implementation of hook_menu().
|
| 43 |
*/
|
| 44 |
function ezmlm_menu($may_cache) {
|
| 45 |
$items = array();
|
| 46 |
if ($may_cache) {
|
| 47 |
$items[] = array(
|
| 48 |
'path' => 'ezmlm/add',
|
| 49 |
'title' => ('mailing lists'),
|
| 50 |
'callback' => 'ezmlm_block',
|
| 51 |
'access' => user_access('access content'),
|
| 52 |
'type' => MENU_CALLBACK,
|
| 53 |
);
|
| 54 |
$items[] = array(
|
| 55 |
'path' => 'ezmlm',
|
| 56 |
'title' => ('Mailing lists'),
|
| 57 |
'callback' => 'ezmlm_page',
|
| 58 |
'access' => user_access('access content'),
|
| 59 |
'type' => MENU_NORMAL_ITEM,
|
| 60 |
);
|
| 61 |
$items[] = array(
|
| 62 |
'path' => 'admin/settings/ezmlm',
|
| 63 |
'title' => t('Ezmlm lists'),
|
| 64 |
'description' => t('Ezmlm mailing lists management.'),
|
| 65 |
'access' => user_access('administer ezmlm'),
|
| 66 |
'callback' => 'drupal_get_form',
|
| 67 |
'callback arguments' => 'ezmlm_settings',
|
| 68 |
'type' => MENU_NORMAL_ITEM,
|
| 69 |
);
|
| 70 |
}
|
| 71 |
return $items;
|
| 72 |
}
|
| 73 |
|
| 74 |
/**
|
| 75 |
* Implementation of hook_block().
|
| 76 |
*
|
| 77 |
* Generates a block with the subscription form.
|
| 78 |
*/
|
| 79 |
|
| 80 |
function ezmlm_block($op = 'list', $delta = 0) {
|
| 81 |
switch ($op) {
|
| 82 |
case 'list':
|
| 83 |
$blocks[0]['info'] = t('ezmlm subscribe to list(s)');
|
| 84 |
return $blocks;
|
| 85 |
case 'view':
|
| 86 |
if (arg(0) != 'ezmlm') {
|
| 87 |
switch ($delta) {
|
| 88 |
case 0:
|
| 89 |
$block = "";
|
| 90 |
if (strlen(trim(variable_get('ezmlmmailinglists', '')))) {
|
| 91 |
$block['subject'] = variable_get('ezmlm_block_title', t('Subscriptions'));
|
| 92 |
$block['content'] .= ezmlm_page();
|
| 93 |
}
|
| 94 |
return $block;
|
| 95 |
}
|
| 96 |
}
|
| 97 |
break;
|
| 98 |
case 'configure':
|
| 99 |
case 'save':
|
| 100 |
return;
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
/**
|
| 105 |
* Menu callback; presents the subscription form on a page, or processes
|
| 106 |
* that form's input, whether from block or page.
|
| 107 |
*/
|
| 108 |
function ezmlm_page() {
|
| 109 |
$output = drupal_get_form('_ezmlm_subscribe_form');
|
| 110 |
return $output;
|
| 111 |
}
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Menu callback; presents the settings form for ezmlm
|
| 115 |
*/
|
| 116 |
function ezmlm_settings() {
|
| 117 |
$form = array();
|
| 118 |
// add mailing list
|
| 119 |
$form['ezmlm_add'] = array(
|
| 120 |
'#type' => 'fieldset',
|
| 121 |
'#title' => t('Add mailing lists'),
|
| 122 |
'#description' => t('Add new lists'),
|
| 123 |
'#collapsible' => TRUE,
|
| 124 |
);
|
| 125 |
$form['ezmlm_add']['ezmlm_add_name'] = array(
|
| 126 |
'#type' => 'textfield',
|
| 127 |
'#title' => t('New list name'),
|
| 128 |
'#size' => 20,
|
| 129 |
'#maxlength' => 20,
|
| 130 |
'#required' => FALSE,
|
| 131 |
);
|
| 132 |
$form['ezmlm_add']['ezmlm_add_address'] = array(
|
| 133 |
'#type' => 'textfield',
|
| 134 |
'#title' => t('New list address'),
|
| 135 |
'#size' => 30,
|
| 136 |
'#maxlength' => 50,
|
| 137 |
'#required' => FALSE,
|
| 138 |
);
|
| 139 |
|
| 140 |
// Delete mailing lists
|
| 141 |
if (_ezmlm_get_count() > 0) {
|
| 142 |
$form['ezmlm_delete'] = array(
|
| 143 |
'#type' => 'fieldset',
|
| 144 |
'#title' => t('Remove mailing lists'),
|
| 145 |
'#description' => t('Check the lists you want to remove'),
|
| 146 |
'#collapsible' => TRUE,
|
| 147 |
);
|
| 148 |
|
| 149 |
$lists = variable_get('ezmlmmailinglists', '');
|
| 150 |
if(is_array($lists) ) {
|
| 151 |
foreach ($lists as $list_text => $list_email) {
|
| 152 |
$form['ezmlm_delete'][$list_text] = array(
|
| 153 |
'#type' => 'checkbox',
|
| 154 |
'#title' => "$list_text ($list_email)" ,
|
| 155 |
);
|
| 156 |
}
|
| 157 |
}
|
| 158 |
}
|
| 159 |
$form['ezmlm_misc'] = array(
|
| 160 |
'#type' => 'fieldset',
|
| 161 |
'#title' => t('Various settings'),
|
| 162 |
'#collapsible' => TRUE,
|
| 163 |
);
|
| 164 |
$form['ezmlm_misc']['ezmlm_title'] = array(
|
| 165 |
'#type' => 'textfield',
|
| 166 |
'#title' => t('List title'),
|
| 167 |
'#description' => t('Configure the title of the subscription list'),
|
| 168 |
'#default_value' => variable_get('ezmlm_title', t('Mailing lists')),
|
| 169 |
'#size' => 20,
|
| 170 |
'#maxlength' => 20,
|
| 171 |
'#required' => FALSE,
|
| 172 |
);
|
| 173 |
$form['ezmlm_misc']['ezmlm_block_title'] = array(
|
| 174 |
'#type' => 'textfield',
|
| 175 |
'#title' => t('Block title'),
|
| 176 |
'#description' => t('Configure the title of the block subscription list'),
|
| 177 |
'#default_value' => variable_get('ezmlm_block_title', t('Subscriptions')),
|
| 178 |
'#size' => 20,
|
| 179 |
'#maxlength' => 20,
|
| 180 |
'#required' => FALSE,
|
| 181 |
);
|
| 182 |
return system_settings_form($form);
|
| 183 |
}
|
| 184 |
|
| 185 |
/**
|
| 186 |
* Validate form
|
| 187 |
*/
|
| 188 |
function ezmlm_settings_validate($form_id, $form_values) {
|
| 189 |
|
| 190 |
if (strlen($form_values['ezmlm_add_address'])) {
|
| 191 |
if ($error = user_validate_mail($form_values['ezmlm_add_address'])) {
|
| 192 |
form_set_error('ezmlm_add_address', $error);
|
| 193 |
}
|
| 194 |
if (! strlen($form_values['ezmlm_add_name'])) {
|
| 195 |
form_set_error('ezmlm_add_name', t('You must fill in the Name field'));
|
| 196 |
}
|
| 197 |
}
|
| 198 |
}
|
| 199 |
|
| 200 |
/**
|
| 201 |
* Submit form
|
| 202 |
*/
|
| 203 |
function ezmlm_settings_submit($form_id, $form_values) {
|
| 204 |
// various
|
| 205 |
if ($form_values['ezmlm_title'] != "") {
|
| 206 |
variable_set('ezmlm_title', $form_values['ezmlm_title']);
|
| 207 |
}
|
| 208 |
if ($form_values['ezmlm_block_title'] != "") {
|
| 209 |
variable_set('ezmlm_block_title', $form_values['ezmlm_block_title']);
|
| 210 |
}
|
| 211 |
// look for deletes
|
| 212 |
$lists = _ezmlm_get_lists();
|
| 213 |
if (is_array($lists)) {
|
| 214 |
// deletions
|
| 215 |
$newlists = array();
|
| 216 |
$delct = 0;
|
| 217 |
foreach ($lists as $list_text => $list_email) {
|
| 218 |
if (isset($form_values[$list_text]) && $form_values[$list_text] == 1) {
|
| 219 |
$delct++;
|
| 220 |
continue; // skip copying this list to the new list
|
| 221 |
}
|
| 222 |
else {
|
| 223 |
$newlists[$list_text] = $list_email; // copy old list entry to new
|
| 224 |
}
|
| 225 |
}
|
| 226 |
if (count($newlists) < count($lists)) {
|
| 227 |
variable_set('ezmlmmailinglists', $newlists);
|
| 228 |
drupal_set_message(t('Deletions done.'));
|
| 229 |
}
|
| 230 |
}
|
| 231 |
// add list
|
| 232 |
$lists = _ezmlm_get_lists();
|
| 233 |
$count = _ezmlm_get_count();
|
| 234 |
if (!is_array($lists) || $count < 1 ) {
|
| 235 |
$lists = array();
|
| 236 |
variable_del('ezmlmmailinglists');
|
| 237 |
}
|
| 238 |
$list_text = trim($form_values['ezmlm_add_name']);
|
| 239 |
$list_email = trim($form_values['ezmlm_add_address']);
|
| 240 |
if (strlen($list_text) && strlen($list_email)) {
|
| 241 |
$lists[$list_text] = $list_email;
|
| 242 |
variable_set('ezmlmmailinglists', $lists);
|
| 243 |
drupal_set_message( t('The mailing lists have been saved.'));
|
| 244 |
}
|
| 245 |
}
|
| 246 |
|
| 247 |
// internal functions
|
| 248 |
/**
|
| 249 |
* Generates the subscription form.
|
| 250 |
*
|
| 251 |
* @return
|
| 252 |
* HTML form.
|
| 253 |
*/
|
| 254 |
function _ezmlm_subscribe_form() {
|
| 255 |
global $user;
|
| 256 |
|
| 257 |
$listct = _ezmlm_get_count();
|
| 258 |
if ($listct == 0) {
|
| 259 |
return t('There are no lists available for subscription.'); // formatting?
|
| 260 |
}
|
| 261 |
$lists = _ezmlm_get_lists();
|
| 262 |
|
| 263 |
$form['subscribe'] = array(
|
| 264 |
'#type' => 'markup',
|
| 265 |
'#value' => variable_get('ezmlm_title', t('Mailing lists')),
|
| 266 |
);
|
| 267 |
$ct = 1;
|
| 268 |
foreach ($lists as $list_text => $list_email) {
|
| 269 |
$list_email = trim($list_email);
|
| 270 |
$form['ezmlm_subscribe']["ezmlm_list_$ct"] = array(
|
| 271 |
'#type' => 'checkbox',
|
| 272 |
'#title' => $list_email,
|
| 273 |
'#return_value' => $list_email,
|
| 274 |
);
|
| 275 |
$ct++;
|
| 276 |
}
|
| 277 |
$form['ezmlm_email'] = array (
|
| 278 |
'#type' => 'textfield',
|
| 279 |
'#title' => t('Your email address'),
|
| 280 |
'#default_value' => ($user->mail ? $user->mail : ''),
|
| 281 |
'#size' => 20,
|
| 282 |
'#maxlength' => 80,
|
| 283 |
);
|
| 284 |
$form['submit'] = array(
|
| 285 |
'#type' => 'submit',
|
| 286 |
'#value' => t('Subscribe'));
|
| 287 |
|
| 288 |
return $form;
|
| 289 |
}
|
| 290 |
|
| 291 |
function _ezmlm_subscribe_form_validate($form_id, $form_values) {
|
| 292 |
$arr = array();
|
| 293 |
$lists = _ezmlm_get_lists();
|
| 294 |
$ct = 1;
|
| 295 |
foreach ($form_values as $key=>$address) {
|
| 296 |
$address = trim($address);
|
| 297 |
if( $key == "ezmlm_list_$ct" && $address && in_array($address, $lists)) {
|
| 298 |
$arr[] = $address;
|
| 299 |
}
|
| 300 |
$ct++;
|
| 301 |
}
|
| 302 |
if ( count($arr) < 1 ) {
|
| 303 |
form_set_error('', t('No lists selected!'));
|
| 304 |
}
|
| 305 |
if ( $error = user_validate_mail($form_values['ezmlm_email'])) {
|
| 306 |
form_set_error('ezmlm_email', $error);
|
| 307 |
}
|
| 308 |
}
|
| 309 |
|
| 310 |
function _ezmlm_subscribe_form_submit($form_id, $form_values) {
|
| 311 |
$lists = _ezmlm_get_lists();
|
| 312 |
$arr = array();
|
| 313 |
$ct = 1;
|
| 314 |
// get the selected mailing list addresses
|
| 315 |
foreach ($form_values as $key=>$address) {
|
| 316 |
$address = trim($address);
|
| 317 |
if( $key == "ezmlm_list_$ct" && $address && in_array($address, $lists)) {
|
| 318 |
$arr[] = $address;
|
| 319 |
}
|
| 320 |
$ct++;
|
| 321 |
}
|
| 322 |
$email = trim($form_values['ezmlm_email']);
|
| 323 |
$ret = _ezmlm_subscribe_process($arr, $email);
|
| 324 |
if ( is_array($ret)) {
|
| 325 |
$output = (t('Please check your mail at %mail to confirm registration of:', array('%mail' => $email)));
|
| 326 |
drupal_set_message($output . "<br>" . implode('<br>', $ret));
|
| 327 |
}
|
| 328 |
else {
|
| 329 |
drupal_set_message($ret, 'error');
|
| 330 |
}
|
| 331 |
}
|
| 332 |
|
| 333 |
/**
|
| 334 |
* Process the subscription form input; does some address checks and
|
| 335 |
* sends mail to the EZMLM subscribe addresses for the lists.
|
| 336 |
*/
|
| 337 |
function _ezmlm_subscribe_process($subs, $mail) {
|
| 338 |
$err = "";
|
| 339 |
list($user_name, $user_domain) = split('@', $mail);
|
| 340 |
$lists = _ezmlm_get_lists();
|
| 341 |
$mylists = array();
|
| 342 |
// loop over the lists
|
| 343 |
foreach ($lists as $list_text => $list_email) {
|
| 344 |
// loop over the subs
|
| 345 |
foreach ($subs as $address) {
|
| 346 |
if ($list_email == $address) {
|
| 347 |
list($list_name, $list_domain) = split('@', $list_email);
|
| 348 |
// subscribe address for list formatted below
|
| 349 |
$to = trim($list_name) .'-subscribe-'. trim($user_name) .'='. trim($user_domain) .'@'. trim($list_domain);
|
| 350 |
mail($to, '', '');
|
| 351 |
$mylists[] = trim($list_email);
|
| 352 |
}
|
| 353 |
}
|
| 354 |
}
|
| 355 |
return($mylists);
|
| 356 |
}
|
| 357 |
|
| 358 |
/**
|
| 359 |
* Return current number of lists.
|
| 360 |
* Use this instead of count(_ezmlm_get_lists()) because non-array counts as 1.
|
| 361 |
*/
|
| 362 |
function _ezmlm_get_count() {
|
| 363 |
$lists = _ezmlm_get_lists();
|
| 364 |
if (!is_array($lists)) {
|
| 365 |
return 0;
|
| 366 |
}
|
| 367 |
else {
|
| 368 |
return count($lists);
|
| 369 |
}
|
| 370 |
}
|
| 371 |
|
| 372 |
/**
|
| 373 |
* Return array of current mailing lists.
|
| 374 |
*/
|
| 375 |
function _ezmlm_get_lists() {
|
| 376 |
return variable_get('ezmlmmailinglists', '');
|
| 377 |
}
|