11 function GetBroadcastFileLoc() {
12 return "/home/homecu/tmp/";
24 function GetGoogleCookie($devmode) {
26 $googleCookie =
"c2FtdWVsQGhvbWVjdS5jb218TVZqSHg3dzRZSzlVZksyUkdsdmc0QzJheCtNQjlxLzhsbEN1MXVxUUFOSXFlZ3J1THZmTkxvRW53cjZsV2ZMY2IxdHk1SkZVdGdic1U2UWpRSjU2UytvL2NWaEIzQnl6VWcrRFNzT2lHRDRuaFFGRlNlOUJlVENDWGd4WkVOWjFQNytueldEL21yVjZlc2xjQ2dtbzNLL0xOZEltMks5Q2dpY0E2YlFuWk9WU2J6VzE3U0REalNFeTJpWHYzNS9LeXc9PXwxNTY2NTc1OTMyfFRyMVE0SU42K0o0QXlhWSt4YkVOWFVpT21lWGVxb0ZJQWhMY1gyUFQvb3RhU1gwa0NVT0lGTktvL2RNWEJpSDVwbDBITHRzNEpwb2c3bk91dWc9PQ==|1566572332|L2UZZ4lu2KexoIzFRxzMpDNu_cU=";
28 $googleCookie =
"{$_COOKIE['homecu_dev_oauth2_proxy']}";
39 function GetDefaultEmail() {
40 return "support@homecu.net";
49 function GetWhenToGetNewCopy() {
64 function GatherCuList($dbh, $sysenv) {
66 $hcuTicket = urlencode($_COOKIE[
'HCUTicket']);
67 $googleCookie = GetGoogleCookie($sysenv[
"devmode"]);
71 if ($sysenv[
"devmode"] == 1) {
72 $curlCookies =
"HCUTicket=$hcuTicket";
73 $url =
"http://localhost/hcuadm/hcucontact_rpt.prg?report=1&csv=raw";
76 $curlCookies =
"HCUTicket=$hcuTicket;homecu_dev_oauth2_proxy=$googleCookie";
77 $url =
"https://my.homecu.net/hcuadm/hcucontact_rpt.prg?report=1&csv=raw";
80 $ch = curl_init($url);
81 curl_setopt($ch, CURLOPT_COOKIE, $curlCookies);
82 curl_setopt($ch, CURLOPT_USERPWD,
"nobody:no1home");
83 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); #
get response as
string 85 $rawresp = curl_exec($ch);
88 $serverList = array();
91 $lines = explode(
"\r\n", $rawresp);
93 foreach($lines as $i => $line) {
95 if ($i < 2 || trim($line) ==
"") {
99 $values = explode(
"\t", $line);
100 $cu = HCU_array_key_value(0, $values);
101 $wwwServer = HCU_array_key_value(1, $values);
102 $cu = $cu ===
false ?
"" : trim($cu);
103 $wwwServer = $wwwServer ===
false ?
"" : trim($wwwServer);
105 case "my.homecu.net":
106 if (!HCU_array_key_exists(
"my", $serverList)) {
107 $serverList[
"my"] = array();
109 $serverList[
"my"][$cu] =
true;
112 if (!HCU_array_key_exists(
"www3", $serverList)) {
113 $serverList[
"www3"] = array();
115 $serverList[
"www3"][$cu] =
true;
118 if (!HCU_array_key_exists(
"www5", $serverList)) {
119 $serverList[
"www5"] = array();
121 $serverList[
"www5"][$cu] =
true;
124 if (!HCU_array_key_exists(
"www6", $serverList)) {
125 $serverList[
"www6"] = array();
127 $serverList[
"www6"][$cu] =
true;
130 if (!HCU_array_key_exists(
"localhost", $serverList)) {
131 $serverList[
"localhost"] = array();
133 $serverList[
"localhost"][$cu] =
true;
139 $elist = GetBroadcastFileLoc() .
"elist_master";
141 $handle = fopen(
"${elist}.new",
"w");
144 throw new exception (
"couldn't open file ${elist}.new", 2);
147 fwrite($handle, HCU_JsonEncode($serverList));
150 $returnArray = array(
"status" =>
"000",
"error" =>
"");
151 }
catch (exception $e) {
152 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
170 function GatherEmailListByServer($dbh, $sysenv, $server, $validCus) {
172 $hcuTicket = urlencode($_COOKIE[
'HCUTicket']);
173 $googleCookie = GetGoogleCookie($sysenv[
"devmode"]);
175 if ($server ==
"my") {
176 $url =
"https://my.homecu.net/hcuadm/hcucontact_rpt.prg?report=4&csv=raw";
177 $curlCookies =
"HCUTicket=$hcuTicket;homecu_dev_oauth2_proxy=$googleCookie";
178 }
else if ($sysenv[
"devmode"] == 1 && $server ==
"localhost") {
179 $url =
"http://localhost/hcuadm/hcucontact_rpt.prg?report=4&csv=raw";
180 $curlCookies =
"HCUTicket=$hcuTicket";
182 $url =
"https://$server.homecu.net/hcuadm/hcucontact_rpt?report=4&csv=raw";
183 $curlCookies =
"HCUTicket=$hcuTicket";
186 $ch = curl_init($url);
187 curl_setopt($ch,CURLOPT_COOKIE, $curlCookies);
188 curl_setopt($ch,CURLOPT_USERPWD,
"nobody:no1home");
189 curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); #
get response as
string 191 $rawresp = curl_exec($ch);
197 $lines = explode(
"\r\n", $rawresp);
199 foreach($lines as $i => $line) {
201 if ($i < 2 || trim($line) ==
"") {
205 $values = explode(
"\t", $line);
207 $cu = HCU_array_key_value(0, $values);
208 $email = HCU_array_key_value(1, $values);
209 $cu = $cu ===
false ?
"" : trim($cu);
210 $email = $email ===
false ?
"" : trim($email);
212 # if the monitor list has this cu on this server, store the result 213 if (HCU_array_key_exists($cu, $validCus) && $email !=
"") {
214 $cuList[$cu] = $email;
219 $newCuList = array();
220 foreach ($cuList as $cu => $email) {
221 $newCuList[$cu] = array();
223 $emlist = preg_split(
'/[;, ]/',$email);
224 foreach ($emlist as $em) {
227 $newCuList[$cu][] = $em;
232 $elist = GetBroadcastFileLoc() .
"elist_$server";
233 $handle = fopen(
"${elist}.new",
"w");
236 throw new exception (
"couldn't open file ${elist}.new", 2);
239 fwrite($handle, HCU_JsonEncode($newCuList));
242 $returnArray = array(
"status" =>
"000",
"error" =>
"");
243 }
catch (exception $e) {
244 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
261 function ReadSetup($dbh, $sysenv) {
263 $sql =
"select email from cuadmnotify where cu = 'HOMECU' and role = 'SES'";
264 $sth = db_query($sql, $dbh);
266 throw new exception (
"Select statement failed.", 1);
268 $emailList = array();
269 if (db_num_rows($sth) > 0) {
270 $row = db_fetch_assoc($sth, 0);
271 $decodedList = HCU_JsonDecode($row[
"email"]);
272 foreach ($decodedList as $emailRec) {
273 if ($emailRec[
"verified"]) {
274 $emailList[] = $emailRec[
"email"];
280 array_unshift($emailList, GetDefaultEmail());
282 $servers = array(
"ALL",
"my",
"www5",
"www3",
"www6");
284 if ($sysenv[
"devmode"] == 1) {
285 $servers[] =
"localhost";
288 $serverList = array();
289 $whenToGetNewCopy = GetWhenToGetNewCopy();
292 $file = GetBroadcastFileLoc() .
"elist_master.new";
293 if (file_exists($file)) {
294 if ($now - filemtime($file) < $whenToGetNewCopy) {
295 $results = GatherCuList($dbh, $sysenv);
296 if ($results[
"status"] !==
"000") {
297 throw new exception ($results[
"error"], 1);
301 $file = GetBroadcastFileLoc() .
"elist_master.last";
304 if (!file_exists($file) || $now - filemtime($file) < $whenToGetNewCopy) {
305 $results = GatherCuList($dbh, $sysenv);
306 if ($results[
"status"] !==
"000") {
307 throw new exception ($results[
"error"], 7);
309 $file = GetBroadcastFileLoc() .
"elist_master.new";
312 if (!file_exists($file)) {
313 throw new exception (
"Cu List doesn't exist.", 2);
315 $serverList = file_get_contents($file);
316 if ($serverList ==
null || trim($serverList) ==
"") {
317 throw new exception (
"Cu List doesn't exist.", 3);
319 $serverList = HCU_JsonDecode($serverList);
323 foreach($serverList as $server => $validCus) {
324 $file = GetBroadcastFileLoc() .
"elist_$server.new";
325 if (file_exists($file)) {
326 if ($now - filemtime($file) < $whenToGetNewCopy) {
327 $results = GatherEmailListByServer($dbh, $sysenv, $server, $validCus);
328 if ($results[
"status"] !==
"000") {
329 throw new exception ($results[
"error"], 4);
333 $file = GetBroadcastFileLoc() .
"elist_$server.last";
336 if (!file_exists($file) || $now - filemtime($file) < $whenToGetNewCopy) {
337 $results = GatherEmailListByServer($dbh, $sysenv, $server, $validCus);
338 if ($results[
"status"] !==
"000") {
339 throw new exception ($results[
"error"], 5);
341 $file = GetBroadcastFileLoc() .
"elist_$server.new";
345 if (!file_exists($file)) {
346 throw new exception (
"Cu List doesn't exist.", 6);
349 $cus = array_merge($cus, array_keys($validCus));
351 sort($cus, SORT_STRING);
353 $actions = array(array(
"text" =>
"Email",
"value" =>
"email"),
354 array(
"text" =>
"Download Tab-Delimited File",
"value" =>
"download"));
356 $data = array(
"emailList" => $emailList,
"servers" => $servers,
"cuList" => $cus,
"actions" => $actions);
357 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $data,
"action" =>
"read");
359 }
catch (exception $e) {
360 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"action" =>
"read");
376 function GetFilteredCUList($sysenv) {
378 $file = GetBroadcastFileLoc() .
"elist_master.new";
379 if (!file_exists($file)) {
380 $file = GetBroadcastFileLoc() .
"elist_master.last";
382 if (!file_exists($file)) {
383 throw new exception (
"Master file doesn't exist.", 1);
386 $serverList = file_get_contents($file);
387 if ($serverList ==
null || trim($serverList) ==
"") {
388 throw new exception (
"Master file doesn't exist.", 2);
391 $filteredCus = HCU_array_key_value(
"filteredCus", $sysenv[
"IMPORTS"]);
392 $filteredServers = HCU_array_key_value(
"filteredServers", $sysenv[
"IMPORTS"]);
393 $filteredCus = $filteredCus ===
false ?
"" : trim($filteredCus);
394 $filteredServers = $filteredServers ===
false ?
"" : trim($filteredServers);
396 if ($filteredCus ==
"") {
397 throw new exception (
"Filtered Cus are required.", 5);
399 if ($filteredServers ===
"") {
400 throw new exception (
"Filtered servers are required.", 6);
402 $filteredCus = HCU_JsonDecode($filteredCus);
403 $filteredServers = HCU_JsonDecode($filteredServers);
405 $serverList = HCU_JsonDecode($serverList);
407 foreach($serverList as $server => $unused) {
408 if (count($filteredServers) > 0 && !in_array(
"ALL", $filteredServers) && !in_array($server, $filteredServers)) {
411 $file = GetBroadcastFileLoc() .
"elist_$server.new";
412 if (!file_exists($file)) {
413 $file = GetBroadcastFileLoc() .
"elist_$server.last";
415 if (!file_exists($file)) {
416 throw new exception (
"Server file $server doesn't exist.", 3);
419 $serverCus = file_get_contents($file);
420 if ($serverCus ==
null || trim($serverCus) ==
"") {
421 throw new exception (
"Server file $server doesn't exist.", 4);
423 $serverCus = HCU_JsonDecode($serverCus);
425 foreach($serverCus as $cu => $emails) {
426 if (count($filteredCus) > 0 && !in_array($cu, $filteredCus)) {
429 $cuRecord = array(
"cu" => $cu,
"server" => $server,
"emails" => $emails);
430 $cuList[$cu] = $cuRecord;
435 ksort($cuList, SORT_STRING);
437 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"cuList" => $cuList);
438 }
catch (exception $e) {
439 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
451 function ChangeToLast() {
453 $fileList = array(
"master",
"localhost",
"www3",
"www5",
"www6",
"my");
454 foreach($fileList as $filePart) {
455 $file = GetBroadcastFileLoc() .
"elist_$filePart";
456 if (file_exists(
"$file.new")) {
457 $results = shell_exec(
"mv ${file}.new ${file}.last 2>&1");
458 if ($results !=
"") {
459 throw new exception ($results, 1);
464 $returnArray = array(
"status" =>
"000",
"error" =>
"");
465 }
catch (exception $e) {
466 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
480 function DownloadCSV($dbh, $sysenv) {
483 $results = GetFilteredCUList($sysenv);
485 if ($results[
"status"] !==
"000") {
486 throw new exception ($results[
"error"], 1);
489 $csvData[] =
"Display Name\tServer\tPrimary Email";
491 foreach($results[
"cuList"] as $cuRecord) {
492 foreach($cuRecord[
"emails"] as $email) {
493 $csvData[] = $cuRecord[
"cu"] .
"\t" . $cuRecord[
"server"] .
"\t" . $email;
497 $results = ChangeToLast();
498 if ($results[
"status"] !==
"000") {
499 throw new exception ($results[
"error"], 2);
502 }
catch (exception $e) {
503 $csvData = array(
"Download Failed: " . $e->getMessage());
506 $csvData = implode(
"\r\n", $csvData);
508 header(
"Content-length: " . strlen($csvData) );
509 header(
"Content-type: application/octetstream");
510 header(
"Content-disposition: inline; filename=\"elist.csv\"");
525 function ValidateEmailParameters($sysenv, $requireTestEmail) {
527 $fromEmail = HCU_array_key_value(
"fromEmail", $sysenv[
"IMPORTS"]);
528 $fromEmail = $fromEmail ===
false ?
"" : trim($fromEmail);
529 if ($fromEmail ==
"") {
530 throw new exception(
"From Email is required.", 1);
532 $fromName = HCU_array_key_value(
"fromName", $sysenv[
"IMPORTS"]);
533 $fromName = $fromName ===
false ?
"" : trim($fromName);
535 $subject = HCU_array_key_value(
"subject", $sysenv[
"IMPORTS"]);
536 $subject = $subject ===
false ?
"" : trim($subject);
538 $testEmail = HCU_array_key_value(
"testEmail", $sysenv[
"IMPORTS"]);
539 $testEmail = $testEmail ===
false ?
"" : trim($testEmail);
540 if ($testEmail ==
"" && $requireTestEmail) {
541 throw new exception (
"Test Email is required.", 2);
544 $body = HCU_array_key_value(
"body", $sysenv[
"IMPORTS"]);
545 $body = $body ===
false ?
"" : htmlspecialchars_decode(trim($body));
547 if (!validateEmail($fromEmail)) {
548 throw new exception (
"From Email is not valid.", 3);
551 if ($testEmail !=
"" && !validateEmail($testEmail)) {
552 throw new exception (
"Test Email is not valid.", 4);
555 $data = array(
"fromEmail" => $fromEmail,
"fromName" => $fromName,
"subject" => $subject,
"testEmail" => $testEmail,
"body" => $body);
556 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $data);
557 }
catch (exception $e) {
558 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
574 function SendTestEmail($dbh, $sysenv) {
576 $results = ValidateEmailParameters($sysenv,
true);
577 if ($results[
"status"] !==
"000") {
578 throw new exception ($results[
"error"], 1);
580 extract($results[
"data"]);
583 $notify->mailfrom = $fromEmail;
584 $notify->mailfromname = $fromName;
585 $notify->subject = $subject;
586 $notify->htmlMsgbody = $body;
587 $notify->executeQuietly =
true;
588 $notify->mailto = $testEmail;
591 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"action" =>
"preview");
592 }
catch (exception $e) {
593 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"action" =>
"preview");
609 function SendEmails($dbh, $sysenv) {
611 $results = ValidateEmailParameters($sysenv,
false);
612 if ($results[
"status"] !==
"000") {
613 throw new exception ($results[
"error"], 1);
615 extract($results[
"data"]);
617 $results = GetFilteredCUList($sysenv);
619 if ($results[
"status"] !==
"000") {
620 throw new exception ($results[
"error"], 2);
623 $emailList = array();
624 foreach($results[
"cuList"] as $cuRecord) {
625 foreach($cuRecord[
"emails"] as $email) {
626 $emailList[] = $email;
631 $notify->mailfrom = $fromEmail;
632 $notify->mailfromname = $fromName;
633 $notify->subject = $subject;
634 $notify->htmlMsgbody = $body;
635 $notify->executeQuietly =
true;
636 $notify->mailto = $emailList;
637 $notify->bulk =
true;
638 $notify->hideto =
true;
641 $results = ChangeToLast();
642 if ($results[
"status"] !==
"000") {
643 throw new exception ($results[
"error"], 3);
647 if ($fromEmail != GetDefaultEmail()) {
648 $sql =
"select email from cuadmnotify where cu = 'HOMECU' and role = 'SES'";
649 $sth = db_query($sql, $dbh);
651 throw new exception (
"Select query failed.", 4);
654 if (db_num_rows($sth) <= 0) {
656 $verifiedEmails = array(array(
"email" => $fromEmail,
"verified" =>
true));
659 $verifiedEmails = db_fetch_row($sth, 0)[0];
660 $verifiedEmails = isset($verifiedEmails) ? HCU_JsonDecode($verifiedEmails) : array();
661 $newEmailTest = trim(strtolower($fromEmail));
662 foreach ($verifiedEmails as $emailRec) {
663 if ($newEmailTest == trim(strtolower($emailRec[
"email"]))) {
669 if ($mode !=
"don't") {
670 $verifiedEmails[] = array(
"email" => $fromEmail,
"verified" =>
true);
676 $sql =
"insert into cuadmnotify (cu, role, email) values ('HOMECU', 'SES', '" . prep_save(HCU_JsonEncode($verifiedEmails)) .
"')";
677 $sth = db_query($sql, $dbh);
679 throw new exception (
"Insert query failed.", 5);
683 $sql =
"update cuadmnotify set email = '" . prep_save(HCU_JsonEncode($verifiedEmails)) .
"' where cu = 'HOMECU' and role = 'SES'";
684 $sth = db_query($sql, $dbh);
686 throw new exception (
"Update query failed.", 6);
691 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"action" =>
"send",
"info" =>
"Emails were successfully sent.");
692 }
catch (exception $e) {
693 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"action" =>
"send");