1, 'mail' => variable_get('site_mail', ''), 'name' => variable_get('site_name', t('Drupal')), ); // TODO incorrect assumption that sender is "mail". $message['sender'] = send_contact('mail', $sender); $scids[] = $message['sender']->scid; // Log the activity. $send = (object) array( 'scid' => $message['sender']->scid, 'profile' => $profile['name'], 'timestamp' => time(), 'message' => $message['message'], 'subject' => $message['subject'], 'contact_count' => 0, ); drupal_write_record('send', $send); // Log any nodes that were sent. foreach($nids as $nid) { $send_node = array('sid' => $send->sid, 'nid' => $nid); drupal_write_record('send_node', $send_node); } foreach ($recipients as $r) { $r = (object) $r; $r_mode = (isset($r->mode)) ? $r->mode : $mode; // Convert the recipient to a send_contact entry. $r = send_contact($r_mode, $r); if(!in_array($r->scid, $scids)) $scids[] = $r->scid; $r_message = $message; // Set the message, replacing any tokens, etc. //send_set_message($r_message, $profile, $nids, $sender, $r); // Add a hash identifier to the recipient instance for tracking, etc. $r->hash = user_password(10); // Allow other modules to track or alter this send message/activity. drupal_alter('send_message', $r_message, $profile, $r, $nids, $r_mode); // Effect a delivery using the selected sending mechanism. if (function_exists($func = $modes[$r_mode]['callback'])) { $r_count = (int) $func($profile, $r, $nids, $r_message, $r_mode); } $send->contact_count += $r_count; $recipient = (object) array( 'sid' => $send->sid, 'scid' => $r->scid, 'hash' => $r->hash, 'contact_count' => $r_count, ); drupal_write_record('send_recipient', $recipient); } // Update the send table's contact_count. if ($send->contact_count) { drupal_write_record('send', $send, 'sid'); } // Update the "last used" timestamp on contact entries. if ($scids) { db_query("UPDATE {send_contact} SET timestamp = %d WHERE scid IN (". join(', ', $scids) .")", $send->timestamp); } return $send->contact_count; } /** * Generate a unique send contact entry for anonymous and authenticated send * contacts, based on the send method and a unique identifier ( e.g. mail ) */ function _send_contact($mode, $contact) { $contact = (object) $contact; if (isset($contact->scid)) return $contact; $contact->mode = $mode; // Where supplementary contact data may be stored. $table = 'send_contact_'. check_plain($mode); if (!db_table_exists($table)) $table = ''; // See if the entry already exists. if (isset($contact->uid) && $contact->uid) { // Only one uid + mode combination allowed, so search by uid if possible. if ($row = db_fetch_object(db_query("SELECT * FROM {send_contact} WHERE uid = %d AND mode = '%s'", $contact->uid, $mode))) { $contact->scid = $row->scid; if ($table) { if ($row = db_fetch_array(db_query("SELECT * FROM {$table} WHERE scid = %d", $row->scid))) { foreach ($row as $key => $val) $contact->key = $contact->val; } } } } elseif ($table) { // Search the table for a unique value. $schema = drupal_get_schema($table); $query = array(); if (isset($schema['unique keys'])) { foreach ($schema['unique keys'] as $keys) { foreach ($keys as $key) { $place = db_type_placeholder($schema['fields'][$key]['type']); $query[] = "$key = $place"; $values[] = $contact->$key; } } if ($row = db_fetch_array(db_query("SELECT * FROM {$table} WHERE ". join(' AND ', $query), $values))) { foreach ($row as $key => $val) $contact->$key = $val; } } } // Save a new entry if we didn't just load one. if (!$contact->scid) { drupal_write_record('send_contact', $contact); if ($table) { drupal_write_record($table, $contact); } } // Allow modules to save and/or alter the contact entry before we return it. drupal_alter('send_contact', $contact); return $contact; } /** * Delivery callback for Send module's built-in delivery mode of "mail". */ function send_deliver_mail($profile, $contact, $nids, &$message, $mode) { $sender = $message['sender']; if (module_exists('mimemail')) { // Use mimemail if available, to support HTML messages. return mimemail($sender, $contact, $message['subject'], $message['body']); } else { // TODO populate this. return drupal_mail(); } } /** * Set configuration variables for send */ function _send_value($name, $module='', $nodetype = '', $node = NULL, $value = NULL) { $nid = is_object($node) ? $node->nid : 0; $pernode = variable_get("{$module}_{$nodetype}_pernode", 0); // set a new value if (!is_null($value)) { if ($nid && !$pernode) { return; // trying to set a per-node value but can't } db_query("DELETE FROM {send_setting} WHERE name='%s' AND module='%s' AND nodetype='%s' AND nid=%d" , $name, $module, $nodetype, $nid); /* We override in this order: module, nodetype, node don't set this value if it's the same as the one above it in the stack */ if ($nid) { // this node has the same value as its nodetype if ($value == _send_value($name, $module, $nodetype)) return; } elseif ($nodetype) { // this nodetype has the same value as its module if ($value == _send_value($name, $module)) return; // for future consistency, delete children that would inherit this value db_query("DELETE FROM {send_setting} WHERE name='%s' AND module='%s' AND nodetype='%s' AND value='%s'" , $name, $module, $nodetype, $value); } elseif ($module) { // this module has the same value as the default if ($value == _send_value($name)) return; // for future consistency, delete children that would inherit this value db_query("DELETE FROM {send_setting} WHERE name='%s' AND module='%s' AND value='%s'" , $name, $module, $value); } // save the value db_query("INSERT INTO {send_setting} ( name, module, nodetype, nid, value ) VALUES ('%s', '%s', '%s', %d, '%s')", $name, $module, $nodetype, $nid, $value); return; } // try getting value from variables to save resources if (!$pernode && $val = variable_get("{$module}_{$nodetype}_{$name}", FALSE)) { return $val; } // get value from database if ($val = db_result(db_query("SELECT value FROM {send_setting} WHERE name='%s' AND nid IN (0, %d) AND nodetype IN ('', '%s') AND module IN ('', '%s') ORDER BY (nid = 0), (nodetype = ''), (module = '') LIMIT 1", $name, $nid, $nodetype, $module))) { return $val; } // return module's default value if ($val = module_invoke($module, 'send', $name, $module, $nodetype, $node)) { return $val; } // return send's default value if ($val = module_invoke('send', 'send', $name, $module, $nodetype, $node)) { return $val; } return; }