PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/adm/admin_megamail.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 506 lines | 382 code | 66 blank | 58 comment | 47 complexity | 5691b0ebd5f1c52acfc6918984fc209d MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Extra credits for this file
  13. * R. U. Serious
  14. *
  15. */
  16. define('IN_ICYPHOENIX', true);
  17. if(!empty($setmodules))
  18. {
  19. $filename = basename(__FILE__);
  20. $module['1100_General']['140_Mega_Mail'] = $filename;
  21. return;
  22. }
  23. if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './../');
  24. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  25. $no_page_header = true;
  26. require('pagestart.' . PHP_EXT);
  27. include_once(IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
  28. // SETTINGS - BEGIN
  29. $def_wait = 10;
  30. $def_size = 100;
  31. define('MEGAMAIL_TABLE', $table_prefix . 'megamail');
  32. define('DAYS_INACTIVE', 180);
  33. // Increase maximum execution time in case of a lot of users, but don't complain about it if it isn't allowed.
  34. @set_time_limit(1200);
  35. // SETTINGS - END
  36. $cancel = isset($_POST['cancel']);
  37. if ($cancel)
  38. {
  39. redirect(ADM . '/' . append_sid('admin_megamail.' . PHP_EXT, true));
  40. }
  41. $modes_array = array('list', 'send', 'delete');
  42. $mode = request_var('mode', $modes_array[0]);
  43. $mode = in_array($mode, $modes_array) ? $mode : $mode_array[0];
  44. $mail_id = request_var('mail_id', 0);
  45. // Delete if needed...
  46. if (($mode == 'delete') && ($mail_id > 0))
  47. {
  48. $confirm = isset($_POST['confirm']);
  49. if($confirm)
  50. {
  51. $sql = "DELETE FROM " . MEGAMAIL_TABLE . "
  52. WHERE mail_id = " . $mail_id;
  53. $result = $db->sql_query($sql);
  54. $message = $lang['megamail_deleted'] . '<br /><br />' . sprintf($lang['megamail_click_return'], '<a href="' . append_sid('admin_megamail.' . PHP_EXT) . '">', '</a>');
  55. message_die(GENERAL_MESSAGE, $message);
  56. }
  57. else
  58. {
  59. include(IP_ROOT_PATH . ADM . '/page_header_admin.' . PHP_EXT);
  60. $template->set_filenames(array('body' => ADM_TPL . 'confirm_body.tpl'));
  61. $hidden_fields = '<input type="hidden" name="mode" value="delete" /><input type="hidden" name="mail_id" value="' . $mail_id . '" />';
  62. $template->assign_vars(array(
  63. 'MESSAGE_TITLE' => $lang['Confirm'],
  64. 'MESSAGE_TEXT' => $lang['megamail_delete_confirm'],
  65. 'L_YES' => $lang['Yes'],
  66. 'L_NO' => $lang['No'],
  67. 'S_CONFIRM_ACTION' => append_sid('admin_megamail.' . PHP_EXT),
  68. 'S_HIDDEN_FIELDS' => $hidden_fields
  69. )
  70. );
  71. $template->pparse('body');
  72. include(IP_ROOT_PATH . ADM . '/page_footer_admin.' . PHP_EXT);
  73. exit;
  74. }
  75. }
  76. $subject = request_post_var('subject', '', true);
  77. $subject = htmlspecialchars_decode($subject, ENT_QUOTES);
  78. $message = request_post_var('message', '', true);
  79. $message = htmlspecialchars_decode($message, ENT_QUOTES);
  80. //$message = $_POST['message'];
  81. // Do the job ...
  82. if (!empty($subject) && !empty($message))
  83. {
  84. $batchsize = request_post_var('batchsize', $def_size);
  85. $batchwait = request_post_var('batchwait', $def_wait);
  86. $mass_pm = request_var('mass_pm', 0);
  87. $email_format = request_var('email_format', 0);
  88. $group_id = request_var(POST_GROUPS_URL, 0);
  89. $mail_session_id = md5(uniqid(''));
  90. $sql = "INSERT INTO " . MEGAMAIL_TABLE . " (mailsession_id, mass_pm, user_id, group_id, email_subject, email_body, email_format, batch_start, batch_size, batch_wait, status)
  91. VALUES ('" . $mail_session_id . "', " . $mass_pm . ", " . $user->data['user_id'] . ", " . $group_id . ", '" . $db->sql_escape($subject) . "', '" . $db->sql_escape($message) . "', " . $email_format . ", 0, " . $batchsize . "," . $batchwait . ", 0)";
  92. $result = $db->sql_query($sql);
  93. $mail_id = $db->sql_nextid();
  94. $url = append_sid('admin_megamail.' . PHP_EXT . '?mail_id=' . $mail_id . '&amp;mail_session_id=' . $mail_session_id);
  95. $redirect_url = ADM . '/' . $url;
  96. meta_refresh($batchwait, $redirect_url);
  97. $message = sprintf($lang['megamail_created_message'], '<a href="' . $url . '">', '</a>');
  98. message_die(GENERAL_MESSAGE, $message);
  99. }
  100. $mail_id = request_get_var('mail_id', 0);
  101. $mail_session_id = request_get_var('mail_session_id', '');
  102. if (!empty($mail_id) && !empty($mail_session_id))
  103. {
  104. @ignore_user_abort(true);
  105. // Let's see if that session exists
  106. $sql = "SELECT *
  107. FROM " . MEGAMAIL_TABLE . "
  108. WHERE mail_id = '" . $mail_id . "'
  109. AND mailsession_id LIKE '" . $db->sql_escape($mail_session_id) . "'";
  110. $result = $db->sql_query($sql);
  111. $mail_data = $db->sql_fetchrow($result);
  112. if (!($mail_data))
  113. {
  114. message_die(GENERAL_MESSAGE, 'Mail ID and Mail Session ID do not match.', '', __LINE__, __FILE__, $sql);
  115. }
  116. //Ok, the session exists
  117. $subject = $mail_data['email_subject'];
  118. $message = $mail_data['email_body'];
  119. // Store the clean version of the message for PM
  120. $pm_message = $message;
  121. $group_id = $mail_data['group_id'];
  122. $mass_pm = $mail_data['mass_pm'];
  123. $email_format = $mail_data['email_format'];
  124. if ($email_format == 1)
  125. {
  126. $config['html_email'] = 1;
  127. $bbcode->allow_html = false;
  128. $bbcode->allow_bbcode = true;
  129. $bbcode->allow_smilies = true;
  130. $message = $bbcode->parse($message);
  131. }
  132. elseif ($email_format == 2)
  133. {
  134. // We are in FULL HTML here
  135. $config['html_email'] = 1;
  136. }
  137. //OLD HTML FORMAT
  138. /*
  139. if ($config['html_email'] == false)
  140. {
  141. $message = $bbcode->bbcode_killer($message, '');
  142. $message = strip_tags($mail_data['email_body'], '');
  143. }
  144. else
  145. {
  146. $bbcode->allow_html = true;
  147. $bbcode->allow_bbcode = ($config['allow_bbcode'] ? $config['allow_bbcode'] : false);
  148. $bbcode->allow_smilies = ($config['allow_smilies'] ? $config['allow_smilies'] : false);
  149. $message = $bbcode->parse($message);
  150. }
  151. */
  152. $sql_non_recent_login = '';
  153. $process_groups = (($group_id == -1) || ($group_id == -2)) ? false : true;
  154. if ($group_id == -2)
  155. {
  156. $sql_non_recent_login = "AND u.user_lastvisit < '" . (time() - (86400 * DAYS_INACTIVE)) . "'";
  157. }
  158. //Now, let's see if we reached the upperlimit, if yes adjust the batch_size
  159. if ($process_groups)
  160. {
  161. $sql = "SELECT COUNT(u.user_email)
  162. FROM " . USERS_TABLE . " u, " . USER_GROUP_TABLE . " ug
  163. WHERE ug.group_id = '" . $group_id . "'
  164. AND ug.user_pending <> " . TRUE . "
  165. AND u.user_id = ug.user_id
  166. AND u.user_active = 1
  167. AND u.user_allow_mass_email = 1";
  168. }
  169. else
  170. {
  171. $sql = "SELECT COUNT(u.user_email)
  172. FROM " . USERS_TABLE . " u
  173. WHERE u.user_active = 1
  174. AND u.user_allow_mass_email = 1
  175. " . $sql_non_recent_login;
  176. }
  177. $result = $db->sql_query($sql);
  178. $totalrecipients = $db->sql_fetchrow($result);
  179. $totalrecipients = $totalrecipients['COUNT(u.user_email)'];
  180. $is_done = '';
  181. /*
  182. // Forcing email max to $force_limit users
  183. $force_limit = 10000;
  184. $force_start = 10000;
  185. $totalrecipients = $force_limit;
  186. $mail_data['batch_start'] = ($mail_data['batch_start'] < $force_start) ? $force_start : $mail_data['batch_start'];
  187. */
  188. if (($mail_data['batch_start'] + $mail_data['batch_size']) > $totalrecipients)
  189. {
  190. $mail_data['batch_size'] = $totalrecipients - $mail_data['batch_start'];
  191. $is_done = ', status = 1';
  192. }
  193. // Create new mail session
  194. $mail_session_id = md5(uniqid(''));
  195. $sql = "UPDATE " . MEGAMAIL_TABLE . "
  196. SET mailsession_id = '" . $db->sql_escape($mail_session_id) . "', batch_start= " . ($mail_data['batch_start'] + $mail_data['batch_size']) . $is_done . "
  197. WHERE mail_id = '" . $mail_id . "'";
  198. $result = $db->sql_query($sql);
  199. // OK, now let's start sending
  200. $error = false;
  201. $error_msg = '';
  202. if ($process_groups)
  203. {
  204. $sql = "SELECT u.user_id, u.user_email
  205. FROM " . USERS_TABLE . " u, " . USER_GROUP_TABLE . " ug
  206. WHERE ug.group_id = '" . $group_id . "'
  207. AND ug.user_pending <> " . TRUE . "
  208. AND u.user_id = ug.user_id
  209. AND u.user_active = 1
  210. AND u.user_allow_mass_email = 1";
  211. }
  212. else
  213. {
  214. $sql = "SELECT user_id, user_email
  215. FROM " . USERS_TABLE . " u
  216. WHERE u.user_active = 1
  217. AND u.user_allow_mass_email = 1
  218. " . $sql_non_recent_login;
  219. }
  220. $sql .= " LIMIT " . $mail_data['batch_start'] . ", " . $mail_data['batch_size'];
  221. $result = $db->sql_query($sql);
  222. if ($row = $db->sql_fetchrow($result))
  223. {
  224. if ($mass_pm)
  225. {
  226. include_once(IP_ROOT_PATH . 'includes/class_pm.' . PHP_EXT);
  227. $privmsg = new class_pm();
  228. }
  229. $bcc_list_array = array();
  230. $bcc_list = '';
  231. do
  232. {
  233. if ($mass_pm)
  234. {
  235. $privmsg->send($user->data['user_id'], $row['user_id'], $subject, $pm_message);
  236. }
  237. $bcc_list .= (($bcc_list != '') ? ', ' : '') . $row['user_email'];
  238. $bcc_list_array[] = $row['user_email'];
  239. }
  240. while ($row = $db->sql_fetchrow($result));
  241. $db->sql_freeresult($result);
  242. if ($mass_pm)
  243. {
  244. unset($privmsg);
  245. }
  246. }
  247. else
  248. {
  249. $message = ($process_groups ? $lang['Group_not_exist'] : $lang['NO_USER']);
  250. $error = true;
  251. $error_msg .= (!empty($error_msg)) ? '<br />' . $message : $message;
  252. }
  253. if (!$error)
  254. {
  255. include(IP_ROOT_PATH . 'includes/emailer.' . PHP_EXT);
  256. // Let's do some checking to make sure that mass mail functions are working in win32 versions of php.
  257. if (preg_match('/[c-z]:\\\.*/i', getenv('PATH')) && !$config['smtp_delivery'])
  258. {
  259. $ini_val = (@phpversion() >= '4.0.0') ? 'ini_get' : 'get_cfg_var';
  260. // We are running on windows, force delivery to use our smtp functions
  261. // since php's are broken by default
  262. $config['smtp_delivery'] = 1;
  263. $config['smtp_host'] = @$ini_val('SMTP');
  264. }
  265. $emailer = new emailer();
  266. $emailer->headers('X-AntiAbuse: Board servername - ' . trim($config['server_name']));
  267. $emailer->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']);
  268. $emailer->headers('X-AntiAbuse: Username - ' . $user->data['username']);
  269. $emailer->headers('X-AntiAbuse: User IP - ' . $user_ip);
  270. if ($email_format == 2)
  271. {
  272. $emailer->use_template('empty_email', $config['default_lang'], true);
  273. }
  274. else
  275. {
  276. $emailer->use_template('admin_send_email', $config['default_lang']);
  277. }
  278. foreach ($bcc_list_array as $bcc_address)
  279. {
  280. if (!empty($bcc_address))
  281. {
  282. $emailer->bcc($bcc_address);
  283. }
  284. }
  285. $emailer->set_subject($subject);
  286. // Do we want to force line breaks? It is HTML, so we should not replace line breaks...
  287. //$message = preg_replace(array("/<br \/>\r\n/", "/<br>\r\n/", "/(\r\n|\n|\r)/"), array("\r\n", "\r\n", "<br />\r\n"), $message);
  288. if ($mass_pm)
  289. {
  290. $server_url = create_server_url();
  291. $pm_inbox_link = $server_url . CMS_PAGE_PRIVMSG . '?folder=inbox';
  292. $pm_inbox_link = (!$config['html_email']) ? $pm_inbox_link : ('<a href="' . $pm_inbox_link . '">' . $pm_inbox_link . '</a>');
  293. $message = str_replace(array('{SITENAME}', '{U_INBOX}'), array($config['sitename'], $pm_inbox_link), $lang['PM_NOTIFICATION']);
  294. $message = (!$config['html_email']) ? str_replace('<br />', "\r\n", $message) : $message;
  295. }
  296. $emailer->assign_vars(array(
  297. 'SITENAME' => $config['sitename'],
  298. 'BOARD_EMAIL' => $config['board_email'],
  299. 'MESSAGE' => $message
  300. )
  301. );
  302. $emailer->send();
  303. $emailer->reset();
  304. if ($is_done == '')
  305. {
  306. $url= append_sid('admin_megamail.' . PHP_EXT . '?mail_id=' . $mail_id . '&amp;mail_session_id=' . $mail_session_id);
  307. $redirect_url = ADM . '/' . $url;
  308. meta_refresh($mail_data['batch_wait'], $redirect_url);
  309. $message = sprintf($lang['megamail_send_message'] ,$mail_data['batch_start'], ($mail_data['batch_start']+$mail_data['batch_size']), '<a href="' . $url . '">', '</a>');
  310. }
  311. else
  312. {
  313. $url= append_sid('admin_megamail.' . PHP_EXT);
  314. $redirect_url = ADM . '/' . $url;
  315. meta_refresh($mail_data['batch_wait'], $redirect_url);
  316. $message = $lang['megamail_done'] . '<br />' . sprintf($lang['megamail_proceed'], '<a href="' . $url . '">', '</a>');
  317. }
  318. message_die(GENERAL_MESSAGE, $message);
  319. // message_die(GENERAL_MESSAGE, $lang['Email_sent'] . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid('index.' . PHP_EXT . '?pane=right') . '">', '</a>'));
  320. }
  321. }
  322. if ($error)
  323. {
  324. $template->set_filenames(array('reg_header' => 'error_body.tpl'));
  325. $template->assign_vars(array(
  326. 'ERROR_MESSAGE' => $error_msg
  327. )
  328. );
  329. $template->assign_var_from_handle('ERROR_BOX', 'reg_header');
  330. }
  331. // Initial selection
  332. $sql = "SELECT m.*, u.username, u.user_active, u.user_color, g.group_name
  333. FROM " . MEGAMAIL_TABLE . " m
  334. LEFT JOIN " . USERS_TABLE . " u ON (m.user_id = u.user_id)
  335. LEFT JOIN " . GROUPS_TABLE . " g ON (m.group_id = g.group_id)
  336. ORDER BY m.mail_id ASC";
  337. $result = $db->sql_query($sql);
  338. $row_class = 0;
  339. if ($mail_data = $db->sql_fetchrow($result))
  340. {
  341. do
  342. {
  343. $url = append_sid('admin_megamail.' . PHP_EXT . '?mail_id=' . $mail_data['mail_id'] . '&amp;mail_session_id=' . $mail_data['mailsession_id']);
  344. $look_up_array = array(
  345. '\"',
  346. '"',
  347. "<",
  348. ">",
  349. "\n",
  350. chr(13),
  351. );
  352. $replacement_array = array(
  353. '&q_mg;',
  354. '\"',
  355. "&lt_mg;",
  356. "&gt_mg;",
  357. "\\n",
  358. "",
  359. );
  360. $plain_message = $mail_data['email_body'];
  361. $plain_message = strtr($plain_message, array_flip(get_html_translation_table(HTML_ENTITIES)));
  362. $plain_message = str_replace($look_up_array, $replacement_array, $plain_message);
  363. $delete_url = append_sid('admin_megamail.' . PHP_EXT . '?mail_id=' . $mail_data['mail_id'] . '&amp;mode=delete');
  364. $template->assign_block_vars('mail_sessions',array(
  365. 'ROW' => ($row_class % 2) ? 'row2' : 'row1',
  366. 'ID' => $mail_data['mail_id'],
  367. 'GROUP' => ($mail_data['group_id'] != -1) ? $mail_data['group_name'] : $lang['All_users'],
  368. 'SUBJECT' => $mail_data['email_subject'],
  369. 'MASS_PM' => $mail_data['mass_pm'] ? $lang['Yes'] : $lang['No'],
  370. 'EMAIL_FORMAT' => (($mail_data['email_format'] == 2) ? $lang['FULL_HTML'] : (($mail_data['email_format'] == 1) ? $lang['BBCode'] : $lang['HTML'])),
  371. 'MESSAGE_BODY' => $plain_message,
  372. 'BATCHSTART' => $mail_data['batch_start'],
  373. 'BATCHSIZE' => $mail_data['batch_size'],
  374. 'BATCHWAIT' => $mail_data['batch_wait'] . ' s.',
  375. 'SENDER' => colorize_username($mail_data['user_id'], $mail_data['username'], $mail_data['user_color'], $mail_data['user_active']),
  376. 'STATUS' => ($mail_data['status'] == 0) ? sprintf($lang['megamail_proceed'], '<a href="' . $url . '">', '</a>') : 'Done',
  377. 'U_DELETE' => $delete_url,
  378. )
  379. );
  380. $row_class++;
  381. }
  382. while($mail_data = $db->sql_fetchrow($result));
  383. }
  384. else
  385. {
  386. $template->assign_block_vars('switch_no_sessions',array(
  387. 'EMPTY' => $lang['megamail_none'],
  388. )
  389. );
  390. }
  391. $sql = "SELECT group_id, group_name
  392. FROM " . GROUPS_TABLE . "
  393. WHERE group_single_user <> 1";
  394. $result = $db->sql_query($sql);
  395. $select_list = '';
  396. $select_list .= '<select name = "' . POST_GROUPS_URL . '">';
  397. $select_list .= '<option value = "-1">' . $lang['All_users'] . '</option>';
  398. $select_list .= '<option value = "-2">' . str_replace('{DAYS}', DAYS_INACTIVE, $lang['megamail_inactive_users']) . '</option>';
  399. if ($row = $db->sql_fetchrow($result))
  400. {
  401. do
  402. {
  403. $select_list .= '<option value = "' . $row['group_id'] . '">' . $row['group_name'] . '</option>';
  404. }
  405. while ($row = $db->sql_fetchrow($result));
  406. }
  407. $select_list .= '</select>';
  408. // Generate page
  409. include(IP_ROOT_PATH . ADM . '/page_header_admin.' . PHP_EXT);
  410. $template->set_filenames(array('body' => ADM_TPL . 'megamail.tpl'));
  411. $template->assign_vars(array(
  412. 'MESSAGE' => $message,
  413. 'SUBJECT' => $subject,
  414. 'L_EMAIL_TITLE' => $lang['140_Mega_Mail'],
  415. 'L_EMAIL_EXPLAIN' => $lang['Megamail_Explain'],
  416. 'L_COMPOSE' => $lang['Compose'],
  417. 'L_RECIPIENTS' => $lang['Recipients'],
  418. 'L_EMAIL_SUBJECT' => $lang['Subject'],
  419. 'L_EMAIL_MSG' => $lang['Message'],
  420. 'L_EMAIL' => $lang['Email'],
  421. 'L_SEND' => $lang['Send'],
  422. 'L_NOTICE' => $notice,
  423. 'S_USER_ACTION' => append_sid('admin_megamail.' . PHP_EXT),
  424. 'S_GROUP_SELECT' => $select_list,
  425. 'L_MAIL_SESSION_HEADER' => $lang['megamail_header'],
  426. 'L_ID' => 'ID',
  427. 'L_GROUP' => $lang['group_name'],
  428. 'L_BATCH_START' => $lang['megamail_batchstart'],
  429. 'L_BATCH_SIZE' => $lang['megamail_batchsize'],
  430. 'L_BATCH_WAIT' => $lang['megamail_batchwait'],
  431. //'L_SENDER' => $lang['Auth_Admin'],
  432. 'L_BBCODE' => $lang['BBCode'],
  433. 'L_STATUS' => $lang['megamail_status'],
  434. 'DEFAULT_SIZE' => $def_size,
  435. 'DEFAULT_WAIT' => $def_wait,
  436. )
  437. );
  438. $template->pparse('body');
  439. include(IP_ROOT_PATH . ADM . '/page_footer_admin.' . PHP_EXT);
  440. ?>