Index: subscriptions_mail.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/subscriptions/Attic/subscriptions_mail.module,v
retrieving revision 1.1.2.8
diff -u -r1.1.2.8 subscriptions_mail.module
--- subscriptions_mail.module	2 Feb 2008 01:47:35 -0000	1.1.2.8
+++ subscriptions_mail.module	8 Feb 2008 11:11:29 -0000
@@ -1,58 +1,58 @@
-<?php
-// $Id: subscriptions_mail.module,v 1.1.2.8 2008/02/02 01:47:35 chx Exp $s
-
-function subscriptions_mail_cron() {
-  global $user;
-  include_once drupal_get_path('module', 'subscriptions_mail') .'/subscriptions_mail.templates.inc';
-
-  $number_of_mails = variable_get('subscriptions_number_of_mails', 200);
-  $from = variable_get('site_mail', ini_get('sendmail_from'));
-  $old_uid = 0;
-  $counter = 0;
-  $loaded_objects = array();
-  $users = array();
-  $fields = array();
-  $mails = array();
-  session_save_session(FALSE);
-  while ($counter++ < $number_of_mails) {
-    subscriptions_mail_mail_edit_variables();  // clear cache
-    $result = db_query_range('SELECT * FROM {subscriptions_queue} WHERE last_sent + send_interval < %d ORDER BY sqid', time(), 0, 1);
-    if ($s = db_fetch_array($result)) {
-      if (!isset($users[$s['uid']])) {
-        $users[$s['uid']] = user_load(array('uid' => $s['uid']));
-      }
-      $saved_user = $user;
-      $user = $users[$s['uid']];
-      if ($user->status && $user->access) {
-        $cids = array();
-        $load_function = $s['load_function'];
-        $index = $load_args = $s['load_args'];
-        if (!isset($loaded_objects[$user->uid][$load_function][$load_args])) {
-          if (is_numeric($load_args)) {
-            $object = $load_function($load_args, $s['sqid'], $s['is_new']);
-          }
-          else {
-            $load_args = unserialize($load_args);
-            $load_args[] = $s['is_new'];
-            $object = call_user_func_array($load_function, $load_args);
-          }
-          if (isset($object)) {
-            $access = module_invoke_all('subscriptions', 'access', $load_function, $load_args, $object);
-            // One FALSE vote is enough to deny. Also, we need a non-empty array.
-            $allow = !empty($access) && array_search(FALSE, $access) === FALSE;
-            $loaded_objects[$user->uid][$load_function][$index] = $allow ? $object : FALSE;
-          }
-        }
-        if ($object = $loaded_objects[$user->uid][$load_function][$index]) {
-          if (!isset($users[$object->uid])) {
-            $users[$object->uid] = user_load(array('uid' => $object->uid));
-          }
-          $user = $users[$object->uid];
-          $module = $s['module'];
-          $ori_field = $field = $s['field'];
+<?php
+// $Id: subscriptions_mail.module,v 1.1.2.8 2008/02/02 01:47:35 chx Exp $s
+
+function subscriptions_mail_cron() {
+  global $user;
+  include_once drupal_get_path('module', 'subscriptions_mail') .'/subscriptions_mail.templates.inc';
+
+  $number_of_mails = variable_get('subscriptions_number_of_mails', 200);
+  $from = variable_get('site_mail', ini_get('sendmail_from'));
+  $old_uid = 0;
+  $counter = 0;
+  $loaded_objects = array();
+  $users = array();
+  $fields = array();
+  $mails = array();
+  session_save_session(FALSE);
+  while ($counter++ < $number_of_mails) {
+    subscriptions_mail_mail_edit_variables(NULL);  // clear cache
+    $result = db_query_range('SELECT * FROM {subscriptions_queue} WHERE last_sent + send_interval < %d ORDER BY sqid', time(), 0, 1);
+    if ($s = db_fetch_array($result)) {
+      if (!isset($users[$s['uid']])) {
+        $users[$s['uid']] = user_load(array('uid' => $s['uid']));
+      }
+      $saved_user = $user;
+      $user = $users[$s['uid']];
+      if ($user->status && $user->access) {
+        $cids = array();
+        $load_function = $s['load_function'];
+        $index = $load_args = $s['load_args'];
+        if (!isset($loaded_objects[$user->uid][$load_function][$load_args])) {
+          if (is_numeric($load_args)) {
+            $object = $load_function($load_args, $s['sqid'], $s['is_new']);
+          }
+          else {
+            $load_args = unserialize($load_args);
+            $load_args[] = $s['is_new'];
+            $object = call_user_func_array($load_function, $load_args);
+          }
+          if (isset($object)) {
+            $access = module_invoke_all('subscriptions', 'access', $load_function, $load_args, $object);
+            // One FALSE vote is enough to deny. Also, we need a non-empty array.
+            $allow = !empty($access) && array_search(FALSE, $access) === FALSE;
+            $loaded_objects[$user->uid][$load_function][$index] = $allow ? $object : FALSE;
+          }
+        }
+        if ($object = $loaded_objects[$user->uid][$load_function][$index]) {
+          if (!isset($users[$object->uid])) {
+            $users[$object->uid] = user_load(array('uid' => $object->uid));
+          }
+          $user = $users[$object->uid];
+          $module = $s['module'];
+          $ori_field = $field = $s['field'];
           $ori_value = $value = $s['value'];
-          if (!isset($fields[$module])) {
-            $fields[$module] = module_invoke_all('subscriptions', 'fields', $module);
+          if (!isset($fields[$module])) {
+            $fields[$module] = module_invoke_all('subscriptions', 'fields', $module);
           }
           if ($module == 'node' && $field == 'nid' && !empty($object->_is_updated) && user_access('subscribe to content types', $user)) {
             $unlisteds = variable_get('subscriptions_unlisted_content_types', array());
@@ -60,31 +60,31 @@
               $field = 'type';
               $value = $object->type;
             }
-          }
-          $mailvars_function = $fields[$module][$field]['mailvars_function'];
-          $mailkey = 'subscriptions-'. $module .'-'. $field;
-          if (!is_numeric($value)) {
-            $mailkey .= '-'. $value;
-          }
-          $digest = $s['digest'] > 0 || $s['digest'] == -1 && _subscriptions_get_setting('digest', 0) > 0;
-          if ($digest) {
-            if (!$body_template = db_result(db_query("SELECT item_body FROM {subscriptions_mail_edit} WHERE mailkey = '%s'", SUBSCRIPTIONS_DIGEST_MAILKEY .'-item'))) {
-              $body_template = SUBSCRIPTIONS_DEFAULT_DIGEST_ITEM_BODY;
-            }
-          }
-          else {
-            $body_template = variable_get('subscriptions_email_body', SUBSCRIPTIONS_DEFAULT_BODY);
-            $subject_template = variable_get('subscriptions_email_subject', SUBSCRIPTIONS_DEFAULT_SUBJECT);
-          }
-          $base = 'user/'. $s['uid'] .'/';
-          $mailvars = array(
-            '!site' => variable_get('site_name', 'drupal'),
-            '!manage_url' => url($base .'subscriptions', NULL, NULL, TRUE),
-            '!name' => $s['name'],
-            '!subs_type' => $fields[$module][$field]['!subs_type'],
-            '!unsubscribe_url' => url("s/del/$module/$ori_field/$ori_value/". $s['author_uid'] .'/'. $s['uid'] .'/' . md5(drupal_get_private_key() . $module . $ori_field . $ori_value . $s['author_uid'] . $s['uid']), NULL, NULL, TRUE),
-          );
-          $mailvars_function($mailvars, $object, $field, $s);
+          }
+          $mailvars_function = $fields[$module][$field]['mailvars_function'];
+          $mailkey = 'subscriptions-'. $module .'-'. $field;
+          if (!is_numeric($value)) {
+            $mailkey .= '-'. $value;
+          }
+          $digest = $s['digest'] > 0 || $s['digest'] == -1 && _subscriptions_get_setting('digest', 0) > 0;
+          if ($digest) {
+            if (!$body_template = db_result(db_query("SELECT item_body FROM {subscriptions_mail_edit} WHERE mailkey = '%s'", SUBSCRIPTIONS_DIGEST_MAILKEY .'-item'))) {
+              $body_template = SUBSCRIPTIONS_DEFAULT_DIGEST_ITEM_BODY;
+            }
+          }
+          else {
+            $body_template = variable_get('subscriptions_email_body', SUBSCRIPTIONS_DEFAULT_BODY);
+            $subject_template = variable_get('subscriptions_email_subject', SUBSCRIPTIONS_DEFAULT_SUBJECT);
+          }
+          $base = 'user/'. $s['uid'] .'/';
+          $mailvars = array(
+            '!site' => variable_get('site_name', 'drupal'),
+            '!manage_url' => url($base .'subscriptions', NULL, NULL, TRUE),
+            '!name' => $s['name'],
+            '!subs_type' => $fields[$module][$field]['!subs_type'],
+            '!unsubscribe_url' => url("s/del/$module/$ori_field/$ori_value/". $s['author_uid'] .'/'. $s['uid'] .'/' . md5(drupal_get_private_key() . $module . $ori_field . $ori_value . $s['author_uid'] . $s['uid']), NULL, NULL, TRUE),
+          );
+          $mailvars_function($mailvars, $object, $field, $s);
           if ($digest && !empty($object->_comments) && module_exists('subscriptions_content')) {
             static $digest_comment_template;
             if (!$digest_comment_template) {
@@ -92,140 +92,140 @@
               $digest_comment_template = ($digest_comment_template ? $digest_comment_template : SUBSCRIPTIONS_DEFAULT_DIGEST_ITEM_COMMENT);
             }
             $mailvars['!comments'] = _subscriptions_content_format_comments($object, $digest_comment_template, '');            
-          }
-          $body = strtr(subscriptions_mail_template_preprocess($body_template, $mailvars), $mailvars);
-          $subject = strtr(subscriptions_mail_template_preprocess($subject_template, $mailvars), $mailvars);
+          }
+          $body = strtr(subscriptions_mail_template_preprocess($body_template, $mailvars), $mailvars);
+          $subject = strtr(subscriptions_mail_template_preprocess($subject_template, $mailvars), $mailvars);
           if ($digest) {
-            $mails[$s['uid']]['bodies'][] = $body;
-            $mails[$s['uid']]['send'] = array(
-              'name' => $s['name'],
+            $mails[$s['uid']]['bodies'][] = $body;
+            $mails[$s['uid']]['send'] = array(
+              'name' => $s['name'],
               'mail' => $s['mail'],
               'from' => $from,
               '!name' => $mailvars['!name'],
               '!manage_url' => $mailvars['!manage_url'],
-            );
-          }
-          else {
-            subscriptions_mail_mail_edit_variables($mailvars);
-            _subscriptions_mail_send($mailkey, $s['name'], $s['mail'], $subject, $body, $from, $s['uid']);
-          }
-        }
-      }
-      $user = $saved_user;
-      db_query("DELETE FROM {subscriptions_queue} WHERE load_function = '%s' AND load_args = '%s' AND uid = %d", $s['load_function'], $s['load_args'], $s['uid']);
-    }
-    else {
-      // No more subscriptions, terminate loop.
-      break;
-    }
-  }
+            );
+          }
+          else {
+            subscriptions_mail_mail_edit_variables($mailvars);
+            _subscriptions_mail_send($mailkey, $s['name'], $s['mail'], $subject, $body, $from, $s['uid']);
+          }
+        }
+      }
+      $user = $saved_user;
+      db_query("DELETE FROM {subscriptions_queue} WHERE load_function = '%s' AND load_args = '%s' AND uid = %d", $s['load_function'], $s['load_args'], $s['uid']);
+    }
+    else {
+      // No more subscriptions, terminate loop.
+      break;
+    }
+  }
   if ($mails) {
     static $separator;
-    if (!isset($separator)) {
-      $separator = db_result(db_query("SELECT item_body FROM {subscriptions_mail_edit} WHERE mailkey = '%s'", SUBSCRIPTIONS_DIGEST_MAILKEY .'-separator'));
+    if (!isset($separator)) {
+      $separator = db_result(db_query("SELECT item_body FROM {subscriptions_mail_edit} WHERE mailkey = '%s'", SUBSCRIPTIONS_DIGEST_MAILKEY .'-separator'));
       $separator = ($separator ? $separator : SUBSCRIPTIONS_DEFAULT_SEPARATOR);
-    }
-    foreach ($mails as $uid => $user_mails) {
-      $s = $user_mails['send'];
-      if ($templates = db_fetch_object(db_query("SELECT * FROM {mail_edit} WHERE mailkey = '%s'", SUBSCRIPTIONS_DIGEST_MAILKEY))) {
-        $subject_template = $templates->subject;
-        $body_template = $templates->body;
-      } else {
-        $subject_template = SUBSCRIPTIONS_DEFAULT_DIGEST_SUBJECT;
-        $body_template = SUBSCRIPTIONS_DEFAULT_DIGEST_BODY;
+    }
+    foreach ($mails as $uid => $user_mails) {
+      $s = $user_mails['send'];
+      if ($templates = db_fetch_object(db_query("SELECT * FROM {mail_edit} WHERE mailkey = '%s'", SUBSCRIPTIONS_DIGEST_MAILKEY))) {
+        $subject_template = $templates->subject;
+        $body_template = $templates->body;
+      } else {
+        $subject_template = SUBSCRIPTIONS_DEFAULT_DIGEST_SUBJECT;
+        $body_template = SUBSCRIPTIONS_DEFAULT_DIGEST_BODY;
       }
       $mailvars['!bodies'] = implode($separator, $user_mails['bodies']);
       $mailvars['!name'] = $s['!name'];
       $mailvars['!manage_url'] = $s['!manage_url'];
-      $subject = strtr(subscriptions_mail_template_preprocess($subject_template, $mailvars), $mailvars);
-      $body = strtr(subscriptions_mail_template_preprocess($body_template, $mailvars), $mailvars);
-      subscriptions_mail_mail_edit_variables($mailvars);
-      _subscriptions_mail_send(SUBSCRIPTIONS_DIGEST_MAILKEY, $s['name'], $s['mail'], $subject, $body, $s['from'], $uid);
-    }
-  }
-}
-
-function _subscriptions_mail_send($mailkey, $name, $to, $subject, $body, $from, $uid) {
-  $mail_success = drupal_mail($mailkey, $to, $subject, $body, $from);
-  $watchdog_params = array('@name' => $name, '@to' => "<$to>");
-  if ($mail_success) {
-    if (variable_get('subscriptions_watchgood', 1)) {
-      watchdog('subscriptions', t('notification for @name at @to', $watchdog_params));
-    }
-    db_query("UPDATE {subscriptions_user} SET last_sent = %d WHERE uid = %d", time(), $uid);
-    if (!db_affected_rows()) {
-      @db_query("INSERT INTO {subscriptions_user} (uid, last_sent) VALUES(%d, %d)", $uid, time());
-    }
-  }
-  else {
-    watchdog('subscriptions', t('error mailing notification for @name at @to', $watchdog_params), WATCHDOG_ERROR);
-  }
-}
-
-function subscriptions_mail_mail_edit_variables(&$variables = NULL, $mailkey = NULL) {
-  static $stored_variables;
+      $subject = strtr(subscriptions_mail_template_preprocess($subject_template, $mailvars), $mailvars);
+      $body = strtr(subscriptions_mail_template_preprocess($body_template, $mailvars), $mailvars);
+      subscriptions_mail_mail_edit_variables($mailvars);
+      _subscriptions_mail_send(SUBSCRIPTIONS_DIGEST_MAILKEY, $s['name'], $s['mail'], $subject, $body, $s['from'], $uid);
+    }
+  }
+}
+
+function _subscriptions_mail_send($mailkey, $name, $to, $subject, $body, $from, $uid) {
+  $mail_success = drupal_mail($mailkey, $to, $subject, $body, $from);
+  $watchdog_params = array('@name' => $name, '@to' => "<$to>");
+  if ($mail_success) {
+    if (variable_get('subscriptions_watchgood', 1)) {
+      watchdog('subscriptions', t('notification for @name at @to', $watchdog_params));
+    }
+    db_query("UPDATE {subscriptions_user} SET last_sent = %d WHERE uid = %d", time(), $uid);
+    if (!db_affected_rows()) {
+      @db_query("INSERT INTO {subscriptions_user} (uid, last_sent) VALUES(%d, %d)", $uid, time());
+    }
+  }
+  else {
+    watchdog('subscriptions', t('error mailing notification for @name at @to', $watchdog_params), WATCHDOG_ERROR);
+  }
+}
+
+function subscriptions_mail_mail_edit_variables(&$variables, $mailkey = NULL) {
+  static $stored_variables;
   if (!isset($variables)) {
     unset($stored_variables);
-  }
-  elseif (isset($mailkey)) {
-    if (substr($mailkey, 0, 13) == 'subscriptions' && !empty($stored_variables)) {
-      $variables = $stored_variables + $variables;
-    }
-  }
-  else {
-    $stored_variables = $variables;
-  }
-}
-
-// TODO: mail_edit.module is hard-coded to call this function,
-//       and we need it, because only mail_edit.module replaces
-//       variables such as !recipient_name.
-//       We ought to be able to get rid of this tight coupling in mail_edit,
-//       but I (hs) don't see yet how this is supposed to work...
-function subscriptions_template_preprocess($template, $mailvars) {
-  return subscriptions_mail_template_preprocess($template, $mailvars);
-}
-
-/**
- * preprocess a mail template(subject or body), detecting conditional clauses
- * that conform to a prescribed syntax
- *
- * @param string $template
- *  the template for preprocessing
- * @param array $mailvars
- *  an associatvie array of currently existing variables that are to be
- *  interpolated into the template later , and which can be used by this
- *  function for preprocessing
- *
- * This function allows the administrator to specify ternary-type conditions
- * to determine what text is used in a mail in a particular situation, using
- * the variables that are currently available for that mail for reference.
- * The syntax is standard PHP/C-style ternary syntax, but only allows the
- * "==" and "!=":
- * {{!variable_name==sometext?text for true condition:text for false condition}}
- *
- * sometext must not contain a question mark, and the true text no colon.
- */
-function subscriptions_mail_template_preprocess($template, $mailvars) {
-  preg_match_all('/{{(?P<condition>[^?]+?)\?(?P<true>[^:]*?):(?P<false>[^\]]*?)}}/', $template, $conditions);
-
-  // locate the actual operators/operand for each
-  $replacement = '';
-  foreach ($conditions[0] as $k => $v) {
-    preg_match('/(?P<operand_1>!.+)\s*(?P<operator>==|!=)\s*(?P<operand_2>.+)/', $conditions['condition'][$k], $matches);
-    $operand1 = (isset($mailvars[$matches['operand_1']]) ? $mailvars[$matches['operand_1']] : $matches['operand_1']);
-    if ($matches['operator'] == '==') {
-      $replacement = ($operand1 == $matches['operand_2']) ? $conditions['true'][$k] : $conditions['false'][$k];
-    }
-    elseif ($matches['operator'] == '!=') {
-      $replacement = ($operand1 != $matches['operand_2']) ? $conditions['true'][$k] : $conditions['false'][$k];
-    }
-    else {
-      continue;
-    }
-    // replace the condition with the result of its evalutation
-    $template = str_replace($v, $replacement, $template);
-  }
-  return $template;
-}
-
+  }
+  elseif (isset($mailkey)) {
+    if (substr($mailkey, 0, 13) == 'subscriptions' && !empty($stored_variables)) {
+      $variables = $stored_variables + $variables;
+    }
+  }
+  else {
+    $stored_variables = $variables;
+  }
+}
+
+// TODO: mail_edit.module is hard-coded to call this function,
+//       and we need it, because only mail_edit.module replaces
+//       variables such as !recipient_name.
+//       We ought to be able to get rid of this tight coupling in mail_edit,
+//       but I (hs) don't see yet how this is supposed to work...
+function subscriptions_template_preprocess($template, $mailvars) {
+  return subscriptions_mail_template_preprocess($template, $mailvars);
+}
+
+/**
+ * preprocess a mail template(subject or body), detecting conditional clauses
+ * that conform to a prescribed syntax
+ *
+ * @param string $template
+ *  the template for preprocessing
+ * @param array $mailvars
+ *  an associatvie array of currently existing variables that are to be
+ *  interpolated into the template later , and which can be used by this
+ *  function for preprocessing
+ *
+ * This function allows the administrator to specify ternary-type conditions
+ * to determine what text is used in a mail in a particular situation, using
+ * the variables that are currently available for that mail for reference.
+ * The syntax is standard PHP/C-style ternary syntax, but only allows the
+ * "==" and "!=":
+ * {{!variable_name==sometext?text for true condition:text for false condition}}
+ *
+ * sometext must not contain a question mark, and the true text no colon.
+ */
+function subscriptions_mail_template_preprocess($template, $mailvars) {
+  preg_match_all('/{{(?P<condition>[^?]+?)\?(?P<true>[^:]*?):(?P<false>[^\]]*?)}}/', $template, $conditions);
+
+  // locate the actual operators/operand for each
+  $replacement = '';
+  foreach ($conditions[0] as $k => $v) {
+    preg_match('/(?P<operand_1>!.+)\s*(?P<operator>==|!=)\s*(?P<operand_2>.+)/', $conditions['condition'][$k], $matches);
+    $operand1 = (isset($mailvars[$matches['operand_1']]) ? $mailvars[$matches['operand_1']] : $matches['operand_1']);
+    if ($matches['operator'] == '==') {
+      $replacement = ($operand1 == $matches['operand_2']) ? $conditions['true'][$k] : $conditions['false'][$k];
+    }
+    elseif ($matches['operator'] == '!=') {
+      $replacement = ($operand1 != $matches['operand_2']) ? $conditions['true'][$k] : $conditions['false'][$k];
+    }
+    else {
+      continue;
+    }
+    // replace the condition with the result of its evalutation
+    $template = str_replace($v, $replacement, $template);
+  }
+  return $template;
+}
+
