PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/system/expressionengine/modules/mailinglist/mod.mailinglist.php

https://bitbucket.org/mbaily/tremain
PHP | 532 lines | 299 code | 121 blank | 112 comment | 55 complexity | e21ae90c394a808d3bc36324fb2a2583 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2013, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Blacklist Module
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Modules
  19. * @category Update File
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Mailinglist {
  24. var $email_confirm = TRUE; // TRUE/FALSE - whether to send an email confirmation when users sign up
  25. var $return_data = '';
  26. /**
  27. * Constructor
  28. */
  29. function Mailinglist()
  30. {
  31. $this->EE =& get_instance();
  32. }
  33. // -------------------------------------------------------------------------
  34. /**
  35. * Mailing List Submission Form
  36. */
  37. function form()
  38. {
  39. $tagdata = ee()->TMPL->tagdata;
  40. $list = (ee()->TMPL->fetch_param('list') === FALSE) ? '0' : ee()->TMPL->fetch_param('list');
  41. $name = '';
  42. if ($list !== FALSE)
  43. {
  44. if (preg_match("/full_name/", $tagdata))
  45. {
  46. $query = ee()->db->query("SELECT list_title FROM exp_mailing_lists WHERE list_name ='".ee()->db->escape_str($list)."'");
  47. if ($query->num_rows() == 1)
  48. {
  49. $name = $query->row('list_title') ;
  50. }
  51. }
  52. }
  53. $tagdata = str_replace(LD.'full_name'.RD, $name, $tagdata);
  54. if (ee()->session->userdata('email') != '')
  55. {
  56. $tagdata = str_replace(LD.'email'.RD, ee()->session->userdata('email'), $tagdata);
  57. }
  58. else
  59. {
  60. $tagdata = str_replace(LD.'email'.RD, '', $tagdata);
  61. }
  62. /** ----------------------------------------
  63. /** Create form
  64. /** ----------------------------------------*/
  65. if (ee()->TMPL->fetch_param('name') !== FALSE &&
  66. preg_match("#^[a-zA-Z0-9_\-]+$#i", ee()->TMPL->fetch_param('name'), $match))
  67. {
  68. $data['name'] = ee()->TMPL->fetch_param('name');
  69. }
  70. $data['id'] = (ee()->TMPL->form_id == '') ? 'mailinglist_form' : ee()->TMPL->form_id;
  71. $data['class'] = ee()->TMPL->form_class;
  72. $data['hidden_fields'] = array(
  73. 'ACT' => ee()->functions->fetch_action_id('Mailinglist', 'insert_new_email'),
  74. 'RET' => ee()->functions->fetch_current_uri(),
  75. 'list' => $list
  76. );
  77. $res = ee()->functions->form_declaration($data);
  78. $res .= $tagdata;
  79. $res .= "</form>";
  80. return $res;
  81. }
  82. // -------------------------------------------------------------------------
  83. /** ----------------------------------------
  84. /** Insert new email
  85. /** ----------------------------------------*/
  86. function insert_new_email()
  87. {
  88. /** ----------------------------------------
  89. /** Fetch the mailinglist language pack
  90. /** ----------------------------------------*/
  91. ee()->lang->loadfile('mailinglist');
  92. // Is the mailing list turned on?
  93. if (ee()->config->item('mailinglist_enabled') == 'n')
  94. {
  95. return ee()->output->show_user_error('general', lang('mailinglist_disabled'));
  96. }
  97. /** ----------------------------------------
  98. /** Blacklist/Whitelist Check
  99. /** ----------------------------------------*/
  100. if (ee()->blacklist->blacklisted == 'y' && ee()->blacklist->whitelisted == 'n')
  101. {
  102. return ee()->output->show_user_error('general', lang('not_authorized'));
  103. }
  104. if ( ! isset($_POST['RET']))
  105. {
  106. exit;
  107. }
  108. /** ----------------------------------------
  109. /** Error trapping
  110. /** ----------------------------------------*/
  111. $errors = array();
  112. $email = ee()->input->get_post('email');
  113. $email = trim(strip_tags($email));
  114. $list = ee()->input->post('list');
  115. $list_id = FALSE;
  116. if ($email == '')
  117. {
  118. $errors[] = lang('ml_missing_email');
  119. }
  120. ee()->load->helper('email');
  121. if ( ! valid_email($email))
  122. {
  123. $errors[] = lang('ml_invalid_email');
  124. }
  125. if (count($errors) == 0)
  126. {
  127. // Secure Forms check - do it early due to amount of further data manipulation before insert
  128. if (ee()->security->check_xid(ee()->input->post('XID')) == FALSE)
  129. {
  130. ee()->functions->redirect(stripslashes(ee()->input->post('RET')));
  131. }
  132. /** ----------------------------------------
  133. /** Which list is being subscribed to?
  134. /** ----------------------------------------*/
  135. // If there is no list ID we'll have to figure it out.
  136. if ($list == '0')
  137. {
  138. $query = ee()->db->query("SELECT COUNT(*) AS count FROM exp_mailing_lists WHERE list_id = 1");
  139. if ($query->row('count') != 1)
  140. {
  141. $errors[] = lang('ml_no_list_id');
  142. }
  143. else
  144. {
  145. $list_id = 1;
  146. }
  147. }
  148. else
  149. {
  150. $query = ee()->db->query("SELECT list_id FROM exp_mailing_lists WHERE list_name = '".ee()->db->escape_str($list)."'");
  151. if ($query->num_rows() != 1)
  152. {
  153. $errors[] = lang('ml_no_list_id');
  154. }
  155. else
  156. {
  157. $list_id = $query->row('list_id') ;
  158. }
  159. }
  160. // Kill duplicate emails from authorization queue. This prevents an error if a user
  161. // signs up but never activates their email, then signs up again. Note- check for list_id
  162. // as they may be signing up for two different llists
  163. ee()->db->query("DELETE FROM exp_mailing_list_queue WHERE email = '".ee()->db->escape_str($email)."' AND list_id = '".ee()->db->escape_str($list_id)."'");
  164. /** ----------------------------------------
  165. /** Is the email already in the list?
  166. /** ----------------------------------------*/
  167. if ($list_id !== FALSE)
  168. {
  169. $query = ee()->db->query("SELECT count(*) AS count FROM exp_mailing_list WHERE email = '".ee()->db->escape_str($email)."' AND list_id = '".ee()->db->escape_str($list_id)."'");
  170. if ($query->row('count') > 0)
  171. {
  172. $errors[] = lang('ml_email_already_in_list');
  173. }
  174. }
  175. }
  176. /** ----------------------------------------
  177. /** Are there errors to display?
  178. /** ----------------------------------------*/
  179. if (count($errors) > 0)
  180. {
  181. return ee()->output->show_user_error('submission', $errors);
  182. }
  183. /** ----------------------------------------
  184. /** Insert email
  185. /** ----------------------------------------*/
  186. $code = ee()->functions->random('alnum', 10);
  187. $return = '';
  188. if ($this->email_confirm == FALSE)
  189. {
  190. ee()->db->query("INSERT INTO exp_mailing_list (list_id, authcode, email, ip_address)
  191. VALUES ('".ee()->db->escape_str($list_id)."', '".$code."', '".ee()->db->escape_str($email)."', '".ee()->db->escape_str(ee()->input->ip_address())."')");
  192. $content = lang('ml_email_accepted');
  193. $return = $_POST['RET'];
  194. }
  195. else
  196. {
  197. ee()->db->query("INSERT INTO exp_mailing_list_queue (email, list_id, authcode, date) VALUES ('".ee()->db->escape_str($email)."', '".ee()->db->escape_str($list_id)."', '".$code."', '".time()."')");
  198. $this->send_email_confirmation($email, $code, $list_id);
  199. $content = lang('ml_email_confirmation_sent')."\n\n";
  200. $content .= lang('ml_click_confirmation_link');
  201. }
  202. // Clear security hash
  203. ee()->security->delete_xid(ee()->input->post('XID'));
  204. $site_name = (ee()->config->item('site_name') == '') ? lang('back') : stripslashes(ee()->config->item('site_name'));
  205. $data = array(
  206. 'title' => lang('ml_mailinglist'),
  207. 'heading' => lang('thank_you'),
  208. 'content' => $content,
  209. 'link' => array($_POST['RET'], $site_name)
  210. );
  211. ee()->output->show_message($data);
  212. }
  213. // -------------------------------------------------------------------------
  214. /**
  215. * Send Confirmation Email
  216. */
  217. function send_email_confirmation($email, $code, $list_id)
  218. {
  219. $query = ee()->db->query("SELECT list_title FROM exp_mailing_lists WHERE list_id = '".ee()->db->escape_str($list_id)."'");
  220. $action_id = ee()->functions->fetch_action_id('Mailinglist', 'authorize_email');
  221. $swap = array(
  222. 'activation_url' => ee()->functions->fetch_site_index(0, 0).QUERY_MARKER.'ACT='.$action_id.'&id='.$code,
  223. 'site_name' => stripslashes(ee()->config->item('site_name')),
  224. 'site_url' => ee()->config->item('site_url'),
  225. 'mailing_list' => $query->row('list_title')
  226. );
  227. $template = ee()->functions->fetch_email_template('mailinglist_activation_instructions');
  228. $email_tit = ee()->functions->var_swap($template['title'], $swap);
  229. $email_msg = ee()->functions->var_swap($template['data'], $swap);
  230. /** ----------------------------
  231. /** Send email
  232. /** ----------------------------*/
  233. ee()->load->library('email');
  234. ee()->email->wordwrap = true;
  235. ee()->email->mailtype = 'plain';
  236. ee()->email->priority = '3';
  237. ee()->email->from(ee()->config->item('webmaster_email'), ee()->config->item('webmaster_name'));
  238. ee()->email->to($email);
  239. ee()->email->subject($email_tit);
  240. ee()->email->message($email_msg);
  241. ee()->email->send();
  242. }
  243. // -------------------------------------------------------------------------
  244. /**
  245. * Authorize email submission
  246. */
  247. function authorize_email()
  248. {
  249. /** ----------------------------------------
  250. /** Fetch the mailinglist language pack
  251. /** ----------------------------------------*/
  252. ee()->lang->loadfile('mailinglist');
  253. // Is the mailing list turned on?
  254. if (ee()->config->item('mailinglist_enabled') == 'n')
  255. {
  256. return ee()->output->show_user_error('general', lang('mailinglist_disabled'));
  257. }
  258. /** ----------------------------------------
  259. /** Fetch the name of the site
  260. /** ----------------------------------------*/
  261. $site_name = (ee()->config->item('site_name') == '') ? lang('back') : stripslashes(ee()->config->item('site_name'));
  262. /** ----------------------------------------
  263. /** No ID? Tisk tisk...
  264. /** ----------------------------------------*/
  265. $id = ee()->input->get_post('id');
  266. if ($id == FALSE)
  267. {
  268. $data = array(
  269. 'title' => lang('ml_mailinglist'),
  270. 'heading' => lang('error'),
  271. 'content' => lang('invalid_url'),
  272. 'link' => array(ee()->functions->fetch_site_index(), $site_name)
  273. );
  274. ee()->output->show_message($data);
  275. }
  276. /** ----------------------------------------
  277. /** Fetch email associated with auth-code
  278. /** ----------------------------------------*/
  279. $expire = time() - (60*60*48);
  280. ee()->db->query("DELETE FROM exp_mailing_list_queue WHERE date < '$expire' ");
  281. $query = ee()->db->query("SELECT email, list_id FROM exp_mailing_list_queue WHERE authcode = '".ee()->db->escape_str($id)."'");
  282. if ($query->num_rows() == 0)
  283. {
  284. $data = array(
  285. 'title' => lang('ml_mailinglist'),
  286. 'heading' => lang('error'),
  287. 'content' => lang('ml_expired_date'),
  288. 'link' => array(ee()->functions->fetch_site_index(), $site_name)
  289. );
  290. echo ee()->output->show_message($data);
  291. exit;
  292. }
  293. /** ----------------------------------------
  294. /** Transfer email to the mailing list
  295. /** ----------------------------------------*/
  296. $email = $query->row('email') ;
  297. $list_id = $query->row('list_id') ;
  298. if ($list_id == 0)
  299. {
  300. $query = ee()->db->query("SELECT COUNT(*) AS count FROM exp_mailing_lists WHERE list_id = 1");
  301. if ($query->row('count') != 1)
  302. {
  303. return ee()->output->show_user_error('general', lang('ml_no_list_id'));
  304. }
  305. else
  306. {
  307. $list_id = 1;
  308. }
  309. }
  310. ee()->db->query("INSERT INTO exp_mailing_list (list_id, authcode, email, ip_address)
  311. VALUES ('".ee()->db->escape_str($list_id)."', '$id', '".ee()->db->escape_str($email)."', '".ee()->db->escape_str(ee()->input->ip_address())."')");
  312. ee()->db->query("DELETE FROM exp_mailing_list_queue WHERE authcode = '".ee()->db->escape_str($id)."'");
  313. /** ----------------------------------------
  314. /** Is there an admin notification to send?
  315. /** ----------------------------------------*/
  316. if (ee()->config->item('mailinglist_notify') == 'y' AND ee()->config->item('mailinglist_notify_emails') != '')
  317. {
  318. $query = ee()->db->select('list_title')
  319. ->get_where(
  320. 'mailing_lists',
  321. array('list_id' => $list_id)
  322. );
  323. $swap = array(
  324. 'email' => $email,
  325. 'mailing_list' => $query->row('list_title')
  326. );
  327. $template = ee()->functions->fetch_email_template('admin_notify_mailinglist');
  328. $email_tit = ee()->functions->var_swap($template['title'], $swap);
  329. $email_msg = ee()->functions->var_swap($template['data'], $swap);
  330. /** ----------------------------
  331. /** Send email
  332. /** ----------------------------*/
  333. // Remove multiple commas
  334. $notify_address = reduce_multiples(ee()->config->item('mailinglist_notify_emails'), ',', TRUE);
  335. if ($notify_address != '')
  336. {
  337. // Send email
  338. ee()->load->library('email');
  339. // Load the text helper
  340. ee()->load->helper('text');
  341. foreach (explode(',', $notify_address) as $addy)
  342. {
  343. ee()->email->EE_initialize();
  344. ee()->email->wordwrap = true;
  345. ee()->email->from(ee()->config->item('webmaster_email'), ee()->config->item('webmaster_name'));
  346. ee()->email->to($addy);
  347. ee()->email->reply_to(ee()->config->item('webmaster_email'));
  348. ee()->email->subject($email_tit);
  349. ee()->email->message(entities_to_ascii($email_msg));
  350. ee()->email->send();
  351. }
  352. }
  353. }
  354. /** ------------------------------
  355. /** Success Message
  356. /** ------------------------------*/
  357. $data = array(
  358. 'title' => lang('ml_mailinglist'),
  359. 'heading' => lang('thank_you'),
  360. 'content' => lang('ml_account_confirmed'),
  361. 'link' => array(ee()->functions->fetch_site_index(), $site_name)
  362. );
  363. ee()->output->show_message($data);
  364. }
  365. // -------------------------------------------------------------------------
  366. /**
  367. * Unsubscribe a user
  368. */
  369. function unsubscribe()
  370. {
  371. ee()->lang->loadfile('mailinglist');
  372. $site_name = (ee()->config->item('site_name') == '') ?
  373. lang('back') : stripslashes(ee()->config->item('site_name'));
  374. $id = ee()->input->get_post('id');
  375. // If $id is invalid, deal with it now
  376. // $id will be 0 if no id is passed or if it's invalid
  377. if ($id === 0)
  378. {
  379. $data = array(
  380. 'title' => lang('ml_mailinglist'),
  381. 'heading' => lang('error'),
  382. 'content' => lang('invalid_url'),
  383. 'link' => array(ee()->functions->fetch_site_index(), $site_name)
  384. );
  385. ee()->output->show_message($data);
  386. }
  387. // Fetch email associated with auth-code
  388. $expire = time() - (60*60*48);
  389. ee()->db->delete('mailing_list', array('authcode' => $id));
  390. if (ee()->db->affected_rows() == 0)
  391. {
  392. $data = array(
  393. 'title' => lang('ml_mailinglist'),
  394. 'heading' => lang('error'),
  395. 'content' => lang('ml_unsubscribe_failed'),
  396. 'link' => array(ee()->functions->fetch_site_index(), $site_name)
  397. );
  398. ee()->output->show_message($data);
  399. }
  400. $data = array(
  401. 'title' => lang('ml_mailinglist'),
  402. 'heading' => lang('thank_you'),
  403. 'content' => lang('ml_unsubscribe'),
  404. 'link' => array(ee()->functions->fetch_site_index(), $site_name)
  405. );
  406. ee()->output->show_message($data);
  407. }
  408. }
  409. // END CLASS
  410. /* End of file mod.mailinglist.php */
  411. /* Location: ./system/expressionengine/modules/mailinglist/mod.mailinglist.php */