<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
//use AppBundle\Controller\SecurityController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use AppBundle\Helpers\Constants;
use Twilio\Rest\Client;
use AppBundle\Helpers\MailerHelper;
use Doctrine\ORM\EntityManagerInterface;
use AppBundle\Shame\Shame;
class DefaultController extends Controller
{
/**
* @Route("/sendcert", name="sendcert")
* @param Request $request
* @return string
*/
public function sendcertAction(Request $request)
{
$subject = $request->get('subject');
$email = $request->get('to');
$cc = $request->get('cc');
$cc2 = $request->get('cc2');
$body = $request->get('body');
$id = $request->get('cert');
$mail = new MailerHelper();
$email = str_replace(';', ',', $email);
$to = [ $email, $cc];
if ($cc2) {
$to[] = $cc2;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.accredible.com/v1/credentials/generate_single_pdf/$id");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Token token=ebcdbc42460d12266cc0f76d8a49286a"
));
$response = curl_exec($ch);
curl_close($ch);
$data =json_decode($response);
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents($data->file, false, stream_context_create($arrContextOptions));
file_put_contents('pdf/' . $id .'.pdf', $response);
$html = $body;
$response = $mail->sendEmailwithAttachment(
$email,
'info@asbestosonlinetraining.com' ,
'Asbestos Guys',
$cc,
$cc2,
$subject,
$html,
$body,
'pdf/' . $id .'.pdf'
);
return (new Response("OK"));
}
/**
* @Route("/sendtest", name="sendtest")
* @param Request $request
* @return string
*/
public function sendtestAction(Request $request)
{
$subject = 'This is a test';
$email = 'erik@olson.host';
$cc = 'erik@evodialer.com';
$cc2 = 'erikolson1965@gmail.com';
$body = 'Hello World - this is a test';
$html = '<h1>Hello World</h2><hr><p>This is a test</p>';
$mail = new MailerHelper();
$response = $mail->sendEmailwithAttachment(
$email,
'info@asbestosonlinetraining.com' ,
'Asbestos Guys',
$cc,
$cc2,
$subject,
$html,
$body,
'/var/www/apic/web/robots.txt'
);
return (new Response("OK"));
}
/**
* @Route("/sendm", name="sendm")
* @param Request $request
* @return string
*/
public function sendmAction(Request $request)
{
$subject = $request->get('subject');
$body = $request->get('body');
$email = $request->get('email');
$mail = new MailerHelper();
$ctr = 1;
$email = str_replace(';', ',', $email);
$email = explode(',', $email);
$response = $mail->sendEmail( $email, 'info@asbestosonlinetraining.com' , $subject, $body);
return (new Response("OK"));
}
/**
* @Route("/post", name="post")
* @param Request $request
* @return string
*/
public function postAction(Request $request)
{
$subject = $request->get('subject');
$body = $request->get('body');
$emails = $request->get('emails');
file_put_contents('mail.html.json', json_encode([$subject, $body]));
$emails = str_replace([ ',' , "\n","\r",' ' ], ';', $emails);
$emails = explode(';',$emails);
$emails = array_filter($emails);
echo "<h1>Sending...</h1><pre>";
//var_dump($emails);
$mail = new MailerHelper();
$ctr = 1;
foreach ($emails as $email) {
if ($ctr++ > 14) {
$ctr = 1;
flush();
sleep(1);
}
echo "Sending to: $email\n";
$response = $mail->sendEmail( [ $email ], 'info@asbestosonlinetraining.com' , $subject, $body);
echo "AWS Response: $response\n\n";
}
return (new Response("Send Completee\n\n</pre><a href='/mailthing'>Send summore mails</a>"));
}
/**
* @Route("/mailthing", name="mailthing")
* @param Request $request
* @return string
*/
public function indexAction(Request $request)
{
//return $this->render('default/index.html.twig');
//$shame = new Shame($this);
//return (new JsonResponse($shame->getUsers()));
$email = file_get_contents('mail.html.json');
$email = json_decode($email);
return $this->render('mail.html.twig', [
'body' => $email[1],
'subject' => $email[0]
]);
}
private function init() {
$this->shame = new Shame($this);
}
public function getP($name)
{
try {
return $this->getParameter($name);
} catch (\Exception $e) {
}
try {
return $this->getParameter(strtoupper($name));
} catch (\Exception $e) {
}
try {
return $this->getParameter(strtolower($name));
} catch (\Exception $e) {
exit($e->getMessage() . " Both upper and lower case variable names were attempted");
}
}
/**
* @Route("/artefacts/recording-events", name="artefacts-recording-events")
* @param Request $request
* @return string
*
* Receive a Callback Event
*
*/
public function recordingEventAction(Request $request)
{
$response = new Response();
$dir = $request->get('RecordingDuration');
if ($dir > 20) {
$text = "Call Recording ($dir seconds)\n";
$text .= $request->get('RecordingUrl');
$this->send($text, true, false);
}
return $response->setContent("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}
/**
* @Route("/artefacts/dial-status-events", name="artefacts-dial-status-events")
* @param Request $request
* @param mixed $text
* @return string
*
* Receive a Callback Event
*
*/
public function dialEventAction(Request $request, $text = "Call Status Update")
{
$text .= "\n";
$response = new Response();
$data = [
'CallStatus',
'CallDuration',
'CallerName',
'Caller',
'CallerCity',
'CallerCountry',
'CallerState',
'CallerZip',
];
foreach ($data as $datum) {
if ($request->get($datum)) {
$text .= "$datum:".$request->get($datum) . "\n";
}
}
$this->send($text);
return $response->setContent("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}
/**
* @Route("/artefacts/call-status-events", name="artefacts-call-status-events")
* @param Request $request
* @return string
*
* Receive a Callback Event
*
*/
public function callEventAction(Request $request)
{
$response = new Response();
return $response->setContent("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}
/**
* @Route("/artefacts/hold-music")
* @param Request $request
* @return string
*
* Hold Music
* rick-olson_sahara-groove-studio
*
*/
public function holdMusicAction(Request $request)
{
$response = new Response();
// Send a Text and Initiate agent dialing before returning hold music to play.
$data = [
'CallStatus',
'CallerName',
'Caller',
'CallerCity',
'CallerCountry',
'CallerState',
'CallerZip',
'CurrentQueueSize',
'QueuePosition',
'QueueTime',
'AvgQueueTime',
];
$text = "Caller in Queue!\n";
foreach ($data as $datum) {
if ($request->get($datum)) {
$text .= "$datum:".$request->get($datum) . "\n";
}
}
$artefacts_sid = getenv('ARTEFACTS_SID');
$artefacts_token = getenv('ARTEFACTS_TOKEN');
$artefacts = new Client($artefacts_sid, $artefacts_token);
$users = new Users();
$who = $users->getUsers();
$A = getenv('ARTEFACTS_ACTIVE_TABLE');
$O = getenv('ARTEFACTS_ONCALL_TABLE');
$host = getenv('PLUGINS_HOST');
$activeCounter = 0;
$oncallCounter = 0;
foreach ($who as $he) {
$phone = $he[0];
$data = $he[1];
if ($data == 'artefacts-supportdesk-active') {
$isActive = $users->getUserX($phone, $A);
$isOncall = $users->getUserX($phone, $O);
$phone = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);
if (strlen($phone) == 10) {
$phone = '1' . $phone;
}
if ($isActive) {
$activeCounter++;
$artefacts->messages->create(
'+' . $phone,
array(
'from' => '+18444493911',
'body' => $text
)
);
}
if ($isOncall) {
$oncallCounter++;
$call = $artefacts->calls
->create("+" . $phone, // to
"+18444493911", // from
array("url" => "$host/artefacts/agent")
);
}
}
}
$everyone = [];
if ($oncallCounter == 0) {
foreach ($who as $he) {
$phone = $he[0];
$data = $he[1];
if ($data == 'artefacts-supportdesk-active') {
if (strlen($phone) == 10) {
$phone = '1' . $phone;
}
$everyone[] = $phone;
}
}
foreach ($everyone as $phone) {
$artefacts->messages->create(
'+' . $phone,
array(
'from' => '+18444493911',
'body' => "WARNING - NO ONE IS ON-CALL - SO I'M CALLING EVERYBODY!!\n\n" . $text
)
);
}
foreach ($everyone as $phone) {
$call = $artefacts->calls
->create("+" . $phone, // to
"+18444493911", // from
array("url" => "$host/artefacts/agent")
);
}
}
return $response->setContent('<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Play>https://chargebacks911.com/Apps/rick-olson_sahara-groove-studio-downsampled220.mp3</Play>
</Response>');
}
/**
* @Route("/artefacts/agent-hold-music")
* @param Request $request
* @return string
*
* Agent Hold Music
*/
public function agentHoldMusicAction(Request $request)
{
$response = new Response();
// http://com.artefacts.sounds.music.s3.amazonaws.com/...
// ClockworkWaltz.mp3
// BusyStrings.mp3
// Mellotroniac_-_Flight_Of_Young_Hearts_Flute.mp3
// ith_chopin-15-2.mp3
// oldDog_-_endless_goodbye_(instr.).mp3
// MARKOVICHAMP-Borghestral.mp3
return $response->setContent('<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Play>http://com.twilio.sounds.music.s3.amazonaws.com/ith_chopin-15-2.mp3</Play>
</Response>');
}
/**
* @Route("/artefacts/call", name="artefacts-call")
* @param Request $request
* @return string
*
* Receive a Call
*
*/
public function callAction(Request $request)
{
$To = $request->get('Called');
$From = $request->get('Caller');
$response = new Response();
if ((!$To) or (!$From)) {
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
return $response;
}
// Todo: Send a text to everyone who is ACTIVE
/*
* Send a text to
00000000000 Jeanette, 00000000000000 Jaclyn
* 00000000000 Others
*
*/
$text = "Incoming Call\n";
$response = new Response();
$data = [
'CallStatus',
'CallDuration',
'CallerName',
'Caller',
'CallerCity',
'CallerCountry',
'CallerState',
'CallerZip',
];
foreach ($data as $datum) {
if ($request->get($datum)) {
$text .= "$datum:".$request->get($datum) . "\n";
}
}
$this->send($text);
$user = new Users();
$onCall = $user->getUsersX(getenv('ARTEFACTS_ONCALL_TABLE'));
// Don't forget to activate group extensions and increase the timeout on US
// from 4 seconds to 20 seconds
// put back greeting
$twiml =
'<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Play>https://chargebacks911.com/Apps/artefactshotlinegreeting.mp3?x=2</Play>
<!-- include the below if you want to try ringing a receptionist before queuing to cell phones -->
<!--Dial record="record-from-ringing-dual"
recordingStatusCallback="/artefacts/recording-events"
recordingStatusCallbackEvent="completed"
method="GET">
<Sip>sip:7080@52.207.99.81;region=us1</Sip>
</Dial-->
<!--
If we get here, that means the above call ended for whatever reason.
If no office is open, this happens right away with the intention of
getting the caller to an On-Call team member.
However, the caller is just as likely to have been talking to an office
team member and this call is ending normally. So, for this reason, we
will want to have a little bit of silence so the caller can hang up.
-->
<!--Pause length="3"/-->
<Enqueue waitUrl="/artefacts/hold-music">support</Enqueue>
</Response>';
$response->setContent($twiml);
return $response;
}
/**
* @Route("/artefacts/agent")
* @param Request $request
* @return string
*
* Agent grab caller in Queue
*
*/
public function agentAction(Request $request)
{
$response = new Response();
if ($request->get('Digits')) {
return $response->setContent('<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial record="record-from-ringing-dual"
recordingStatusCallback="/artefacts/recording-events"
recordingStatusCallbackEvent="completed"
method="GET">
<Queue>support</Queue>
</Dial>
</Response>');
} else {
return $response->setContent('<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather>
<Say>I have a call for you. Please press 1 at any time to access the queue. If you hear music, then the queue is empty. You may stay on the line and if someone calls they will be transferred to you immediately.</Say>
</Gather>
<Say>Goodbye!</Say>
</Response>');
}
}
public function send($text, $Active=true, $Oncall = true, $override = false)
{
$artefacts_sid = getenv('ARTEFACTS_SID');
$artefacts_token = getenv('ARTEFACTS_TOKEN');
$artefacts = new Client($artefacts_sid, $artefacts_token);
$users = new Users();
$who = $users->getUsers();
$A = getenv('ARTEFACTS_ACTIVE_TABLE');
$O = getenv('ARTEFACTS_ONCALL_TABLE');
$cnt = 0;
foreach ($who as $he) {
$phone = $he[0];
$data = $he[1];
if ($data == 'artefacts-supportdesk-active') {
$isActive = false;
$isOncall = false;
if ($Active) {
$isActive = $users->getUserX($phone, $A);
}
if ($Oncall) {
$isOncall = $users->getUserX($phone, $O);
}
// If nobody is active, do something like email everyone
if ($isActive or $isOncall or $override) {
$cnt++;
$phone = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);
if (strlen($phone) == 10) {
$phone = '1' . $phone;
}
$artefacts->messages->create(
'+' . $phone,
array(
'from' => '+18444493911',
'body' => $text
)
);
}
}
}
return $cnt;
}
public function sendTo($text, $recipients)
{
$artefacts_sid = getenv('ARTEFACTS_SID');
$artefacts_token = getenv('ARTEFACTS_TOKEN');
$artefacts = new Client($artefacts_sid, $artefacts_token);
foreach ($recipients as $phone) {
$phone = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);
if (strlen($phone) == 10) {
$phone = '1' . $phone;
}
$artefacts->messages->create(
'+' . $phone,
array(
'from' => '+18444493911',
'body' => $text
)
);
}
}
}