Xataface Email Module  0.3.2
Email/Mailmerge Module for Xataface
 All Data Structures Files Functions Variables Pages
email_opt_out.php
Go to the documentation of this file.
1 <?php
3  const ADDRESS_NOT_FOUND = 8404;
4  const ADDRESS_ALREADY_FOUND = 8300;
5 
6 
7  function handle(&$params){
8  $app = Dataface_Application::getInstance();
9  $query = $app->getQuery();
10 
11  if ( @$_POST['--opt-in'] ){
12  // The user wants to opt in to the list
13 
14  $this->optIn(@$_POST['--email-id']);
15 
16 
17  } else if ( @$_POST['--opt-out'] ){
18  // The user wants to opt out of the list
19  $this->optOut(@$_POST['--email-id']);
20 
21  } else {
22  // Display the form
23 
24 
25 
26  $mod = Dataface_ModuleTool::getInstance()->loadModule('modules_Email');
27  $mod->addPaths();
28  Dataface_JavascriptTool::getInstance()->import('xataface/modules/Email/email_opt_out.js');
29  $addr = $this->getEmailAddressFromId($query['email']);
30  if ( !@$query['email'] or !$addr){
31  // No email ID was provided ... Display error
32  df_display(array(), 'xataface/modules/email/email_opt_out_error.html');
33 
34  } else {
35  $context = array('emailId'=>$query['email'], 'emailAddress'=> $addr);
36 
37  if ( $this->checkBlackList($query['email']) ){
38  //echo "Blacklisted";
39  $context['currentlyBlackListed'] = 1;
40  } else {
41  //echo "Not blacklisted";
42  $context['currentlyBlackListed'] = 0;
43  }
44  df_display($context, 'xataface/modules/email/email_opt_out.html');
45 
46  }
47 
48  }
49 
50 
51  }
52 
53 
54  function optIn($emailId){
55 
56  try {
57  if ( $this->checkBlackList($emailId) ){
58  $addr = $this->getEmailAddressFromId($emailId);
59  df_q("delete from dataface__email_blacklist where email='".addslashes($addr)."'");
60  $app = Dataface_Application::getInstance();
61  $del = $app->getDelegate();
62  $method = 'Email__afterOptIn';
63  if ( isset($del) and method_exists($del, $method) ){
64  $del->$method($addr);
65  }
66  $this->out(array(
67  'code' => 200,
68  'message' => 'Opt-in successful. Thank you for your participation.'
69  ));
70  } else {
71  throw new Exception('You have already opted into our mail list and should be able to receive our mailouts.', self::ADDRESS_NOT_FOUND);
72 
73  }
74 
75 
76  } catch (Exception $ex){
77 
78  if ( $ex->getCode() > 8000 and $ex->getCode() < 9000 ){
79  $this->out(array(
80  'code' => intval($ex->getCode()-8000),
81  'message' => $ex->getMessage()
82  ));
83  }
84 
85  }
86 
87  }
88 
89 
90  function optOut($emailId){
91  error_log("Opting out $emailId");
92  try {
93  if ( !$this->checkBlackList($emailId) ){
94  $addr = $this->getEmailAddressFromId($emailId);
95  df_q("insert into dataface__email_blacklist (email) values ('".addslashes($addr)."')");
96  $app = Dataface_Application::getInstance();
97  $del = $app->getDelegate();
98  $method = 'Email__afterOptOut';
99  if ( isset($del) and method_exists($del, $method) ){
100  $del->$method($addr);
101  }
102  $this->out(array(
103  'code' => 200,
104  'message' => 'Opt-out successful. You will no longer receive emails from us.'
105  ));
106  } else {
107  throw new Exception('You have already opted out. You should no longer receive any email from us.', self::ADDRESS_ALREADY_FOUND);
108 
109  }
110 
111 
112  } catch (Exception $ex){
113 
114  if ( $ex->getCode() > 8000 and $ex->getCode() < 9000 ){
115  $this->out(array(
116  'code' => intval($ex->getCode()-8000),
117  'message' => $ex->getMessage()
118  ));
119  }
120 
121  }
122  }
123 
124 
125  function getEmailAddressFromId($emailId){
126  $res = df_q("select recipient_email from xataface__email_log where `uniqid`='".addslashes($emailId)."'");
127  if ( mysql_num_rows($res) == 0 ) return null;
128  list($addr) = mysql_fetch_row($res);
129  @mysql_free_result($res);
130  return $addr;
131 
132 
133  }
134 
135 
136  function checkBlackList($emailId){
137  $addr = $this->getEmailAddressFromId($emailId);
138 
139  $res = df_q("select email from dataface__email_blacklist where `email`='".addslashes($addr)."' limit 1");
140  if ( mysql_num_rows($res) == 0 ){
141  @mysql_free_result($res);
142  return false;
143  } else {
144  @mysql_free_result($res);
145  return true;
146  }
147 
148  }
149 
150  function out($out){
151  header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
152  echo json_encode($out);
153  }
154 }