Xataface Email Module 0.2
Email/Mailmerge Module for Xataface
actions/email_opt_out.php
Go to the documentation of this file.
00001 <?php
00002 class actions_email_opt_out {
00003         const ADDRESS_NOT_FOUND = 8404;
00004         const ADDRESS_ALREADY_FOUND = 8300;
00005         
00006 
00007         function handle(&$params){
00008                 $app = Dataface_Application::getInstance();
00009                 $query = $app->getQuery();
00010                 
00011                 if ( @$_POST['--opt-in'] ){
00012                         // The user wants to opt in to the list
00013                         
00014                         $this->optIn(@$_POST['--email-id']);
00015                         
00016                         
00017                 } else if ( @$_POST['--opt-out'] ){
00018                         // The user wants to opt out of the list
00019                         $this->optOut(@$_POST['--email-id']);
00020                 
00021                 } else {
00022                         // Display the form
00023                         
00024                         
00025                         
00026                         $mod = Dataface_ModuleTool::getInstance()->loadModule('modules_Email');
00027                         $mod->addPaths();
00028                         Dataface_JavascriptTool::getInstance()->import('xataface/modules/Email/email_opt_out.js');
00029                         $addr = $this->getEmailAddressFromId($query['email']);
00030                         if ( !@$query['email'] or !$addr){
00031                                 // No email ID was provided ... Display error
00032                                 df_display(array(), 'xataface/modules/email/email_opt_out_error.html');
00033                         
00034                         } else {
00035                                 $context = array('emailId'=>$query['email'], 'emailAddress'=> $addr);
00036                                 
00037                                 if ( $this->checkBlackList($query['email']) ){
00038                                         //echo "Blacklisted";
00039                                         $context['currentlyBlackListed'] = 1;
00040                                 } else {
00041                                         //echo "Not blacklisted";
00042                                         $context['currentlyBlackListed'] = 0;
00043                                 }
00044                                 df_display($context, 'xataface/modules/email/email_opt_out.html');
00045                                 
00046                         }
00047                                 
00048                 }
00049                 
00050                 
00051         }
00052         
00053         
00054         function optIn($emailId){
00055         
00056                 try {
00057                         if ( $this->checkBlackList($emailId) ){
00058                                 $addr = $this->getEmailAddressFromId($emailId);
00059                                 df_q("delete from dataface__email_blacklist where email='".addslashes($addr)."'");
00060                                 
00061                                 $this->out(array(
00062                                         'code' => 200,
00063                                         'message' => 'Opt-in successful.  Thank you for your participation.'
00064                                 ));
00065                         } else {
00066                                 throw new Exception('You have already opted into our mail list and should be able to receive our mailouts.', self::ADDRESS_NOT_FOUND);
00067                                 
00068                         }
00069                         
00070                 
00071                 } catch (Exception $ex){
00072                 
00073                         if ( $ex->getCode() > 8000 and $ex->getCode() < 9000 ){
00074                                 $this->out(array(
00075                                         'code' => intval($ex->getCode()-8000),
00076                                         'message' => $ex->getMessage()
00077                                 ));
00078                         }
00079                 
00080                 }
00081         
00082         }
00083         
00084         
00085         function optOut($emailId){
00086                 error_log("Opting out $emailId");
00087                 try {
00088                         if ( !$this->checkBlackList($emailId) ){
00089                                 $addr = $this->getEmailAddressFromId($emailId);
00090                                 df_q("insert into  dataface__email_blacklist (email) values ('".addslashes($addr)."')");
00091                                 
00092                                 $this->out(array(
00093                                         'code' => 200,
00094                                         'message' => 'Opt-out successful.  You will no longer receive emails from us.'
00095                                 ));
00096                         } else {
00097                                 throw new Exception('You have already opted out.  You should no longer receive any email from us.', self::ADDRESS_ALREADY_FOUND);
00098                                 
00099                         }
00100                         
00101                 
00102                 } catch (Exception $ex){
00103                 
00104                         if ( $ex->getCode() > 8000 and $ex->getCode() < 9000 ){
00105                                 $this->out(array(
00106                                         'code' => intval($ex->getCode()-8000),
00107                                         'message' => $ex->getMessage()
00108                                 ));
00109                         }
00110                 
00111                 }
00112         }
00113         
00114         
00115         function getEmailAddressFromId($emailId){
00116                 $res = df_q("select recipient_email from xataface__email_log where `uniqid`='".addslashes($emailId)."'");
00117                 if ( mysql_num_rows($res) == 0 ) return null;
00118                 list($addr) = mysql_fetch_row($res);
00119                 @mysql_free_result($res);
00120                 return $addr;
00121                 
00122                 
00123         }
00124         
00125         
00126         function checkBlackList($emailId){
00127                 $addr = $this->getEmailAddressFromId($emailId);
00128                 
00129                 $res = df_q("select email from dataface__email_blacklist where `email`='".addslashes($addr)."' limit 1");
00130                 if ( mysql_num_rows($res) == 0 ){
00131                         @mysql_free_result($res);
00132                         return false;
00133                 } else {
00134                         @mysql_free_result($res);
00135                         return true;
00136                 }
00137         
00138         }
00139         
00140         function out($out){
00141                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00142                 echo json_encode($out);
00143         }
00144 }
 All Data Structures Files Functions Variables Enumerations