/[drupal]/contributions/modules/phplist/phplist.module
ViewVC logotype

Diff of /contributions/modules/phplist/phplist.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.19.2.1 by paulbeaney, Fri Aug 14 07:50:25 2009 UTC revision 1.19.2.2 by pwolanin, Wed Nov 18 16:48:01 2009 UTC
# Line 57  function phplist_menu($may_cache) { Line 57  function phplist_menu($may_cache) {
57    if ($mlm && $may_cache) {    if ($mlm && $may_cache) {
58      $items[] = array(      $items[] = array(
59        'path' => 'newsletters',        'path' => 'newsletters',
60        'title' => t('Mailing lists'),        'title' => t('Newsletters'),
61        'description' => t('Mailing lists'),        'description' => t('Newsletters'),
62        'callback' => 'drupal_get_form',        'callback' => 'drupal_get_form',
63        'callback arguments' => array('_phplist_redirect'),        'callback arguments' => array('_phplist_redirect'),
64        'access' => TRUE        'access' => TRUE,
65        );      );
66        $items[] = array(
67          'path' => 'newsletters/unsubscribe',
68          'title' => t('Unsubscribe from Newsletters'),
69          'description' => t('Unsubscribe from Newsletters'),
70          'callback' => 'drupal_get_form',
71          'callback arguments' => array('phplist_unsubscribe_form'),
72          'access' => TRUE,
73          'type' => MENU_DYNAMIC_ITEM,
74        );
75        $items[] = array(
76          'path' => 'newsletters/confirm',
77          'title' => t('Confirm subscription'),
78          'description' => t('Confirm subscription'),
79          'callback' => 'drupal_get_form',
80          'callback arguments' => array('phplist_confirm_subscription'),
81          'access' => TRUE,
82          'type' => MENU_DYNAMIC_ITEM,
83        );
84    }    }
85    $admin_access = user_access('administer phpList');    $admin_access = user_access('administer phpList');
86    $items[] = array(    $items[] = array(
# Line 72  function phplist_menu($may_cache) { Line 90  function phplist_menu($may_cache) {
90      'callback' => 'drupal_get_form',      'callback' => 'drupal_get_form',
91      'callback arguments' => array('phplist_admin_settings'),      'callback arguments' => array('phplist_admin_settings'),
92      'access' => $admin_access      'access' => $admin_access
93      );    );
94    
95    return $items;    return $items;
96  }  }
97    
# Line 131  function phplist_admin_settings() { Line 150  function phplist_admin_settings() {
150      );      );
151    }    }
152    else {    else {
     // Test database connection  
     $strprefix = _phplist_dbconn();  
     if ($strprefix === FALSE) {  
       $message = t('no connection');  
     }  
     else {  
       db_set_active('phplist');  
       if (db_table_exists($strprefix .'user_user')) {  
         db_set_active('default');  
         $message = t('connection OK');  
       }  
       else {  
         db_set_active('default');  
         $message = t('no connection');  
       }  
     }  
   
153      $form['general']['phplist_dbpassstatus'] = array(      $form['general']['phplist_dbpassstatus'] = array(
154        '#type' => 'markup',        '#type' => 'markup',
155        '#prefix' => '<span style="padding:4px;border:solid 1px #000">',        '#prefix' => '<span style="padding:4px;border:solid 1px #000">',
156        '#value' => t('Password is set') ." - ". $message,        '#value' => t('Password is set'), // ." - ". $message,
157        '#suffix' => '</span>'        '#suffix' => '</span>'
158      );      );
159    }    }
# Line 161  function phplist_admin_settings() { Line 163  function phplist_admin_settings() {
163      '#default_value' => variable_get('phplist_prefix', 'phplist_'),      '#default_value' => variable_get('phplist_prefix', 'phplist_'),
164      '#description' => t('Prefix for the database tables')      '#description' => t('Prefix for the database tables')
165    );    );
   
166    $form['block'] = array(    $form['block'] = array(
167      '#type' => 'fieldset',      '#type' => 'fieldset',
168      '#title' => t('Block settings'),      '#title' => t('Block settings'),
# Line 174  function phplist_admin_settings() { Line 175  function phplist_admin_settings() {
175      '#description' => t('The absolute path or URL of your PHPList front page, for example: <em>/lists/</em> or <em>http://lists.mydomain.com/</em><br />Used for joining the mailing list via the block.'),      '#description' => t('The absolute path or URL of your PHPList front page, for example: <em>/lists/</em> or <em>http://lists.mydomain.com/</em><br />Used for joining the mailing list via the block.'),
176      '#default_value' => variable_get('phplist_subscribe_url', '/lists'),      '#default_value' => variable_get('phplist_subscribe_url', '/lists'),
177    );    );
   $form['block']['phplist_email_confirm'] = array(  
     '#type' => 'checkbox',  
     '#title' => t('Show email confirmation field in subscribe block'),  
     '#default_value' => variable_get('phplist_email_confirm', 0),  
   );  
   $form['block']['phplist_email_width'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Size of email box'),  
     '#default_value' => variable_get('phplist_email_width', 16),  
     '#size' => 5,  
   );  
178    
179    if (module_exists('profile')) {    if (module_exists('profile')) {
180      $form['general']['mapping'] = array(      $form['general']['mapping'] = array(
181        '#title' => 'Attribute mapping',        '#title' => 'Attribute mapping',
182        '#type' => 'fieldset',        '#type' => 'fieldset',
183        '#description' => t('Use these settings to transfer first and last names from Drupal profiles to phpList.  If the phpList attributes do not already exist, they will be created.  Use the SYNCHRONISE NOW link to refresh all existing accounts.')        '#description' => t('Use these settings to transfer first and last names from Drupal profiles to phpList.  If the phpList attributes do not already exist, they will be created.  Use the SYNCHRONISE NOW link to refresh all existing accounts (note that this will un-blacklist all accounts).  Make all these boxes blank if you do not have any appropriate profile fields configured.')
184      );      );
185    
186      $form['general']['mapping']['phplist_profilefirstname'] = array(      $form['general']['mapping']['phplist_profilefirstname'] = array(
# Line 279  function _phplist_get_prefix() { Line 269  function _phplist_get_prefix() {
269    static $strprefix;    static $strprefix;
270    
271    if ($strprefix === NULL) {    if ($strprefix === NULL) {
272      $strprefix = variable_get('phplist_prefix', 'phplist_');      $strprefix = db_escape_table(variable_get('phplist_prefix', 'phplist_'));
273    }    }
274    
275    return $strprefix;    return $strprefix;
# Line 314  function phplist_user($op, &$edit, &$use Line 304  function phplist_user($op, &$edit, &$use
304          $autosubscribe_on_reg = variable_get('phplist_autosubscribe_on_register', 0);          $autosubscribe_on_reg = variable_get('phplist_autosubscribe_on_register', 0);
305    
306          if ($subscribe_on_reg || $autosubscribe_on_reg) {          if ($subscribe_on_reg || $autosubscribe_on_reg) {
307            $lists = _phplist_get_public_lists($user);            if ($autosubscribe_on_reg && !$subscribe_on_reg) {
308            if ($autosubscribe_on_reg) {              $lists = _phplist_get_public_lists($user);
309              $lists = array_slice($lists, 0, 1);              $lists = array_slice($lists, 0, 1);
310            }            }
311              else {
312                $lists = array();
313                if (is_array($user->phplist_subscribe_lists)) {
314                  foreach ($user->phplist_subscribe_lists as $l) {
315                    $lists[] = $l;
316                    $lists[count($lists)]->lid = $l;
317                  }
318                }
319              }
320            foreach ($lists as $l) {            foreach ($lists as $l) {
321              if (isset($user->phplist_subscribe_lists[$l->lid]) && !isset($l->userid)) {              if ((isset($user->phplist_subscribe_lists[$l->lid]) || $autosubscribe_on_reg) && !isset($l->userid)) {
322                _phplist_manage_subscription($user->mail, $l->lid);                _phplist_manage_subscription($user->mail, $l->lid, FALSE);
323                drupal_set_message(t('You have been subscribed to %name at %mail', array('%name' => $l->name, '%mail' => $user->mail)));                drupal_set_message(t('You have been subscribed to %name at %mail', array('%name' => $l->name, '%mail' => $user->mail)));
324              }              }
325              elseif (isset($l->userid)) {              elseif (isset($l->userid)) {
326                drupal_set_message(t('You are already subscribed to %name', array('%name' => $l->name)));                drupal_set_message(t('You are already subscribed to %name', array('%name' => $l->name)));
327              }              }
328                else {
329                  drupal_set_message(t('Unable to subscribe you to %name', array('%name' => $l->name)));
330                }
331            }            }
332            unset($user->phplist_subscribe_lists);            unset($user->phplist_subscribe_lists);
333            user_save($user);            user_save($user);
334          }          }
335        }        }
336    
337        break;        break;
338    
339      case 'update':      case 'update':
# Line 348  function phplist_user($op, &$edit, &$use Line 351  function phplist_user($op, &$edit, &$use
351        $phplistid = _phplist_lookup_phplistid($user->mail);        $phplistid = _phplist_lookup_phplistid($user->mail);
352    
353        if (true) {        if (true) {
354                  // Just remove the Drupal flag from the phpList account                  // Just remove the Drupal flag from the phpList account - leave the email in PHPlist
355          _phplist_update_attribute('Drupal', 'checkbox', 'off', $phplistid);          _phplist_update_attribute('Drupal', 'checkbox', 'off', $phplistid);
356        }        }
357        else {        else {
# Line 372  function phplist_user($op, &$edit, &$use Line 375  function phplist_user($op, &$edit, &$use
375        $lid = arg(5);        $lid = arg(5);
376    
377        if ($action != '' && is_numeric($lid)) {        if ($action != '' && is_numeric($lid)) {
378          _phplist_manage_subscription($user->mail, $lid, $action);          _phplist_manage_subscription($user->mail, $lid, $action, TRUE);
379          drupal_set_message(t("Your subscriptions have been updated"));          drupal_set_message(t("Your subscriptions have been updated"));
         drupal_goto('user/'. $user->uid .'/edit/phplist');  
380        }        }
381    
382        if ($category == 'phplist' && user_access('access lists')) {        if ($category == 'phplist' && user_access('access lists')) {
383          // Lookup the list of mails lists          $form['holder'] = array(
384          $form = phplist_lists($user);            '#type' => 'fieldset',
385              '#title' => t('My newsletters'),
                 // Show the pre-amble text  
         $form['preamble'] = array(  
           '#value' => variable_get('phplist_preamble', ''),  
           '#weight' => -10  
386          );          );
387    
388            // Lookup the list of mails lists
389            $form['holder']['lists'] = phplist_lists($user);
390    
391          $form["phplist_html"] = array(                  // Show the pre-amble text
392            '#type' => 'checkbox',          if (variable_get('phplist_preamble', '')) {
393            '#title' => t('Receive emails in text format'),            $form['holder']['preamble'] = array(
394            '#options' => array(0, 1),              '#value' => variable_get('phplist_preamble', ''),
395            '#default_value' => $user->phplist_html,              '#weight' => -10
396            '#weight' => 10            );
397          );          }
398    
399          return $form;          return $form;
400        }        }
# Line 417  function phplist_user($op, &$edit, &$use Line 418  function phplist_user($op, &$edit, &$use
418   * @ingroup phplist   * @ingroup phplist
419   * @return none   * @return none
420   */   */
421  function _phplist_manage_subscription($email, $lid, $action='subscribe') {  function _phplist_manage_subscription($email, $lid, $action='subscribe', $redirect=TRUE) {
422    $strprefix = _phplist_dbconn();    $strprefix = _phplist_dbconn();
423    if ($strprefix === FALSE) {    if ($strprefix === FALSE) {
424      return;      return;
425    }    }
426    
427    $lid    = intval($lid);    $lid    = intval($lid);
428    $userid = intval($userid);    $account = user_load(array('mail' => $email));
429    
430      // Check token - CSRF protection
431      $path = preg_replace('/\/'. $value .'$/', '', $_GET['q']);
432      if ($redirect && (!isset($_GET['token']) || !phplist_check_token($_GET['token'], arg(5)))) {
433        drupal_set_message(t('Got an invalid token. Subscription not updated.'), 'error');
434        drupal_goto('user/'. $account->uid .'/edit/phplist');
435      }
436      else {
437        if ($userid > 0 && user_access("manage subscriptions")) {
438          // This is a manager updating another user's account
439          $user = user_load(array('uid' => $userid));
440          $email = $user->mail;
441        }
442        else {
443          $user = user_load(array('mail' => $email));
444        }
445    
446        if ($email == '') {
447          form_set_error("", t('Email address is empty for UID '. $userid));
448          return;
449        }
450      }
451    
452    if ($lid == 0 || $lid == '') return;    if ($lid == 0 || $lid == '') return;
453    
# Line 434  function _phplist_manage_subscription($e Line 457  function _phplist_manage_subscription($e
457    // Thanks to Dave Cohen, http://drupal.org/node/249559    // Thanks to Dave Cohen, http://drupal.org/node/249559
458    if (!$phplistid) {    if (!$phplistid) {
459      $account = user_load(array('mail' => $email));      $account = user_load(array('mail' => $email));
460      if ($account)      if ($account) {
461        _phplist_sync_user($phplistid, $account);        _phplist_sync_user($phplistid, $account);
462        }
463        else {
464          // Non-Drupal user - allow direct anonymous sign-up
465          $account->mail = $email;
466          $account->roles = array('');
467          _phplist_sync_user($phplistid, $account);
468        }
469      $phplistid = _phplist_lookup_phplistid($email);      $phplistid = _phplist_lookup_phplistid($email);
470    }    }
471    
# Line 444  function _phplist_manage_subscription($e Line 474  function _phplist_manage_subscription($e
474    
475      switch ($action) {      switch ($action) {
476        case 'subscribe';        case 'subscribe';
477          db_query("INSERT INTO $strprefix"."listuser(userid, listid, entered) VALUES(%d, %d, NOW())", $phplistid, $lid);          db_query("REPLACE INTO $strprefix"."listuser(userid, listid, entered) VALUES(%d, %d, NOW())", $phplistid, $lid);
478          break;          break;
479        case 'unsubscribe':        case 'unsubscribe':
480          db_query("DELETE FROM $strprefix"."listuser WHERE userid=%d AND listid=%d", $phplistid, $lid);          db_query("DELETE FROM $strprefix"."listuser WHERE userid=%d AND listid=%d", $phplistid, $lid);
# Line 456  function _phplist_manage_subscription($e Line 486  function _phplist_manage_subscription($e
486    else {    else {
487      drupal_set_message(t('Failed to update email newsletter subscription.'));      drupal_set_message(t('Failed to update email newsletter subscription.'));
488    }    }
489      if ($redirect) drupal_goto('user/'. $user->uid .'/edit/phplist');
490  }  }
491    
492    
# Line 488  function phplist_lists($user, $op='list' Line 519  function phplist_lists($user, $op='list'
519    foreach ($lists as $list) {    foreach ($lists as $list) {
520      if ($list->userid == '' && !$subonly) {      if ($list->userid == '' && !$subonly) {
521        $rows[] = array('name' => "<b>". $list->name ."</b>". ($booshowdescription ? " <br />". $list->description : ""),        $rows[] = array('name' => "<b>". $list->name ."</b>". ($booshowdescription ? " <br />". $list->description : ""),
522          'subscribe' => l(t('Subscribe'), "user/". $user->uid ."/edit/phplist/subscribe/". $list->lid)          'subscribe' => l(t('Subscribe'), "user/". $user->uid ."/edit/phplist/subscribe/". $list->lid, NULL, 'token='. phplist_get_token($list->lid))
523        );        );
524      }      }
525      elseif ($list->userid != '') {      elseif ($list->userid != '') {
526        $rows[] = array('name' => "<b>". $list->name ."</b>". ($booshowdescription ? " <br />". $list->description : ""),        $rows[] = array('name' => "<b>". $list->name ."</b>". ($booshowdescription ? " <br />". $list->description : ""),
527          'unsubscribe' => l(t('Unsubscribe'), "user/". $user->uid ."/edit/phplist/unsubscribe/". $list->lid)          'unsubscribe' => l(t('Unsubscribe'), "user/". $user->uid ."/edit/phplist/unsubscribe/". $list->lid, NULL, 'token='. phplist_get_token($list->lid))
528        );        );
529      }      }
530    }    }
# Line 516  function phplist_lists($user, $op='list' Line 547  function phplist_lists($user, $op='list'
547      '#type' => 'markup',      '#type' => 'markup',
548      '#value' => $content      '#value' => $content
549      );      );
550      $form['#redirect'] = 'user/'. $user->uid .'/edit/phplist';
551    
552    return $form;    return $form;
553  }  }
# Line 537  function _phplist_get_public_lists($user Line 569  function _phplist_get_public_lists($user
569    $lists = array();    $lists = array();
570    
571    db_set_active('phplist');    db_set_active('phplist');
572    
573    $result = db_query("SELECT DISTINCT l.id as lid, l.name as name, l.description as description, (SELECT userid FROM $strprefix"."listuser t2 WHERE t2.listid=l.id AND userid=%d) as 'userid'    $result = db_query("SELECT DISTINCT l.id as lid, l.name as name, l.description as description, (SELECT userid FROM $strprefix"."listuser t2 WHERE t2.listid=l.id AND userid=%d) as 'userid'
574      FROM $strprefix"."list l LEFT OUTER JOIN $strprefix"."listuser t2 ON l.id = t2.listid      FROM $strprefix"."list l LEFT OUTER JOIN $strprefix"."listuser t2 ON l.id = t2.listid
575      WHERE active=1 ORDER BY listorder", $phplistid);      WHERE active=1 ORDER BY listorder", $phplistid);
# Line 616  function _phplist_sync_user($phplistid, Line 648  function _phplist_sync_user($phplistid,
648    
649    if ($phplistid != '') {    if ($phplistid != '') {
650      db_set_active('phplist');      db_set_active('phplist');
651      $result = db_query("UPDATE ". $strprefix ."user_user SET email='%s', modified=NOW(), htmlemail=%d, confirmed=1 WHERE id=%d", $user->mail, ($user->phplist_html == 0 || $user->phplist_html == '' ? 1 : 0), $phplistid);      $result = db_query("UPDATE ". $strprefix ."user_user SET email='%s', modified=NOW(), htmlemail=%d, confirmed=1, blacklisted=0 WHERE id=%d", $user->mail, ($user->phplist_html == 0 || $user->phplist_html == '' ? 1 : 0), $phplistid);
652    }    }
653    else {    else {
654      $uniqueid = _phplist_uniqueid($user->mail);      $uniqueid = _phplist_uniqueid($user->mail);
655      db_set_active('phplist');      db_set_active('phplist');
656      db_query("INSERT INTO ". $strprefix ."user_user (email, entered, htmlemail, confirmed, uniqid) VALUES('%s', NOW(), %d, 1, '%s')", $user->mail, ($user->phplist_html == 0 || $user->phplist_html == '' ? 1 : 0), $uniqueid);      db_query("INSERT INTO ". $strprefix ."user_user (email, entered, htmlemail, confirmed, blacklisted, uniqid) VALUES('%s', NOW(), %d, 1, 0, '%s')", $user->mail, ($user->phplist_html == 0 || $user->phplist_html == '' ? 1 : 0), $uniqueid);
657      $phplistid = db_result(db_query("SELECT last_insert_id()"));      $phplistid = db_result(db_query("SELECT last_insert_id()"));
658    }    }
659    
# Line 632  function _phplist_sync_user($phplistid, Line 664  function _phplist_sync_user($phplistid,
664    }    }
665    
666    // Update/add phpList attrbiutes for this user's roles    // Update/add phpList attrbiutes for this user's roles
667    $aryroles = $user->roles;    if (count($user->roles) > 0) {
668    foreach ($aryroles as $role) {      $aryroles = $user->roles;
669      if ($booroles && $role != 'authenticated user') {      foreach ($aryroles as $role) {
670        _phplist_update_attribute('Drupal role - '. $role, 'checkbox', 'on', $phplistid);        if ($booroles && $role != 'authenticated user') {
671      }          _phplist_update_attribute('Drupal role - '. $role, 'checkbox', 'on', $phplistid);
672      elseif ($role == 'authenticated user') {        }
673            // Always add this role to tag the phpList user as originating from Drupal        elseif ($role == 'authenticated user') {
674        _phplist_update_attribute('Drupal', 'checkbox', 'on', $phplistid, $strprefix);            // Always add this role to tag the phpList user as originating from Drupal
675        if (!$booroles) break;          _phplist_update_attribute('Drupal', 'checkbox', 'on', $phplistid, $strprefix);
676            if (!$booroles) break;
677          }
678      }      }
679    }    }
680    
# Line 648  function _phplist_sync_user($phplistid, Line 682  function _phplist_sync_user($phplistid,
682    if (module_exists('profile')) {    if (module_exists('profile')) {
683          // Store other attributes/profile settings          // Store other attributes/profile settings
684      foreach ($attrmap as $attr) {      foreach ($attrmap as $attr) {
685        _phplist_update_attribute($attr[1], 'textline', $user->{$attr[0]}, $phplistid);        if (isset($user->{$attr[0]})) _phplist_update_attribute($attr[1], 'textline', $user->{$attr[0]}, $phplistid);
686      }      }
687    }    }
688    
# Line 698  function _phplist_default_list() { Line 732  function _phplist_default_list() {
732  }  }
733    
734  /**  /**
735   * Check database DSN is available - returm empty string if not   * Check database DSN is available - returm FALSE if not
736     * (Using FALSE allows use of empty prefix)
737   *   *
738   * @ingroup phplist   * @ingroup phplist
739   * @return table prefix   * @return table prefix
# Line 758  function _phplist_redirect() { Line 793  function _phplist_redirect() {
793      drupal_goto('user/'. $user->uid .'/edit/phplist');      drupal_goto('user/'. $user->uid .'/edit/phplist');
794    }    }
795    else {    else {
796      drupal_goto('user/login');      drupal_goto('user/login', 'destination=newsletters');
797    }    }
798  }  }
799    
# Line 818  function phplist_block($op = 'list', $de Line 853  function phplist_block($op = 'list', $de
853        $block_body['format'] = FILTER_FORMAT_DEFAULT;        $block_body['format'] = FILTER_FORMAT_DEFAULT;
854      }      }
855      $form['body_filter']['phplist_subscribe_block_header_format'] = filter_form($block_body['format'], -16);      $form['body_filter']['phplist_subscribe_block_header_format'] = filter_form($block_body['format'], -16);
856    
857        $form['block']['phplist_email_confirm'] = array(
858          '#type' => 'checkbox',
859          '#title' => t('Show email confirmation field'),
860          '#default_value' => variable_get('phplist_email_confirm', 0),
861        );
862        $form['block']['phplist_format_block'] = array(
863          '#type' => 'checkbox',
864          '#title' => t('Show email format box'),
865          '#default_value' => variable_get('phplist_format_block', 1),
866        );
867        $form['block']['phplist_format_default'] = array(
868          '#type' => 'select',
869          '#title' => t('If not showing the email format box, default to this format'),
870          '#default_value' => variable_get('phplist_format_default', 0),  // Default to HTML format
871          '#options' => array(0 => 'html', 1 => 'text'),
872        );
873    
874        $form['block']['phplist_email_width'] = array(
875          '#type' => 'textfield',
876          '#title' => t('Size of email box(es)'),
877          '#default_value' => variable_get('phplist_email_width', 16),
878          '#size' => 5,
879        );
880    
881      return $form;      return $form;
882    }    }
883    
# Line 826  function phplist_block($op = 'list', $de Line 886  function phplist_block($op = 'list', $de
886        $edit['phplist_subscribe_block_header_format'] = FILTER_FORMAT_DEFAULT;        $edit['phplist_subscribe_block_header_format'] = FILTER_FORMAT_DEFAULT;
887      }      }
888      variable_set('phplist_subscribe_block_header', array('content' => $edit['phplist_subscribe_block_header'], 'format' => $edit['phplist_subscribe_block_header_format']));      variable_set('phplist_subscribe_block_header', array('content' => $edit['phplist_subscribe_block_header'], 'format' => $edit['phplist_subscribe_block_header_format']));
889        variable_set('phplist_email_confirm', $edit['phplist_email_confirm']);
890        variable_set('phplist_email_width', $edit['phplist_email_width']);
891        variable_set('phplist_format_block', $edit['phplist_format_block']);
892        variable_set('phplist_format_default', $edit['phplist_format_default']);
893      return TRUE;      return TRUE;
894    }    }
895    
896    elseif ($op == 'view' && $delta == 0) {    elseif ($op == 'view' && $delta == 0) {
897      $block = array(      $block = array(
898        'subject' => t('Subscribe to our mailing list'),        'subject' => t('Subscribe to our mailing list'),
899        'content' => drupal_get_form('phplist_subscribe_form'),        'content' => theme('subscribe_block'),
900      );      );
901      return $block;      return $block;
902    }    }
903  }  }
904    
905    function theme_subscribe_block() {
906      $block = '';
907      $block = drupal_get_form('phplist_subscribe_form');
908      return $block;
909    }
910    
911  /*  /*
912   * Implementation of hook_form_alter().   * Implementation of hook_form_alter().
913   *   *
# Line 878  function phplist_admin_settings_submit_p Line 948  function phplist_admin_settings_submit_p
948    }    }
949  }  }
950    
951    function phplist_unsubscribe_form() {
952      // Use the PHPlist uid if available - for anonymous users
953      // Drupal users should be sent to their account page
954      global $user;
955      if ($user->uid > 0) drupal_goto('newsletters');
956    
957      $email = '';
958    
959      if (isset($_GET["uid"])) {
960        $strprefix = _phplist_dbconn();
961        if ($strprefix !== FALSE) {
962          // Lookup email from PHPlist
963          db_set_active("phplist");
964          $email = db_result(db_query("SELECT email FROM ". $strprefix ."user_user WHERE uniqid='%s'", check_plain($_GET["uid"])));
965          db_set_active("default");
966        }
967      }
968      if ($email) {
969        // Check whether this is a Drupal user
970        $user = user_load(array('mail' => $email));
971        if ($user) {
972          // They have a Drupal account - get them to login
973          drupal_set_message(t("Please login to manage your newsletter subscriptions"));
974          drupal_goto('newsletters');
975        }
976      }
977    
978    
979      // If we are still here, this is a non-Druapl person unsubscribing
980      $form['mail'] = array(
981        '#title' => t('Email address'),
982            '#type' => 'textfield',
983        '#required' => TRUE,
984        '#default_value' => $email,
985      );
986      $form['reason'] = array(
987        '#type' => 'textarea',
988        '#title' => t('Reason for unsubscribing'),
989        '#rows' => 4,
990        '#cols' => 40,
991        '#resizable' => FALSE,
992        '#description' => t('We are sorry you are no longer interested in receiving our newsletters.  We would be grateful if you could tell us why.'),
993      );
994      $form['unsubscribe'] = array(
995        '#type' => 'submit',
996        '#value' => t('Unsubscribe'),
997      );
998      return $form;
999    }
1000    
1001    function phplist_unsubscribe_form_validate($form_id, $form_values) {
1002      if (! valid_email_address($form_values['mail'])) {
1003        form_set_error('mail', t('Please enter a valid email address.'));
1004      }
1005    
1006      // Check this email address doesn't belong to a Drupal user
1007      if (user_load(array('mail' => $form_values['mail']))) {
1008        drupal_set_message(t('This email address belongs to a registered account.  Please login to manage your subscriptions.'));
1009        drupal_goto('newsletters');
1010      }
1011    }
1012    
1013    function phplist_unsubscribe_form_submit($form_id, $form_values) {
1014      $data = array(
1015        'unsubscribeemail' => $form_values['mail'],
1016        'unsubscribereason' => $form_values['reason'],
1017        'unsubscribe' => 'Continue',
1018      );
1019    
1020      $postdata = '';
1021      foreach ($data as $key => $val) {
1022        $postdata .= ($postdata ? '&' : '') . urlencode($key) .'='. urlencode($val);
1023      }
1024    
1025      $subscribe_url = url(variable_get('phplist_subscribe_url', 'lists'), 'p=unsubscribe', NULL, TRUE);
1026      $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
1027      $result = drupal_http_request($subscribe_url, $headers, 'POST', $postdata);
1028    
1029      if ($result->code != 200 || strpos($result->data, 'You have been unsubscribed') === FALSE) {
1030        drupal_set_message(t('Oops, you have not been unsubscribed; please contact a site administrator.'));
1031      }
1032      else {
1033        drupal_set_message(t('You have been unsubscribed from all newsletters and you will shortly receive email confirmation.'));
1034      }
1035    }
1036    
1037  /*  /*
1038   * Create the 'subscribe' form   * Create the 'subscribe' form
1039   */   */
# Line 893  function phplist_subscribe_form() { Line 1049  function phplist_subscribe_form() {
1049    if ($user->uid) {    if ($user->uid) {
1050      // Logged in, so do nothing      // Logged in, so do nothing
1051      // TODO Add list of mailing lists to which the logged in person is subscribed      // TODO Add list of mailing lists to which the logged in person is subscribed
1052      $form['mail'] = array(  /*    $form['mail'] = array(
1053        '#type' => 'item',        '#type' => 'item',
1054        '#title' => t('E-mail'),        '#title' => t('E-mail'),
1055        '#size' => variable_get('phplist_email_width', 16),        '#size' => variable_get('phplist_email_width', 16),
1056        '#value' => $user->mail,        '#value' => $user->mail,
1057      );      );
1058    */
1059      $form['link'] = array(      $form['link'] = array(
1060        '#type' => 'markup',        '#type' => 'markup',
1061        '#prefix' => '<div>',        '#prefix' => '<div>',
1062        '#suffix' => '</div>',        '#suffix' => '</div>',
1063        '#value' => l(t('My subscriptions'), 'user/'. $user->uid .'/edit/phplist'),        '#value' => l(t('Manage my subscriptions'), 'user/'. $user->uid .'/edit/phplist'),
1064      );      );
1065    }    }
1066    else {    else {
# Line 921  function phplist_subscribe_form() { Line 1078  function phplist_subscribe_form() {
1078      }      }
1079    
1080      $form['lists'] = phplist_subscribe_checkboxes();      $form['lists'] = phplist_subscribe_checkboxes();
1081    
1082        if (variable_get('phplist_format_block', 1)) {
1083          $form['phplist_html'] = array(
1084            '#type' => 'checkbox',
1085            '#title' => t('Receive emails in text format'),
1086            '#options' => array(0, 1),
1087            '#default_value' => 0,
1088          );
1089        }
1090        else {
1091          $form['phplist_html'] = array(
1092            '#type' => 'value',
1093            '#value' => variable_get('phplist_format_default', 0),
1094          );
1095        }
1096    
1097      $form['submit'] = array(      $form['submit'] = array(
1098        '#type' => 'submit',        '#type' => 'submit',
1099        '#value' => t('Subscribe'),        '#value' => t('Subscribe'),
1100      );      );
1101    }    }
1102    
1103    return $form;    return $form;
1104  }  }
1105    
# Line 958  function phplist_subscribe_checkboxes($h Line 1130  function phplist_subscribe_checkboxes($h
1130      );      );
1131    }    }
1132    
   $form_checkboxes['phplist_html'] = array(  
     '#type' => 'checkbox',  
     '#title' => t('Receive emails in text format'),  
     '#options' => array(0, 1),  
     '#default_value' => 0,  
   );  
   
1133    return $form_checkboxes;    return $form_checkboxes;
1134  }  }
1135    
# Line 982  function phplist_subscribe_form_validate Line 1147  function phplist_subscribe_form_validate
1147      form_set_error('mailconfirm', t('The email address you entered does not match.'));      form_set_error('mailconfirm', t('The email address you entered does not match.'));
1148    }    }
1149    
1150    if (_phplist_lookup_phplistid($form_values['mail']) > 0) {    // Check this email address doesn't belong to a Drupal user
1151      form_set_error('mailconfirm', t('This email address is already registered.'));    if ($form_values['mail'] && user_load(array('mail' => $form_values['mail']))) {
1152        form_set_error('mailconfirm', t('This email address belongs to a registered account.  Please login to manage your subscriptions.'));
1153    }    }
1154    
1155    if (count($form_values['phplist_subscribe_lists']) == 0) {    if (count($form_values['phplist_subscribe_lists']) == 0) {
# Line 1012  function phplist_subscribe_form_submit($ Line 1178  function phplist_subscribe_form_submit($
1178      'htmlemail' => ($form_values['phplist_html'] ? 0 : 1), // looks backwards, is not. see usage in _phplist_sync_user()      'htmlemail' => ($form_values['phplist_html'] ? 0 : 1), // looks backwards, is not. see usage in _phplist_sync_user()
1179      'subscribe' => 'Subscribe',      'subscribe' => 'Subscribe',
1180    );    );
1181    
1182    $lists = _phplist_get_public_lists($user);    $lists = _phplist_get_public_lists($user);
1183    
1184    foreach ($lists as $l) {    foreach ($lists as $l) {
# Line 1032  function phplist_subscribe_form_submit($ Line 1198  function phplist_subscribe_form_submit($
1198    
1199    $result = drupal_http_request($subscribe_url, $headers, 'POST', $postdata);    $result = drupal_http_request($subscribe_url, $headers, 'POST', $postdata);
1200    
1201    if ($result->code != 200 || strpos($result->data, 'Thank you for subscribing') === FALSE) {    if ($result->code != 200 || strpos($result->data, t('Thank you for subscribing')) === FALSE) {
1202      drupal_set_message(t('Oops, you have not been added to the mailing list; please contact a site administrator.'));      drupal_set_message(t('Oops, you have not been added to the mailing list; please contact a site administrator.'));
1203        return false;
1204    }    }
1205    else {    else {
1206      drupal_set_message(t('You will be e-mailed shortly with a request to confirm your membership. Please make sure to click the link in that message to confirm your subscription.'));      drupal_set_message(t('You will be e-mailed shortly with a request to confirm your membership. Please make sure to click the link in that message to confirm your subscription.'));
1207        return true;
1208      }
1209    }
1210    
1211    function phplist_confirm_subscription() {
1212      $uid = isset($_GET['uid']) ? $_GET['uid'] : '';
1213    
1214      // Pass through to PHPlist installation
1215      $confirm_url = url(variable_get('phplist_subscribe_url', 'lists'), 'p=confirm&uid='. $uid, NULL, TRUE);
1216    
1217      $result = drupal_http_request($confirm_url);
1218    print_r($result);
1219    exit;
1220      if ($result->code != 200) {
1221        // Something went wrong at the network level
1222        $form['info'] = array(
1223          '#type' => 'item',
1224          '#title' => t('Error with confirmation'),
1225          '#value' => t('Oops, you have not been confirmed; please contact a site administrator.'),
1226        );
1227    }    }
1228      elseif (strpos($result->data, t('request for confirmation was not recognised')) > 0) {
1229        // Invalid uid
1230        $form['info'] = array(
1231          '#type' => 'item',
1232          '#title' => t('Problem with confirmation'),
1233          '#value' => t('Sorry, your request for confirmation was not recognised.
1234                    Please make sure to use the full web address as mentioned in the email that you received.
1235                    Sometimes this web address wraps onto multiple lines.'),
1236        );
1237      }
1238      elseif (strpos($result->data, t('Thank you for confirming ')) > 0) {
1239        // All ok
1240        $form['info'] = array(
1241          '#type' => 'item',
1242          '#title' => t('Confirmation confirmed'),
1243          '#value' => t('Thank you. Your subscription is now confirmed and activated.'),
1244        );
1245      }
1246    
1247      return $form;
1248    }
1249    
1250    /**
1251     * Get a private token used to protect links from CSRF attacks.
1252     * "Borrowed" from 5 Star module :D
1253     */
1254    function phplist_get_token($value) {
1255      global $user;
1256    
1257      // Anonymous users don't get a session ID, which breaks page caching.
1258      $session_id = $user->uid ? session_id() : '';
1259      $private_key = drupal_get_private_key();
1260      return md5($session_id . $value . $private_key);
1261    }
1262    
1263    /**
1264     * Check to see if a token value matches the specified node.
1265     * "Borrowed" from 5 Star module :D
1266     */
1267    function phplist_check_token($token, $value) {
1268      return phplist_get_token($value) == $token;
1269  }  }

Legend:
Removed from v.1.19.2.1  
changed lines
  Added in v.1.19.2.2

  ViewVC Help
Powered by ViewVC 1.1.3