PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/e107_plugins/calendar_menu/subs_menu.php

https://github.com/e107/e107
PHP | 250 lines | 160 code | 37 blank | 53 comment | 64 complexity | 36e129ddf49ce3971a62bc4b97d01323 MD5 | raw file
  1. <?php
  2. /*
  3. + ----------------------------------------------------------------------------+
  4. | e107 website system
  5. |
  6. | Copyright (C) 2001-2002 Steve Dunstan (jalist@e107.org)
  7. | Copyright (C) 2008-2010 e107 Inc (e107.org)
  8. |
  9. | Released under the terms and conditions of the
  10. | GNU General Public License (http://gnu.org).
  11. |
  12. | $URL: https://e107.svn.sourceforge.net/svnroot/e107/trunk/e107_0.7/e107_plugins/calendar_menu/subs_menu.php $
  13. | $Revision: 11678 $
  14. | $Id: subs_menu.php 11678 2010-08-22 00:43:45Z e107coders $
  15. | $Author: e107coders $
  16. +----------------------------------------------------------------------------+
  17. | 09.07.06 - Mods by steved:
  18. | General restructuring to use common routines
  19. | Support for sending emails on previous day.
  20. | Logging capability
  21. | Debugging option
  22. |
  23. | 11.07.06 - Adjustment to logging messages
  24. | 12.07.06 - More adjustment to logging messages
  25. | 15.07.06 - Adjustment to 'tomorrow' query
  26. | 17.07.06 - More adjustment to 'tomorrow' query
  27. |
  28. | 04.10.06 - Mods to mailout to allow mix of voluntary and forced subs to the same event
  29. | 24.10.06 - Change DB names so works as a menu
  30. | 25.10.06 - Logging selectively disabled when run as menu
  31. | 27.10.06 - Update queries to new structure, don't email banned users
  32. | 31.10.06 - Attempt to optimise query better
  33. | 01.11.06 - More refinements on query
  34. | 05.11.06 - More refinement on query - ignores midnight at end of day. **** BANG ****
  35. |
  36. +----------------------------------------------------------------------------+
  37. */
  38. if (!defined('e107_INIT')) { exit; }
  39. // This menu can be called from a cron job - see readme.rtf
  40. $run_from_menu = function_exists("parseheader"); // Use this to suppress logging in 'through' path
  41. $ec_dir = e_PLUGIN . "calendar_menu/";
  42. // Check if we are going to do the notify
  43. $debug_level = 0; // Set to 1 or 2 to suppress actual sending of emails
  44. if (($debug_level > 0) && e_QUERY)
  45. { // Run with query of ?dd-mm[-yy] to test specific date
  46. list($day,$month,$year) = explode("-",e_QUERY);
  47. if (!isset($year) || ($year == 0)) $year = date("Y");
  48. $cal_starttime = mktime(0,0,0,$month,$day,$year);
  49. echo "Debug run for {$day}-{$month}-{$year}<br />";
  50. }
  51. else
  52. { // Normal operation
  53. $cal_starttime = mktime(0, 0, 0, date("n"), date("d"), date("Y"));
  54. }
  55. $log_requirement = 0; // Logging required 0=none, 1=summary, 2=detailed
  56. if (isset($pref['eventpost_emaillog'])) $log_requirement = $pref['eventpost_emaillog'];
  57. if ($debug_level >= 2) $log_requirement = 2; // Force full logging if debug
  58. if ($log_requirement > 0)
  59. {
  60. $log_filename = $ec_dir.'log/calendar_mail.txt';
  61. if (!$run_from_menu)
  62. {
  63. if (!($handle = fopen($log_filename, 'a'))) $log_requirement = 0;
  64. if (fwrite($handle,"\r\n\r\nMail subscriptions run started at ".date("D j M Y G:i:s")) === false) $log_requirement = 0;
  65. fclose($handle);
  66. }
  67. }
  68. // Start with the 'in advance' emails
  69. $cal_args = "select * from #event left join #event_cat on event_category=event_cat_id where (event_cat_subs>0 OR event_cat_force_class != '') and
  70. event_cat_last < " . intval($cal_starttime) . " and
  71. event_cat_ahead > 0 and
  72. event_start >= (" . intval($cal_starttime) . "+(86400*(event_cat_ahead))) and
  73. event_start < (" . intval($cal_starttime) . "+(86400*(event_cat_ahead+1))) and
  74. find_in_set(event_cat_notify,'1,3,5,7')";
  75. send_mailshot($cal_args, 'Advance',1);
  76. // then for today
  77. //$cal_starttime = mktime(0, 0, 0, date("n"), date("d"), date("Y"));
  78. $cal_args = "select * from #event left join #event_cat on event_category=event_cat_id where (event_cat_subs>0 OR event_cat_force_class != '') and
  79. event_cat_today < " . intval($cal_starttime) . " and
  80. event_start >= (" . intval($cal_starttime) . ") and
  81. event_start < (86400+" . intval($cal_starttime) . ") and
  82. find_in_set(event_cat_notify,'2,3,6,7')";
  83. send_mailshot($cal_args, 'today',2);
  84. // Finally do 'day before' emails
  85. $cal_args = "select * from #event left join #event_cat on event_category=event_cat_id where (event_cat_subs>0 OR event_cat_force_class != '') and
  86. event_cat_today < " . intval($cal_starttime) . " and
  87. event_start >= (" . intval($cal_starttime) ." + 86400 ) and
  88. event_start < (" . intval($cal_starttime) ." + 172800) and
  89. find_in_set(event_cat_notify,'4,5,6,7')";
  90. send_mailshot($cal_args, 'tomorrow',2);
  91. if (($log_requirement > 0) && (!$run_from_menu))
  92. {
  93. if (!($handle = fopen($log_filename, 'a'))) $log_requirement = 0;
  94. if (fwrite($handle," .. completed at ".date("D j M Y G:i:s")."\r\n") === false) $log_requirement = 0;
  95. fclose($handle);
  96. }
  97. // Done
  98. /*
  99. Function to actually send a mailshot
  100. */
  101. function send_mailshot($cal_query, $shot_type, $msg_num)
  102. {
  103. global $sql, $sql2;
  104. global $log_requirement, $log_filename, $debug_level;
  105. global $pref;
  106. global $run_from_menu;
  107. if (($log_requirement > 1) && (!$run_from_menu))
  108. {
  109. if (!$handle = fopen($log_filename, 'a')) $log_requirement = 0;
  110. if (fwrite($handle,"\r\n Starting emails for ".$shot_type." at ".date("D j M Y G:i:s")) === false) $log_requirement = 0;
  111. if ($debug_level >= 2)
  112. {
  113. if (fwrite($handle,"\r\n Query is: ".$cal_query."\r\n") === false) $log_requirement = 0;
  114. }
  115. }
  116. if ($num_cat_proc = $sql->db_Select_gen($cal_query))
  117. { // Got at least one event to process here
  118. if ($log_requirement > 1)
  119. {
  120. if ($run_from_menu) if (!($handle = fopen($log_filename, 'a'))) $log_requirement = 0;
  121. if (fwrite($handle," - ".$num_cat_proc." categories found to process\r\n") === false) $log_requirement = 0;
  122. }
  123. require_once(e_HANDLER . "mail.php");
  124. while ($cal_row = $sql->db_Fetch())
  125. { // Process one event at a time
  126. extract($cal_row);
  127. if ($log_requirement > 1)
  128. {
  129. if (fwrite($handle," Processing event: ".$event_title." \r\n") === false) $log_requirement = 0;
  130. }
  131. if ($msg_num == 1)
  132. $sql2->db_Update("event_cat", "event_cat_last=" . time() . " where event_cat_id=" . intval($event_cat_id));
  133. else
  134. $sql2->db_Update("event_cat", "event_cat_today=" . time() . " where event_cat_id=" . intval($event_cat_id));
  135. // Start of next try on query
  136. // Four cases for the query:
  137. // 1. No forced mailshots - based on event_subs table only Need INNER JOIN
  138. // 2. Forced mailshot to members - send to all users (don't care about subscriptions) Don't need JOIN
  139. // 3. Forced mailshot to group of members - based on user table only Don't need JOIN
  140. // 4. Forced mailshot to group, plus optional subscriptions - use the lot! Need LEFT JOIN
  141. // (Always block sent to banned members)
  142. $manual_subs = (isset($pref['eventpost_asubs']) && ($pref['eventpost_asubs'] == '1'));
  143. $subs_fields = '';
  144. $subs_join = '';
  145. $where_clause = '';
  146. $group_clause = '';
  147. if ($event_cat_force_class != e_UC_MEMBER)
  148. { // Cases 1, 3, 4 (basic query does for case 2)
  149. if ((!$event_cat_force_class) || ($manual_subs))
  150. { // Cases 1 & 4 - need to join with event_subs database
  151. $subs_fields = ", es.* ";
  152. if ($event_cat_force_class) $subs_join = "LEFT"; else $subs_join = "INNER";
  153. $subs_join .= " join #event_subs AS es on u.user_id=es.event_userid ";
  154. $where_clause = " es.event_cat='".intval($event_category)."' ";
  155. $group_clause = " GROUP BY u.user_id";
  156. }
  157. if ($event_cat_force_class)
  158. { // cases 3 and 4 - ... and check for involuntary subscribers
  159. if ($where_clause) $where_clause .= " OR ";
  160. if ($event_cat_force_class == e_UC_ADMIN)
  161. {
  162. $where_clause .= "(u.user_admin = '1' )";
  163. }
  164. else
  165. {
  166. $where_clause .= "find_in_set('".intval($event_cat_force_class)."', u.user_class)";
  167. }
  168. }
  169. if ($where_clause) $where_clause = ' AND ('.$where_clause.' ) ';
  170. } // End of cases 1, 3, 4
  171. $cal_emilargs = "SELECT u.user_id, u.user_class, u.user_email, u.user_name, u.user_ban, u.user_admin{$subs_fields}
  172. from #user AS u {$subs_join}
  173. WHERE u.user_ban = '0' {$where_clause} {$group_clause}";
  174. if ($debug_level >= 2)
  175. {
  176. if (fwrite($handle,"\r\n Email selection query is: ".$cal_emilargs."\r\n") === false) $log_requirement = 0;
  177. }
  178. if ($num_shots = $sql2->db_Select_gen($cal_emilargs))
  179. {
  180. if ($log_requirement > 1)
  181. {
  182. if (fwrite($handle," - ".$num_shots." emails found to send\r\n") === false) $log_requirement = 0;
  183. }
  184. while ($cal_emrow = $sql2->db_Fetch())
  185. {
  186. extract($cal_emrow);
  187. if ($msg_num == 1)
  188. $cal_msg = $event_title . "\n\n" . $event_cat_msg1;
  189. else
  190. $cal_msg = $event_title . "\n\n" . $event_cat_msg2;
  191. if ($debug_level == 0) $send_result = sendemail($user_email, $pref['eventpost_mailsubject'], $cal_msg, $user_name, $pref['eventpost_mailaddress'], $pref['eventpost_mailfrom']);
  192. if ($log_requirement > 1)
  193. {
  194. $log_string = " Send to: ".$user_email." Name: ".$user_name;
  195. if ($debug_level > 0)
  196. { $log_string .= " *DEBUG*
  197. "; }
  198. else
  199. { $log_string .= " Result = ".$send_result."
  200. "; }
  201. if (fwrite($handle,$log_string) === false) $log_requirement = 0;
  202. }
  203. }
  204. }
  205. } // while
  206. if ($log_requirement > 1)
  207. {
  208. if (fwrite($handle," Completed emails for ".$shot_type." at ".date("D j M Y G:i:s")."\r\n") === false) $log_requirement = 0;
  209. fclose($handle);
  210. }
  211. }
  212. }
  213. ?>