2 require_once(dirname(__FILE__) .
"/cu_notify.data");
8 function GetSelectArray() {
25 function GetTagList() {
26 return "<blockquote><p><a><strong><em><span><img><ul><li><ol>";
44 function RemoveTemplate($dbh, $Cu, $Cn, $templateId) {
46 if ($templateId == 0) {
47 throw new exception(
"Template Id is required.", 1);
49 $updateTable = array(
"_action" =>
"delete",
"tmpl_id" => $templateId);
50 $updateTable = array(
"cuadmemailtmpl" => array($updateTable));
52 $sql =
"select email from cuadminusers where user_name = '$Cn' and cu = '$Cu'";
53 if (($sth = db_query($sql, $dbh)) !==
false) {
54 $email = db_fetch_row($sth)[0];
56 throw new exception(
"Email query failed.", 2);
59 if (DataAdminTableUpdate($dbh, array(
"cu" => $Cu), $updateTable, $Cn,
"RM_ETEMPL",
"broadcastEmails.prg",
"A",
"Email Template Deleted", $Cn, $email,
60 trim($_SERVER[
"REMOTE_ADDR"])) ===
false) {
61 throw new exception(
"Update failed.", 3);
64 $results = ReadTemplate($dbh, $Cu);
65 if ($results [
"status"] !==
"000") {
66 throw new exception (
"Read Template failed.", 4);
68 $templateDDL = $results[
"data"][
"templateDDL"];
70 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"templateDDL" => $templateDDL),
"info" =>
"Template was deleted successfully.",
"operation" =>
"removeTemplate");
71 }
catch(exception $e) {
72 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array(
"templateDDL" => array()),
"operation" =>
"removeTemplate");
101 function SaveTemplate($dbh, $Cu, $Cn, $testAddress, $body, $fromEmail, $fromName, $subject, $optin, $select, $templateName, $templateId) {
103 $selectArray = GetSelectArray();
104 $tagList = GetTagList();
105 $results = _ValidateTemplate ($testAddress, $fromEmail, $subject, $select, $templateName, $selectArray, $optin);
106 if ($results [
"status"] !==
"000") {
107 throw new exception ($results [
"error"], 18);
110 $fromEmail = strtolower(trim($fromEmail));
112 $body = strip_tags($body, $tagList);
115 $intSelect = array_flip($selectArray);
116 $intSelect = $intSelect[$select];
117 $updateTable = array(
"cu" => $Cu,
118 "tmpl_email" => $fromEmail,
119 "tmpl_email_name" => $fromName,
120 "tmpl_subject" => $subject,
121 "tmpl_message" => $body,
122 "tmpl_opt_mailto" => $optin ==
"Y" ? 1 : 0,
123 "tmpl_opt_mbr" => $intSelect,
124 "tmpl_lastmodified" => DBTIMESTAMP_USENOW,
125 "tmpl_name" => $templateName);
127 if ($templateId == 0) {
128 $sql =
"select nextval(pg_get_serial_sequence('cuadmemailtmpl', 'tmpl_id'))";
129 $sth = db_query($sql, $dbh);
131 throw new exception (
"nextval query failed.", 16);
133 $row = db_fetch_row($sth, 0);
134 $templateId = intval($row[0]);
135 $updateTable[
"tmpl_id"] = $templateId;
136 $updateTable[
"_action"] =
"insert";
137 $shortAction =
"ADD_ETEMPL";
138 $longAction =
"Add Email Template";
140 $updateTable[
"_action"] =
"update";
141 $shortAction =
"UPD_ETEMPL";
142 $updateTable[
"tmpl_id"] = $templateId;
143 $longAction =
"Update Email Template";
146 $sql =
"select email from cuadminusers where user_name = '$Cn' and cu = '$Cu'";
147 $sth = db_query($sql, $dbh);
149 throw new exception(
"Email query failed.", 14);
152 $email = trim(db_fetch_row($sth, 0)[0]);
154 $updateTable = array(
"cuadmemailtmpl" => array($updateTable));
155 if (DataAdminTableUpdate($dbh, array(
"cu" => $Cu), $updateTable, $Cn, $shortAction,
"broadcastEmails.prg",
"A", $longAction, $Cn, $email,
156 trim($_SERVER[
"REMOTE_ADDR"])) ===
false) {
157 throw new exception(
"Update failed.", 1);
160 $results = ReadTemplate($dbh, $Cu);
161 if ($results [
"status"] !==
"000") {
162 throw new exception (
"Read Template failed.", 17);
164 $templateDDL = $results [
"data"] [
"templateDDL"];
166 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"templateDDL" => $templateDDL,
"templateId" => $templateId),
"info" =>
"Template was saved successfully.",
167 "operation" =>
"saveTemplate");
168 }
catch(exception $e) {
169 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array(
"templateDDL" => array(),
"templateId" => 0),
"operation" =>
"saveTemplate");
189 function _ValidateTemplate ($testAddress, $fromEmail, $subject, $select, $templateName, $selectArray, $optin) {
191 if ($fromEmail ==
"") {
192 throw new exception (
"From email is required.", 12);
195 if (!validateEmail($fromEmail)) {
196 throw new exception(
"From address is not valid.", 7);
198 if ($subject ==
"") {
199 throw new exception (
"Subject is required.", 3);
201 if ($templateName ==
"") {
202 throw new exception (
"Name is required.", 15);
205 if (!isset($selectArray) || !is_array($selectArray)) {
206 throw new exception (
"Select array is not valid.", 2);
209 if (!in_array($select, $selectArray)) {
210 throw new exception (
"Select is invalid.", 5);
213 if ($testAddress !=
"" && !validateEmail($testAddress)) {
214 throw new exception(
"Test address is not valid.", 1);
217 if (!in_array($optin, array(
"N",
"Y"))) {
218 throw new exception (
"Optin is not valid.", 4);
221 $returnArray = array(
"status" =>
"000",
"error" =>
"");
222 }
catch (exception $e) {
223 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
242 function SendEmail($dbh, $Cu, $logger, $encoded) {
243 $tagList = GetTagList();
245 if ($encoded ==
"") {
246 throw new exception(
"Encoded is required.", 2);
248 $encoded = HCU_PayloadDecode($Cu, $encoded);
250 if (!is_array($encoded)) {
251 throw new exception(
"Encoded is not encoded correctly.", 3);
254 if (!HCU_array_key_exists(
"realSQL", $encoded) || !HCU_array_key_exists(
"body", $encoded) || !HCU_array_key_exists(
"fromName", $encoded) ||
255 !HCU_array_key_exists(
"templateId", $encoded) || !HCU_array_key_exists(
"templateId", $encoded) || !HCU_array_key_exists(
"subject", $encoded) ||
256 !HCU_array_key_exists(
"fromEmail", $encoded)) {
257 throw new exception(
"Encoded is not encoded correctly.", 5);
261 $body = strip_tags($body, $tagList);
263 if ($templateId != 0) {
264 $sql =
"update cuadmemailtmpl set tmpl_lastused = now() where tmpl_id = $templateId and cu = '$Cu'";
265 $sth = db_query($sql, $dbh);
267 throw new exception(
"Update query failed.", 1);
271 $sth = db_query($realSQL, $dbh);
273 throw new exception(
"Email read query failed.", 4);
277 $notify->mailfrom = $fromEmail;
278 $notify->mailfromname = $fromName;
279 $notify->subject = $subject;
280 $notify->htmlMsgbody = $body;
281 $notify->bulk =
true;
282 $notify->executeQuietly =
true;
283 $notify->callingfunction = __FUNCTION__;
284 $notify->file = __FILE__;
287 $results = db_fetch_all($sth);
288 $results = $results ===
false ? array() : $results;
291 foreach($results as $result) {
292 $emails[] = $result[
"email"];
295 $notify->mailto = $emails;
296 $notify->hideto =
true;
299 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"info" =>
"Emails were sent successfully.",
"operation" =>
"sendEmail");
300 }
catch(exception $e) {
301 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"operation" =>
"sendEmail");
320 function ReadSetup($dbh, $Cu) {
321 $templateDDL = array();
322 $emailList = array();
324 $results = ReadTemplate($dbh, $Cu);
325 if ($results [
"status"] !==
"000") {
326 throw new exception (
"Read Template failed.", 1);
328 $templateDDL = $results[
"data"][
"templateDDL"];
330 $sql =
"select lower(trim(r.email)) as email, r.verified from json_to_recordset((select a.email from cuadmnotify a where a.role = 'SES' and a.cu = '" .
331 prep_save($Cu, 10) .
"')::json) as r (email varchar, verified boolean) order by 1";
332 $sth = db_query($sql, $dbh);
334 throw new exception (
"Verified query failed.", 2);
336 $emailList = db_fetch_all($sth);
337 $emailList = $emailList ===
false ? array() : $emailList;
339 $hasUnverified =
false;
341 $verifiedList = array();
342 foreach($emailList as $emailRow) {
343 if ($emailRow[
"verified"] ===
"t") {
344 $verifiedList[] = array(
"email" => $emailRow[
"email"]);
345 }
else if (!$hasUnverified) {
346 $hasUnverified =
true;
350 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"templateDDL" => $templateDDL,
"emailList" => $verifiedList,
"hasUnverified" =>
351 $hasUnverified),
"operation" =>
"readSetup");
352 }
catch(exception $e) {
353 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array(
"templateDDL" => array(),
"emailList" => array(),
354 "hasUnverified" =>
false),
"operation" =>
"readSetup");
371 function ReadTemplate($dbh, $Cu) {
373 $selectArray = GetSelectArray();
374 $sql =
"select tmpl_id as value, tmpl_name as text, lower(trim(tmpl_email)) as fromemail, tmpl_email_name as fromname, tmpl_subject as subject, 375 tmpl_message as body, 376 case when tmpl_opt_mailto = 1 then 'Y' else 'N' end as optin, tmpl_opt_mbr as select from cuadmemailtmpl where cu = '$Cu' order by tmpl_name";
378 $sth = db_query($sql, $dbh);
380 throw new exception(
"Select query failed.", 1);
382 $templateDDL = db_fetch_all($sth);
383 $templateDDL = $templateDDL ===
false ? array() : $templateDDL;
385 foreach($templateDDL as $i => $row) {
386 $templateDDL[$i][
"select"] = HCU_array_key_value($row[
"select"], $selectArray);
389 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"templateDDL" => $templateDDL));
390 }
catch (exception $e) {
391 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array(
"templateDDL" => array()));
420 function PrepareEmail($dbh, $Cu, $testAddress, $body, $fromEmail, $fromName, $subject, $optin, $select, $templateId) {
423 $selectArray = GetSelectArray();
424 $tagList = GetTagList();
427 $results = _ValidateTemplate ($testAddress, $fromEmail, $subject, $select,
"unused", $selectArray, $optin);
428 if ($results [
"status"] !==
"000") {
429 throw new exception ($results [
"error"], 10);
432 $fromEmail = strtolower(trim($fromEmail));
434 $emailFlag = GetMsgTxValue(
'MSGTX_FORCE_EM');
435 $askBillPay = GetUserFlagsValue(
'MEM_ASKBPAY');
436 $results = _ConstructSQL($Cu, $optin, $select, $emailFlag, $askBillPay);
437 if ($results [
"status"] !==
"000") {
438 throw new exception (
"SQL failed.", 9);
440 extract($results[
"data"]);
442 $countSQL =
"select count(distinct lower(trim(u.email))) $baseSQL";
443 $realSQL =
"select distinct lower(trim(u.email)) as email $baseSQL";
445 $body = strip_tags($body, $tagList);
447 $sth = db_query($countSQL, $dbh);
449 throw new exception(
"Query failed.", 6);
451 $numEmails = intval(db_fetch_row($sth, 0)[0]);
453 if ($testAddress !=
"") {
456 $notify->mailto = $testAddress;
457 $notify->mailfrom = $fromEmail;
458 $notify->mailfromname = $fromName;
459 $notify->subject = $subject;
460 $notify->htmlMsgbody = $body;
462 $notify->file = __FILE__;
463 $notify->callingfunction = __FUNCTION__;
469 $encoded = HCU_PayloadEncode($Cu, array(
"realSQL" => $realSQL,
"body" => $body,
"fromEmail" => $fromEmail,
"templateId" => $templateId,
470 "subject" => $subject,
"fromName" => $fromName));
472 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"numEmails" => $numEmails,
"testSent" => $testSent,
"encoded" => $encoded),
473 "operation" =>
"prepareEmail");
474 }
catch(exception $e) {
475 $returnArray = array(
"status" => $e->getCode(),
"error" => array($e->getMessage()),
"data" => array(
"numEmails" => 0,
"testSent" =>
false,
"encoded" =>
""),
476 "operation" =>
"prepareEmail");
495 function _ConstructSQL($Cu, $optin, $select, $emailFlag, $askBillPay) {
497 $sql =
"from ${Cu}user u ";
498 $where = array(
"coalesce(trim(u.email), '') <> ''",
"(coalesce(u.msg_tx, 0) & $emailFlag) = 0");
502 $sql .=
" inner join (select user_id, accountnumber, row_number() over (partition by user_id, accountnumber) as rown 503 from ${Cu}useraccounts) uc on u.user_id = uc.user_id and uc.rown = 1";
504 $where[] =
"exists (select 'FOUND' from ${Cu}memberacct ma where uc.accountnumber = ma.accountnumber and estmnt_flag = 'Y')";
507 $sql .=
" inner join (select user_id, accountnumber, row_number() over (partition by user_id, accountnumber) as rown 508 from ${Cu}useraccounts) uc on u.user_id = uc.user_id and uc.rown = 1";
509 $where[] =
"not exists (select 'FOUND' from ${Cu}memberacct ma where uc.accountnumber = ma.accountnumber and estmnt_flag = 'Y')";
518 $sql .=
" inner join (select user_id, accountnumber, row_number() over (partition by user_id, accountnumber) as rown 519 from ${Cu}useraccounts) uc on u.user_id = uc.user_id and uc.rown = 1";
520 $where[] =
"exists (select 'FOUND' from cusms s where u.user_id = s.user_id and uc.accountnumber = s.accountnumber 521 and coalesce(trim(s.cellnumber::text), '') <> '' and s.cu = '${Cu}')";
526 throw new exception(
"Option is not valid.", 6);
530 $where[] =
"u.egenl_flag = 'Y'";
533 if (count($where) > 0) {
534 $sql .=
" where " . implode(
" and ", $where);
537 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"baseSQL" => $sql));
538 }
catch (exception $e) {
539 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array(
"baseSQL" =>
""));
556 function UpdateEmailList($dbh, $Cu) {
558 $results = ReadVerifiedEmailList($dbh, $Cu);
559 if ($results [
"status"] !==
"000") {
560 throw new exception (
"ReadVerifiedEmailList failed.", 1);
563 $emailList = array();
564 foreach($results [
"verifiedEmailList"] as $emailRow) {
565 if ($emailRow[
"status"] ===
"Success") {
566 $emailList[] = array(
"email" => strtolower(trim($emailRow[
"email"])));
570 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"emailList" => $emailList),
"operation" =>
"updateEmailList");
571 }
catch (exception $e) {
572 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array(
"emailList" => array()),
"operation" =>
"updateEmailList");