teasers) { case 0: return; case 1: $txt = $node->teaser; break; case 2: $txt = $node->body; } $txt = ereg_replace ("(
|
|

)", notify_lb(), $txt); $txt = html_entity_decode($txt); $txt = strip_tags($txt); return $txt; } function notify_lb() { $os = php_uname(); switch ($os) { case strstr($os, 'Windows'): return "\r\n"; case strstr($os, 'nix'): return "\n"; case strstr($os, 'Mac'): return "\r"; default: return "\n"; } } function notify_send() { global $base_url; $period = variable_get("notify_send_last", time() - variable_get("notify_send", 86400)); // Fetch all new nodes $nresult = db_query("SELECT n.body, n.nid, n.type, n.title, n.promote, n.moderate, n.teaser, n.created, n.changed, u.name FROM {node} n LEFT JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND n.created > %d ORDER BY n.created", $period); $php_node_types = array("book", "page"); while ($node = db_fetch_object($nresult)) { // 030312AX: do not notify / show nodes containing php code // TODO: should these be eval'ed? if (!(in_array($node->type, $php_node_types) && db_fetch_object(db_query("SELECT nid FROM {$node->type} WHERE nid = %d AND format = 1", $node->nid)))) { $nodes[$node->nid] = $node; } } // Fetch new comments $cresult = db_query("SELECT c.cid, c.nid, c.subject, c.pid, u.name FROM {comments} c LEFT JOIN {users} u ON u.uid = c.uid WHERE c.status = 0 AND c.timestamp > %d ORDER BY c.nid, c.timestamp", $period); while ($comment = db_fetch_object($cresult)) { $comments[$comment->nid][] = $comment; } // Fetch users with notify enabled $uresult = db_query("SELECT uid, status, node, teasers, comment FROM {notify} WHERE status = 1". (($notify_attempts = variable_get(notify_attempts, 5)) ? " AND attempts <= %d" : ""), $notify_attempts); $from = variable_get("site_mail", ini_get("sendmail_from")); while ($notify = db_fetch_object($uresult)) { $user = user_load(array("uid" => $notify->uid)); $subject = variable_get("site_name", "drupal") ." ". t("notification for %name", array ("%name" =>$user->name)); $body = ""; // New content if ($notify->node && count($nodes)) { $body .= t("Recent content") ."\n". str_repeat("-", strlen(t("Recent content"))) ."\n\n"; foreach ($nodes as $node) { if ($notify->moderate) { $body .= strtr(t("%status %type by %author: %title"), array("%status" => "queued", "%type" => $node->type, "%title" => $node->title, "%author" => ($node->name ? $node->name : variable_get(anonymous, "Anonymous")))) ."\n"; $body .= notify_content($node, $notify); // 040701AH Applied patch by Capnj (http://drupal.org/node/view/7166#comment-7892) $body .= " [ ". url("queue/view/$node->nid", NULL, NULL, TRUE) ." ]\n\n"; } elseif (!$notify->moderate && $node->nid) { $body .= strtr(t("%status %type by %author: %title"), array("%status" => "published", "%type" => $node->type, "%title" => $node->title, "%author" => ($node->name ? $node->name : variable_get(anonymous, "Anonymous")))) ."\n"; $body .= notify_content($node, $notify); // 040701AH Applied patch by Capnj (http://drupal.org/node/view/7166#comment-7892) $body .= " [ ". url("node/view/$node->nid", NULL, NULL, TRUE) ." ]\n\n"; } } } // New comments if ($notify->comment && count($comments)) { $body .= t("Recent comments") ."\n". str_repeat("-", strlen(t("Recent comments"))) ."\n\n"; foreach ($comments as $nid => $comment) { if ($nid != $nid_old) { if (strlen($nodes[$nid]->title) < 2) { $result = db_query("SELECT title FROM {node} WHERE nid = %d", $nid); $nodes[$nid] = db_fetch_object($result); } $body .= strtr(t("%count new comments attached to: %title"), array("%count" => count($comment), "%title" => $nodes[$nid]->title)) ."\n"; $nid_old = $nid; } foreach ($comment as $c) { // 040701AH Applied patch by Capnj (http://drupal.org/node/view/7166#comment-7892) $body .= " ". strtr(t("%title by %author"), array("%title" => $c->subject, "%author" => ($c->name ? $c->name : variable_get(anonymous, "Anonymous")))) ."\n" . " ". url("node/view/$nid/$c->cid#$c->cid", NULL, NULL, TRUE) ."\n\n"; } } } if ($body) { $to = $user->mail; $body = t("Greetings") ." ". $user->name .",\n\n$body"; $body .= "\n-- \n"; $body .= t("This is an automatic mail from") ." ". variable_get("site_name", "drupal") ."\n"; $body .= t("To stop receiving these mails go to") ." $base_url/notify\n"; if (!user_mail($to, $subject, wordwrap($body, 72), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: <$from>\nErrors-to: $from\n")) { db_query("UPDATE {notify} SET attempts = attempts + 1 WHERE uid = %d", $user->uid); } } } } function notify_settings() { $period = array(900 => format_interval(900), 1800 => format_interval(1800), 3600 => format_interval(3600), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600), 2419200 => format_interval(2419200), 1000000000 => "Never"); $output .= form_select(t("Send notifications every"), "notify_send", variable_get("notify_send", 86400), $period, t("Requires crontab.")); $output .= form_select(t("Number of failed sends after which notifications are disabled"), "notify_attempts", variable_get("notify_attempts", 5), array(t("Disabled"), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20)); return $output; } function notify_cron() { if (time() - variable_get("notify_send_last", 0) > variable_get("notify_send", 86400)) { notify_send(); variable_set("notify_send_last", time()); } } function notify_perm() { return array("access notify", "administer notify"); } function notify_link($type) { // 040701AH changed positions/titles in menu if ($type == "system") { if (user_access('access notify')) { menu("user/notify", t("email notification"), "notify_page"); } if (user_access("administer notify")) { menu("admin/user/notify", t("notifications"), "notify_admin"); } } return array(); } function notify_page() { global $user; $op = $_POST["op"]; $edit = $_POST["edit"]; if (user_access("access notify") && $user) { switch ($op) { case "cron": notify_send(); break; case t("Save settings"): db_query("REPLACE {notify} (uid, status, node, teasers, comment) VALUES (%d, %d, %d, %d, %d)", $user->uid, $edit["status"], $edit["node"], $edit["teasers"], $edit["comment"]); drupal_set_message(t("Notify settings saved")); default: $result = db_query("SELECT u.name, u.mail, n.status, n.node, n.teasers, n.comment FROM {users} u LEFT JOIN {notify} n ON u.uid = n.uid WHERE u.uid = %d AND u.status = 1 ORDER BY u.name", $user->uid); $notify = db_fetch_object($result); $form .= form_select(t("Notify status"), "status", $notify->status, array(t("Disabled"), t("Enabled")), t("Do you wish to receive periodic emails when new content is posted?")); $form .= form_select(t("Notify new content"), "node", $notify->node, array(t("Disabled"), t("Enabled")), t("Include new content in the notification mail.")); $form .= form_select(t("Content"), "teasers", $notify->teasers, array(t("Title only"), t("Title + Teaser"), t("Title + Body")), t("Select the amount of each post that you would like to see in your notification emails.")); $form .= form_select(t("Notify new comments"), "comment", $notify->comment, array(t("Disabled"), t("Enabled")), t("Include new comments in the notification mail.")); $form .= form_submit(t("Save settings")); print theme("page", form($form)); } } else { print message_access(); } } function notify_admin() { // 040701AH Fixed to show/edit all users instead of only users that have set // notification settings, fixed theming. $op = $_POST["op"]; switch ($op) { case "Save": $edit = $_POST["edit"]; foreach ($edit as $uid=>$settings) { // 040701AH Changed to create a new entry in the db in the user doesn't // have one yet $result = db_query("SELECT uid FROM {notify} WHERE uid = %d", $uid); if (is_object(db_fetch_object($result))) { db_query("UPDATE {notify} SET status = %d, node = %d, teasers = %d, comment = %d WHERE uid = %d", $settings['status'], $settings['node'], $settings['teasers'], $settings['comment'], $uid); } else if ($settings['status']) { db_query("INSERT INTO {notify} (uid,status,node,teasers,comment) VALUES (%d,%d,%d,%d,%d)", $uid, $settings['status'], $settings['node'], $settings['teasers'], $settings['comment']); } } default: // 040701AH Using n.* instead of listing all the n. cells NULLs the uid // cells when a notify entry is not available; $result = db_query("SELECT u.uid, u.name, u.mail, n.status, n.node, n.comment, n.attempts, n.teasers FROM {users} u LEFT JOIN {notify} n ON n.uid = u.uid WHERE u.status = 1 ORDER BY u.name"); $header = array (t("username"), t("email address"), t("enable"), t("content"), t("teasers"), t("comment"), t("failed attempts")); $i = 0; while ($notify = db_fetch_object($result)) { $rows[$i][] = format_name($notify); $rows[$i][] = $notify->mail; // 030312AX TODO: it really is a shame that we cannot use form_* functions // here (as they wrap everything into

title description

). implement // this once (see mailing list)! // 040701AH Removed hidden form elements that didn't do anything // Reenstated the status field $rows[$i][] = 'status ? ' checked="checked"' : '') .'>'; $rows[$i][] = 'node ? ' checked="checked"' : '') .'>'; $select = ""; foreach (array(t("Title only"), t("Title + Teaser"), t("Title + Body")) as $key => $choice) { $select .= '"; } $rows[$i][] = ''; $rows[$i][] = 'comment ? ' checked="checked"' : '') .">"; $rows[$i][] = $notify->attempts; $i++; } $output = theme("table", $header, $rows); $output .= form_submit("Save"); // 040701AH fixed formatting print theme("page", form($output)); } } ?>