Index: phplist.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/massmailer/engines/phplist/phplist.module,v
retrieving revision 1.38
diff -U65535 -r1.38 phplist.module
--- phplist.module 4 Apr 2006 22:10:06 -0000 1.38
+++ phplist.module 13 Aug 2006 05:27:04 -0000
@@ -1,937 +1,952 @@
'. t('The PHPList module allows for advanced mass mailing. You can create an audience, capture an audience, and maintain an audience') .'
';
$output .= ''. t('PHPList allows for open tracking, click tracking, bounce handling, HTML mailing, subscription management, preference management.') .'
';
$output .= t('You can:
- enable the mass mailer module to use the PHPList engine
- configure bounce handling in config.php.
- set rules for hard and soft bounce handling.
- monitor open tracking and click tracking with [usertrack] tag.
- manage subscriptions by adding links to emails with [subscribe] and [unsubscribe].
- add personalization features with attributes.
');
$output .= ''. t('For more information please read the configuration and customization handbook PHPList page.', array('%phplist' => 'http://www.drupal.org/handbook/modules/phplist/')) .'
';
return $output;
case 'admin/modules#description':
return t('A mailing engine that implements features from phplist. Requires massmailer.module.');
case 'is_mail_engine':
return 'phplist';
}
}
/**
* Returns settings information for this module.
*
* @ingroup phplist_core
* @return the content for a settings page.
*/
function phplist_engine_settings() {
global $base_url;
$domain = explode('/', $base_url);
$website = explode('/', $base_url, 3);
$inits = array( 'website'=>$website[2],
'domain'=>$domain[2],
'report_address'=>variable_get('massmailer_report_email', variable_get("site_mail", "")),
'admin_address'=>variable_get('massmailer_report_email', variable_get("site_mail", "")),
'public_baseurl'=>$base_url.'?q=phplist');
foreach($inits as $key=>$value) {
db_query('REPLACE INTO {phplist_config} (item, value) VALUES (\'%s\', \'%s\')', $key, $value);
variable_set($key, $value);
}
//variable_set('phplist_command_line_user', $_SERVER["USER"]);
//variable_set('phplist_version', '2.8.11');
// $group = form_item('', t('If you are on a shared host, it will probably be appreciated if you don\'t send out loads of emails in one go. To do this, you can configure batch processing. Please note, the following two values can be overridden by your ISP by using a server wide configuration. So if you notice these values to be different in reality, that may be the case'));
// $group .= form_textfield(t('Batch email size'), 'phplist_batch_size', variable_get('phplist_batch_size', 0), 40, 40, t('Define the amount of emails you want to send per period. If 0, batch processing is disabled'));
// $group .= form_textfield(t('Batch email period'), 'phplist_batch_period', variable_get('phplist_batch_period', 3600), 40, 40, t('Define the length of one batch processing period, in seconds (3600 is an hour)'));
// $group .= form_checkbox(t('Cron queue processing'), 'phplist_cron_enable', 1, variable_get('phplist_cron_enable', 0), t('Checked: when cron is called the message queue will be processed.'));
// $output .= form_group(t('Batch Processing'), $group);
$test = TRUE;
$output = '';
- // test for bash
- $test_shell = shell_exec('which bash');
- if ($test_shell != "/bin/bash\n") { // this will also bail out if shell_exec failed completely.
+ // test for CURL
+ if (!function_exists('curl_init')) { // Doest we have CURL?
$test = FALSE;
- $output .= t('The bash shell was not found in the expected place on your system. This means it is either not available on your system or it is somewhere else. The diagnostic output is %output and may help you to find the bash executable. If you find it on your system, please edit the first line of the file %file to reflect the correct place. If bash is not installed, ask your system administrator to install it for you.', array('%output' => theme('placeholder', $test_shell), '%file' => theme('placeholder', 'modules/massmailer/engines/phplist/bin/phplist')));
+ $output .= t('The CURL PHP extension is not available on your server. You will need to enable this extension in php.ini or ask your hosting provider to do it for you.');
}
- // test for php as an apache module
- $test_sapi = php_sapi_name();
- $output .= t('You are running PHP as %sapi. Massmailer is known to work if this is apache, other configurations are unsupported.', array('%sapi' => theme('placeholder', $test_sapi)));
if ($test) {
$output .= form_item('', t('PHPlist powered by PHPlist'));
}
else {
}
return $output;
}
/**
* Returns default values for massmailer settings.
*
* @ingroup phplist_core
* @return the default value for massmailer settings.
*/
function phplist_engine_settings_description($type) {
switch ($type) {
case 'message_footer' :
return 'Use the following keys to include the various preference and subscription urls in the email footer:
[UNSUBSCRIBE] : The unsubscribe url for the user
[PREFERENCES] : The list preferences url for the user
';
break;
}
}
/**
* Provides some the phplist menu items.
* Generally, mailing engines should not have menu items,
* but in this case we have to redirect and clean the unsubscribe urls.
*
* @ingroup massmailer_core
*/
function phplist_menu($may_cache) {
$items = array();
if($may_cache) {
$items[] = array('path' => 'phplist', 'title' => t('phplist subscription link'), 'callback' => 'phplist_page', 'access' => TRUE, 'type' => MENU_CALLBACK);
}
return $items;
}
/**
* Provides some phplist menu rewriting for handling unsubscribe urls.
*
* @ingroup massmailer_core
*/
function phplist_page() {
drupal_goto('massmailer/'.$_GET['p'] .'/0/'. $_GET['uid']);
}
/**
* @defgroup phplist_lists list related functions for phplist.
*/
/**
* Retrieves a list.
*
* @ingroup phplist_lists
* @param $lid id value of the list
* @return list object
*/
function phplist_get_list($lid) {
$list = db_fetch_object(db_query('SELECT l.id AS lid, l.*, count(lu.userid) AS total FROM {phplist_list} l LEFT JOIN {phplist_listuser} lu ON l.id = lu.listid WHERE l.id=%d GROUP BY lid ORDER BY listorder', $lid));
return $list;
}
/**
* Retrieves all the lists for a message
*
* @ingroup phplist_lists
* @return array of list object
*/
function phplist_get_lists($mid = null) {
$lists = array();
if($mid) {
$result = db_query('SELECT l.id AS lid, l.name AS name, l.description AS description FROM {phplist_list} l LEFT JOIN {phplist_listmessage} j ON l.id=j.listid WHERE j.messageid=%d ORDER BY listorder', $mid);
}
else {
$result = db_query('SELECT l.id as lid, l.name as name, l.description as description, count(lu.userid) as total FROM {phplist_list} l LEFT JOIN {phplist_listuser} lu ON l.id = lu.listid GROUP BY lid ORDER BY listorder');
}
while($list = db_fetch_object($result)) {
$list->groups = array();
if (module_exist('civicrm')) {
$list->groups = phplist_get_cicivrm_groups($list->lid);
}
$lists[] = $list;
}
return $lists;
}
/**
* Retrieves all public lists
*
* @ingroup phplist_lists
* @return array of list objects
*/
function phplist_get_public_lists() {
$lists = array();
$result = db_query('SELECT l.id as lid, l.name as name, l.description as description FROM {phplist_list} l WHERE public=1 ORDER BY listorder');
while($list = db_fetch_object($result)) {
$lists[] = $list;
}
return $lists;
}
/**
* Deletes a list.
*
* @ingroup phplist_lists
* @param $lid id value of the list.
* @return boolean true if successful.
*/
function phplist_delete_list($lid) {
$success = true;
$success = db_query('DELETE FROM {phplist_listmessage} WHERE listid=%d', $lid);
$success = db_query('DELETE FROM {phplist_listuser} WHERE listid=%d', $lid);
$success = db_query('DELETE FROM {phplist_list} WHERE id=%d', $lid);
return $success;
}
/**
* Creates a list.
*
* @ingroup phplist_lists
* @param $edit The form data submitted from the create list form.
* @return value of the new list if successful. null if not successful.
*/
function phplist_create_list($edit) {
if ($edit['public']=='hidden') {
variable_set('mm_hidden_'.trim(substr($edit['name'], 0, 35)), TRUE);
$edit['public'] = 0;
}
else {
variable_set('mm_hidden_'.trim(substr($edit['name'], 0, 35)), FALSE);
}
return db_query('INSERT INTO {phplist_list} (name, description, listorder, active, public, owner) VALUES (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\')', $edit['name'], $edit['description'], $edit['listorder'], $edit['active'], $edit['public'], $edit['owner']);
}
/**
* Updates a list.
*
* @ingroup phplist_lists
* @param $edit The form data submitted from the create list form.
* @return boolean true if successful.
*/
function phplist_update_list($edit) {
if ($edit['public']=='hidden') {
variable_set('mm_hidden_'.trim(substr($edit['name'], 0, 35)), TRUE);
$edit['public'] = 0;
}
else {
variable_set('mm_hidden_'.trim(substr($edit['name'], 0, 35)), FALSE);
}
return db_query('UPDATE {phplist_list} SET name=\'%s\', description=\'%s\', listorder=\'%s\', active=\'%s\', public=\'%s\', owner=\'%s\' WHERE id=%d', $edit['name'], $edit['description'], $edit['listorder'], $edit['active'], $edit['public'], $edit['owner'], $edit['lid']);
}
/**
* Retrieves a lists's statistics.
*
* @ingroup phplist_lists
* @param $lid The id value of the list.
* @return themed list statistics content.
*/
function phplist_get_list_stats($lid) {
echo 'get list stats: '. $lid;
return null;
}
/**
* Retrieves the list create form.
*
* @param $lid Optional. List id value to populate the form with.
* @ingroup phplist_lists
* @return form with the list creation fields.
*/
function phplist_list_form($lid=null) {
global $user;
$list = phplist_get_list($lid);
$form = form_textfield(t('Name'), 'name', $list->name, 40, '', t('This is the name of your mailing list'));
$form .= form_textarea(t('Description'), 'description', $list->description, 40, 5, t('This is the description of the mailing list'));
$form .= form_weight(t('Weight'), 'listorder', $list->listorder, 10, t('Optional. In listings, the heavier lists will sink and the lighter lists will be positioned nearer the top.'));
//$form .= form_checkbox(t('Public'), 'public', 1, $list->public, t('Checked: this list will be available for users on the system to subscribe and unsubscribe to.'));
$formgroup = form_radio('Public', 'public', 1, ($list->public), t('All users can sign up.'));
$formgroup .= form_radio('Private', 'public', 0, $list->public==0 && !variable_get('mm_hidden_'.trim(substr($list->name, 0, 35)), FALSE), t('List is hidden unless user is subscribed.'));
$formgroup .= form_radio('Private and Hidden', 'public', 'hidden', $list->public==0 && variable_get('mm_hidden_'.trim(substr($list->name, 0, 35)), FALSE), t('List is hidden from all users, even if they are subscribed.'));
$form .= form_group(t('Publicity'), $formgroup);
$form .= form_hidden('id', $list->id);
$form .= form_hidden('owner', $user->uid);
return $form;
}
/**
* @defgroup phplist_subscribers subscriber related functions for phplist.
*/
/**
* Subscribes or unsubscribes a user to a list.
*
* @ingroup phplist_subscribers
* @param $id The id value of the subscriber.
* @param $lid The id value of the list.
* @param $value Boolean true to subscribe, false to unsubscribe.
* @return boolean true if successful.
*/
function phplist_set_subscriber($id, $lid, $value) {
if($value) {
if (module_exist('civicrm')) {
civicrm_initialize(TRUE);
$groups = phplist_get_cicivrm_groups($lid);
db_set_active('civicrm');
$contact = array('contact_id' => $id);
$contact = crm_get_contact($contact);
foreach($groups as $gid) {
$group = array('id' => $gid);
$group = crm_get_groups($group);
$group_contact = array($contact);
crm_add_group_contacts($group[0], $group_contact);
}
db_set_active();
}
return db_query('REPLACE INTO {phplist_listuser} (userid, listid) VALUES (%d, %d)', $id, $lid);
}
else {
if (module_exist('civicrm')) {
civicrm_initialize(TRUE);
$groups = phplist_get_cicivrm_groups($lid);
db_set_active('civicrm');
$contact = array('contact_id' => $id);
$contact = crm_get_contact($contact);
foreach($groups as $gid) {
$group = array('id' => $gid);
$group = crm_get_groups($group);
$group_contact = array($contact);
crm_delete_group_contacts($group[0], $group_contact);
}
db_set_active();
}
return db_query('DELETE FROM {phplist_listuser} WHERE userid=%d AND listid=%d', $id, $lid);
}
}
/**
* Updates a subscribers information.
*
* @ingroup phplist_subscribers
* @param $user The user object to update
* @return boolean true if successful.
*/
function phplist_update_subscriber($subscriber) {
foreach($subscriber as $key=>$value) {
if($key!='id' && $key!='mail' && $key!='name') {
$values[] = $key.'=\''. db_escape_string($value) .'\'';
}
elseif($key=='mail') {
$values[] = 'email=\''. db_escape_string($value) .'\'';
}
}
$test = db_fetch_object(db_query('SELECT id FROM {phplist_user_user} WHERE email=\'%s\'', $subscriber['mail']));
if (!$test->id) {
// that email address is not in the subscriber database
if (!isset($subscriber['drupalid'])) {
//if we already have the drupalid, jump to the query
$drupal = user_load(array('mail'=>$subscriber->mail));
if($drupal) {
// that email is in the drupal users table, link this subscriber to that user
$values[] = 'drupalid=\''. $drupal->uid .'\'';
}
else {
//that email does not belong to a drupal user, remove any drupal user association
$values[] = 'drupalid=\'0\'';
}
}
return db_query('UPDATE {phplist_user_user} SET '. implode(', ', $values) .' WHERE id=%d', $subscriber['id']);
}
}
/**
* Retrieves a subscriber.
*
* @ingroup phplist_subscribers
* @param $id The id value of the subscriber.
* @return subscriber object.
*/
function phplist_get_subscriber($id) {
return db_fetch_object(db_query('SELECT *, email AS mail, email AS name, uniqid AS hash FROM {phplist_user_user} WHERE id=%d', $id));
}
/**
* Retrieves a subscriber by drupal system id.
*
* @ingroup phplist_subscribers
* @param $id The id value of the subscriber.
* @return subscriber object.
*/
function phplist_get_subscriber_by_system_id($uid) {
return db_fetch_object(db_query('SELECT *, email AS mail, email AS name, uniqid AS hash FROM {phplist_user_user} WHERE drupalid=%d', $uid));
}
/**
* Retrieves a subscriber by hash id.
*
* @ingroup phplist_subscribers
* @param $hash The hash value of the subscriber.
* @return subscriber object.
*/
function phplist_get_subscriber_by_unique_id($hash) {
return db_fetch_object(db_query('SELECT *, email AS mail, email AS name, uniqid AS hash FROM {phplist_user_user} WHERE uniqid=\'%s\'', $hash));
}
/**
* Retrieves a list's subscribers.
*
* @ingroup phplist_subscribers
* @param $lid The id value of the list.
* @return array of subscriber objects.
*/
function phplist_get_subscribers($lid, $page) {
$limit = variable_get('massmailer_query_limit', 250);
$offset = $limit*$page;
$result = db_query('SELECT u.*, u.email AS mail, email AS name, u.uniqid AS hash FROM {phplist_user_user} u LEFT JOIN {phplist_listuser} j ON j.userid=u.id WHERE j.listid=%d ORDER BY email LIMIT %d, %d', $lid, $offset, $limit);
$subscribers = array();
while($subscriber = db_fetch_object($result)){
$subscribers[] = $subscriber;
}
return $subscribers;
}
/**
* Retrieves all active subscribers.
*
* @ingroup phplist_subscribers
* @return array of subscriber objects.
*/
function phplist_get_active_subscribers($page) {
$limit = variable_get('massmailer_query_limit', 250);
$offset = $limit*$page;
$result = db_query('SELECT *, email AS mail, email AS name, uniqid AS hash FROM {phplist_user_user} WHERE disabled!=1 ORDER BY email LIMIT %d, %d', $offset, $limit);
$subscribers = array();
while($subscriber = db_fetch_object($result)){
$subscribers[] = $subscriber;
}
return $subscribers;
}
/**
* Retrieves all subscribers.
*
* @ingroup phplist_subscribers
* @return array of subscriber objects.
*/
function phplist_get_all_subscribers($page) {
$limit = variable_get('massmailer_query_limit', 250);
$offset = $limit*$page;
$result = db_query('SELECT *, email AS mail, email AS name, uniqid AS hash FROM {phplist_user_user} ORDER BY email LIMIT %d, %d', $offset, $limit);
$subscribers = array();
while($subscriber = db_fetch_object($result)){
$subscribers[] = $subscriber;
}
return $subscribers;
}
/**
* Retrieves a subscriber's lists.
*
* @ingroup phplist_subscribers
* @param $id The id value of the subscriber.
* @return array of list objects.
*/
function phplist_get_subscriber_lists($id) {
$result = db_query('SELECT l.* FROM {phplist_list} l LEFT JOIN {phplist_listuser} j ON l.id=j.listid WHERE j.userid=%d', $id);
$lists = array();
while($list = db_fetch_object($result)) {
$list->lid = $list->id;
$lists[] = $list;
}
return $lists;
}
/**
* Checks to se if a subscriber is subscribed to a list.
*
* @ingroup phplist_subscribers
* @param $id The id value of the subscriber.
* @param $lid The id value of the list.
* @return boolean true is the user is subscribed to the list.
*/
function phplist_is_subscribed($id, $lid) {
$list = db_fetch_object(db_query('SELECT listid FROM {phplist_listuser} WHERE userid=%d AND listid=%d', $id, $lid));
if($list->listid) {
return true;
}
else {
return false;
}
}
/**
* Deletes a subscriber.
*
* @ingroup phplist_subscribers
* @param $id The id value of the subscriber.
* @return boolean.
*/
function phplist_delete_subscriber($id, $remove_from_groups = FALSE) {
$success = true;
if(!db_query('DELETE FROM {phplist_user_user} WHERE id=%d', $id)) {
$success = false;
}
if(!db_query('DELETE FROM {phplist_listuser} WHERE userid=%d', $id)) {
$success = false;
}
if (module_exist('civicrm')) {
civicrm_initialize(TRUE);
$groups = phplist_get_cicivrm_groups();
db_set_active('civicrm');
$contact = array('contact_id' => $id);
$contact = crm_get_contact($contact);
if ($remove_from_groups) {
foreach($groups as $gid) {
$group = array('id' => $gid);
$group = crm_get_groups($group);
$group_contact = array($contact);
crm_delete_group_contacts($group[0], $group_contact);
}
}
$contact->do_not_email = 1;
crm_update_contact($contact);
db_set_active();
}
return $success;
}
/**
* Retrieves a subscriber's statistics.
*
* @ingroup phplist_subscribers
* @param $id The id value of the subscriber.
* @return themed subscriber statistics content.
*/
function phplist_get_subscriber_stats($id) {
return array();
}
/**
* Adds an array of subscribers to a list.
*
* @ingroup phplist_subscribers
* @param $subscribers An array of subscriber objects.
* @param $lid The id value of the list.
* @return boolean true if successful.
*/
function phplist_add_subscribers($subscribers, $lid) {
$success = true;
foreach ($subscribers as $subscriber) {
if ($subscriber->id) {
// subscriber exists already, just assign them to the list
$id = $subscriber->id;
}
else {
$test = db_fetch_object(db_query('SELECT id FROM {phplist_user_user} WHERE email=\'%s\'', $subscriber->mail));
if (!$test->id) {
// that email address is not in the subscriber table, create a new subscriber
$drupal = user_load(array('mail'=>$subscriber->mail));
$drupalid = 0;
if($drupal) {
// that email is in the drupal users table, link this new entry to that user
$drupalid = $drupal->uid;
}
$subscribe = TRUE;
// generate unique hash id from Drupal / CiviCRM data
if (module_exist('civicrm')) {
civicrm_initialize(TRUE);
db_set_active('civicrm');
$get_by_email = array('email' => $subscriber->mail);
$contact = crm_get_contact($get_by_email);
if (strtolower(get_class($contact)) != 'crm_contact_bao_contact') {
$contact = crm_create_contact($get_by_email);
}
db_set_active();
if(strtolower(get_class($contact)) != 'crm_contact_bao_contact') {
$subscribe = FALSE;
drupal_set_message(t('The email address %email could not be subscribed. Creating a CiviCRM contact failed.', array('%email' => $edit['email'])), 'error');
}
$hash = md5(CIVICRM_DOMAIN_ID .":$contact->id:$drupalid");
if ($contact->do_not_email == 1) {
$subscribe = FALSE;
drupal_set_message(t('The email address %email could not be subscribed as the CiviCRM contact data specified "do not email".', array('%email' => $edit['email'])), 'error');
}
}
else {
$hash = md5("0:$subscriber->id:$drupalid");
}
if ($subscribe) {
$id = db_result(db_query('SELECT MAX(id) FROM {phplist_user_user}'));
$id++;
if(!db_query('REPLACE INTO {phplist_user_user} (id, email, uniqid, drupalid, disabled, confirmed, htmlemail) VALUES (%d, \'%s\', \'%s\', %d, 0, 1, 1)', $id, $subscriber->mail, $hash, $drupalid)) {
$success = false;
}
}
else {
$success = false;
}
}
else {
// that email exists in the subscriber db already, subscribe them to the list
$id = $test->id;
}
}
$test = db_fetch_object(db_query('SELECT userid FROM {phplist_listuser} WHERE userid=%d AND listid=%d', $id, $lid));
if(!$test->userid) {
// subscriber is not already subscribed to the list
if(!db_query('REPLACE INTO {phplist_listuser} (userid, listid) VALUES (%d, %d)', $id, $lid)) {
$success = false;
}
}
}
return $success;
}
/**
* Adds a CiviCRM group of contacts to a list.
*
* @ingroup phplist_subscribers
* @param $gid The id of the group.
* @param $lid The id value of the list.
* @return boolean true if successful.
*/
function phplist_add_civicrm_group($lid, $gid) {
return db_query('INSERT INTO {phplist_civicrm_groups} (lid, gid) VALUES (%d, %d)', $lid, $gid);
}
/**
* Removes a CiviCRM group of subscribers from a list.
*
* @ingroup phplist_subscribers
* @param $gid The id of the CiviCRM group.
* @param $lid The id value of the list.
* @return boolean true if successful.
*/
function phplist_remove_civicrm_group($lid, $gid) {
if (module_exist('civicrm')) {
civicrm_initialize(TRUE);
db_set_active('civicrm');
$group = array('id' => $gid);
$group = crm_get_groups($group);
$contacts = crm_get_group_contacts($group[0]);
db_set_active();
$addresses = array();
foreach ($contacts as $contact) {
$addresses[] = $contact->email;
}
if (count($addresses)) {
$result = db_query("SELECT id FROM {phplist_user_user} WHERE email IN (%s)", implode("','", $addresses));
while ($id = db_result($result)) {
db_query('DELETE FROM {phplist_listuser} WHERE userid = %d AND listid = %d', $id, $lid);
}
}
}
return db_query('DELETE FROM {phplist_civicrm_groups} WHERE lid = %d AND gid = %d', $lid, $gid);
}
/**
* Retrieves the CiviCRM group ids subscribed to a list.
*
* @ingroup phplist_subscribers
* @param $lid The id value of the list.
* @return array of CiviCRM group ids that are subscribed to the list.
*/
function phplist_get_cicivrm_groups($lid = 0) {
if ($lid) {
$results = db_query('SELECT gid FROM {phplist_civicrm_groups} WHERE lid = %d', $lid);
}
else {
$results = db_query('SELECT gid FROM {phplist_civicrm_groups}');
}
$sids = array();
while($sid = db_fetch_object($results)) {
$sids[] = $sid->gid;
}
return $sids;
}
/**
* @defgroup phplist_message message related functions for phplist.
*/
/**
* Retrieves a message.
*
* @ingroup phplist_message
* @param $mid The id value of the message.
* @return message object.
*/
function phplist_get_message($mid) {
$msg = db_fetch_object(db_query('SELECT id AS mid, subject, message AS body, fromfield AS fromfield, embargo AS timestamp, owner AS uid, htmlformatted AS message_type, status FROM {phplist_message} WHERE id=%d', $mid));
$msg->message_type = ($msg->message_type ? 'html' : 'text');
$msg->subject = mime_header_decode($msg->subject);
return $msg;
}
/**
* Retrieves messages sent to a list.
*
* @ingroup phplist_message
* @param $lid The id value of the list.
* @param $unsent Boolean true retrieves only unsent messages.
* @return assoc array of keys and replacement value descriptions.
*/
function phplist_get_messages($lid=null, $unsent = false) {
if($lid) {
$results = db_query('SELECT m.id AS mid, m.subject AS subject, m.message AS body, fromfield, m.embargo AS timestamp, m.owner AS uid, htmlformatted AS message_type, status FROM {phplist_message} m LEFT JOIN {phplist_listmessage} l ON l.messageid=m.id WHERE l.listid=%d'.($unsent?' AND m.sent!=NULL':'') .' ORDER BY timestamp', $lid);
}
else {
$results = db_query('SELECT m.id AS mid, m.subject AS subject, m.message AS body, fromfield, m.embargo AS timestamp, m.owner AS uid, htmlformatted AS message_type, status FROM {phplist_message} m ORDER BY timestamp');
}
$msgs = array();
while($msg = db_fetch_object($results)) {
$msg->send_date = format_date(strtotime($msg->timestamp .' UTC'), 'custom', 'M d, Y');
$msg->message_type = ($msg->message_type ? 'html' : 'text');
$msg->subject = mime_header_decode($msg->subject);
$msgs[] = $msg;
}
return $msgs;
}
/**
* Deletes a message.
*
* @ingroup phplist_lists
* @param $mid id value of the message.
* @return boolean true if successful.
*/
function phplist_delete_message($mid) {
$success = true;
$success = db_query('DELETE FROM {phplist_listmessage} WHERE messageid=%d', $mid);
$success = db_query('DELETE FROM {phplist_message} WHERE id=%d', $mid);
return $success;
}
/**
* Retrieves a message's statistics.
*
* @ingroup phplist_message
* @param $mid The id value of the list.
* @return themed message statistics content.
*/
function phplist_get_message_stats($mid) {
return null;
}
/**
* Returns the replacement key values available for email messages.
*
* @ingroup phplist_message
* @return assoc array of keys and replacement value descriptions.
*/
function phplist_get_email_keys() {
//echo '_get_email_keys';
return array();
}
/**
* @defgroup phplist_mail mailing related functions for phplist.
*/
/**
* Queues a email message for delivery.
*
* @ingroup phplist_mail
* @param $lids An array of id values of the lists to send message to.
*/
function phplist_send_mail($lids, $mid, $subject, $message_type, $body_header, $body, $footer, $from, $timestamp=null) {
global $user;
// despite the name this actually just queues the mail, but other MLM engines may not implement a mail queue so we just create the message and hold it for a bit.
if ($message_type=='html') {
$sendformat = "text and HTML";
$message_type = 1;
}
else {
$sendformat = "text";
$message_type = 0;
}
$embargo = date('Y-m-d H:i:s', $timestamp);
if($mid) {
$id = $mid;
$msg = $body;
db_query('REPLACE INTO {phplist_message} (id, subject, fromfield, message, footer, embargo, owner, modified, status, htmlformatted, sendformat) VALUES (%d, \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'submitted\', %d, \'%s\')', $id, mime_header_encode($subject), $from, $msg, $footer, $embargo, $user->uid, date('YmdHis'), $message_type, $sendformat);
}
else {
$id = db_next_id('phplist_message');
$msg = ($body_header ? $body_header ."\n\n" : '') . $body . ($footer ? "\n\n-- \n". $footer : '');
db_query('INSERT INTO {phplist_message} (id, subject, fromfield, message, footer, embargo, owner, entered, modified, status, htmlformatted, sendformat) VALUES (%d, \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'submitted\', %d, \'%s\')', $id, mime_header_encode($subject), $from, $msg, $footer, $embargo, $user->uid, date('Y-m-d'), date('YmdHis'), $message_type, $sendformat);
}
db_query('DELETE FROM {phplist_listmessage} WHERE messageid=%d', $id);
db_query('DELETE FROM {phplist_usermessage} WHERE messageid=%d', $id);
foreach($lids as $lid) {
db_query('INSERT INTO {phplist_listmessage} (messageid, listid, entered, modified) VALUES (%d, %d, \'%s\', \'%s\')', $id, $lid, date('Y-m-d'), date('YmdHis'));
}
}
/**
* Processes the email queue.
*
* search for email, if a match is found on a contact, then contact->uid is checked. If it exists then the subscriber.drupalid value is set, allowing the user to maintain their subscription.
* @ingroup phplist_mail
* @return html formatted return message.
*/
function phplist_process_queue() {
global $base_url;
if (module_exist('civicrm')) {
civicrm_initialize(TRUE);
$messages = db_query("SELECT m.id as mid FROM {phplist_message} m WHERE m.status = 'submitted' AND m.embargo <= '%s' ORDER BY m.embargo", date('Y-m-d H:i:s'));
// retrieve contacts in searches, compare emails to subscriber table, and assign new ones to the target lists before running the queue
// IF contact->mail = subscriber->mail AND contact->uid THEN subscriber->drupalid = contact->uid ELSE add_subscriber
while($message = db_fetch_object($messages)) {
$lists = phplist_get_lists($message->mid);
foreach($lists as $list) {
$groups = phplist_get_cicivrm_groups($list->lid);
foreach($groups as $group) {
$contacts = phplist_get_contacts($group);
foreach($contacts as $contact) {
$test = db_fetch_object(db_query('SELECT id FROM {phplist_user_user} WHERE email=\'%s\'', $contact->email));
if (!$test->id && $contact->do_not_email == 0 && isset($contact->email)) {
// that email address is not in the subscriber table, create a new subscriber
$drupal = user_load(array('mail' => $contact->email));
$drupalid = 0;
if($drupal) {
// that email is in the drupal users table, link this new entry to that user
$drupalid = $drupal->uid;
}
// generate unique id from Drupal / CiviCRM data
$hash = md5("$contact->domain_id:$contact->id:$drupalid");
$id = db_next_id('phplist_subscriber');
db_query("REPLACE INTO {phplist_user_user} (id, email, uniqid, drupalid, disabled, confirmed, htmlemail) VALUES (%d, '%s', '%s', %d, 0, 1, 1)", $id, $contact->email, $hash, $drupalid);
}
else if ($contact->do_not_email == 0 && isset($contact->email)) {
// that email address exists in the subscriber table already, set the id
$id = $test->id;
}
$test = db_fetch_object(db_query('SELECT userid FROM {phplist_listuser} WHERE userid=%d AND listid=%d', $id, $list->lid));
if ($test->userid && $contact->do_not_email) {
db_query('DELETE FROM {phplist_listuser} WHERE userid = %d AND listid = %d', $id, $list->lid);
}
else if(!$test->userid) {
// subscriber is not already subscribed to the list
db_query('INSERT INTO {phplist_listuser} (userid, listid) VALUES (%d, %d)', $id, $list->lid);
}
}
}
}
}
}
if(!isset($_GET['confirm'])) {
- $output .= t('Click here to continue to send.',array('url-status', 'admin/massmailer/message','%url-sendconfirm'=>'admin/massmailer/queue?confirm=1'));
+ $output .= t('Click here to continue to send.',array('url-status', '?q=admin/massmailer/message','%url-sendconfirm'=>'?q=admin/massmailer/queue&confirm=1'));
return $output;
}
// Set phplist password
global $db_url;
$db_str = is_array($db_url) ? (isset($db_url['phplist']) ? $db_url['phplist'] : $db_url['default']) : $db_url;
$url = parse_url($db_str);
db_query("UPDATE {phplist_admin} SET password = '%s', email = '%s' WHERE loginname = 'admin'", $url['pass'], variable_get('site_mail', ini_get('sendmail_from')));
$postdata = array( 'login' => 'admin', 'password' => $url['pass']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url.'/modules/massmailer/engines/phplist/phplist/admin/?page=processqueue' );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo str_replace('',"",curl_exec($ch));
exit;
}
/**
* Returns contact emails from a CiviCRM group.
*
* @param $gid The id of the CiviCRM group.
* @ingroup phplist_functions
* @return array of contact objects.
*/
function phplist_get_contacts($gid) {
if (module_exist('civicrm')) {
civicrm_initialize(TRUE);
db_set_active('civicrm');
$group = array('id' => $gid);
$groups = crm_get_groups($group);
$contacts = crm_get_group_contacts($groups[0]);
db_set_active();
return $contacts;
}
return array();
}
function phplist_cron() {
if (module_exist('civicrm')) {
+
civicrm_initialize(TRUE);
- // Check existance of subscribed groups.
+
+ // Get unsubscribed emails from phplist
+ $result = db_query('SELECT email FROM {phplist_user_blacklist}');
+ $emails = array();
+ while ($email = db_fetch_object($result)) {
+ $emails[] = $email->email;
+ }
+
db_set_active('civicrm');
+
+ // mark these contacts as do_not_email
+ foreach ($emails as $email) {
+ $contact = crm_get_contact(array('email' => $email));
+ if ( ! is_a( $contact, 'CRM_Core_Error' ) ) {
+ crm_update_contact(&$contact, array('do_not_email' =>1));
+ }
+ }
+
+ // Check existance of subscribed groups.
$groups = crm_get_groups(NULL, array('id'));
db_set_active();
$gids = array();
foreach ($groups as $group) {
$gids[] = $group->id;
}
db_query('DELETE FROM {phplist_civicrm_groups} WHERE gid NOT IN (%s)', implode(',', $gids));
+
}
phplist_process_queue();
}
if (!function_exists('mime_header_decode')) {
function mime_header_decode($header) {
// First step: encoded chunks followed by other encoded chunks (need to collapse whitespace)
$header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', '_mime_header_decode', $header);
// Second step: remaining chunks (do not collapse whitespace)
return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', '_mime_header_decode', $header);
}
}
if (!function_exists('_mime_header_decode')) {
function _mime_header_decode($matches) {
// Regexp groups:
// 1: Character set name
// 2: Escaping method (Q or B)
// 3: Encoded data
$data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
if (strtolower($matches[1]) != 'utf-8') {
$data = drupal_convert_to_utf8($data, $matches[1]);
}
return $data;
}
}
?>