add("ACLU", __DIR__ . '/../lib/'); // ACLU Resources use ACLU\Services\Capwiz; use ACLU\Services\Drupal; use ACLU\Util\Util; use Sendgrid\Email; $app = new \Slim\Slim(); // Parameters for static image that will be shared $userOptions = Util::validateUserOptions($_GET); $fileName = Util::dataToFilename($userOptions, false); // pass the current URL as a variable to the template //$app->render(array('current_url' => $current_url)); // Configure Slim $app->config(array( 'templates.path' => '../templates' )); // Prepare twig templating $app->view(new \Slim\Views\Twig()); // Twig options $app->view->parserOptions = array( 'charset' => 'utf-8', 'cache' => realpath('../templates/cache'), 'auto_reload' => true, 'strict_variables' => false, 'autoescape' => true ); $app->view->parserExtensions = array( new \Slim\Views\TwigExtension(), ); $twig = $app->view->getEnvironment(); // Twig globals $twig->addGlobal("assetsRoot", WEB_PATH . 'assets/'); //$twig->addGlobal("assetsRoot", '/assets/'); $twig->addGlobal("webRoot", WEB_PATH); $twig->addGlobal("shareUrl", 'https://theuncovery.org/'.$fileName); /* * Step 1. Race */ $app->get('/', function () use ($app) { $raceData = file_get_contents('../data/data.json'); $app->render('statement-1.html', array('statsJSON' => $raceData)); })->name('statement-1'); /* * Step 2. Money */ $app->get('/money-spent/', function () use ($app) { $parameters = $app->request()->get(); $data = Util::validateUserOptions($parameters); $data['moneyStats'] = file_get_contents('../data/data.json'); $app->render('statement-2.html', $data); })->name('statement-2'); /* * Step 3. Act */ $app->get('/act/', function () use ($app) { $parameters = $app->request()->get(); $data = Util::validateUserOptions($parameters); $stats = Util::calculateStats('../data/data.json', $data); $data = array_merge($stats, $data); Util::makeEmailBody($data); $app->render('act.html', $data); })->name('act'); $app->get('/facts/', function () use ($app) { $raceData = file_get_contents('../data/data.json'); $app->render('facts.html', array('statsJSON' => $raceData)); })->name('facts'); $app->get('/fact/', function () use ($app) { $raceData = file_get_contents('../data/data.json'); $app->render('fact.html', array('statsJSON' => $raceData)); })->name('fact'); $app->get('/the-story/', function () use ($app) { $raceData = file_get_contents('../data/data.json'); $app->render('the-story.html', array('statsJSON' => $raceData)); })->name('the-story'); /* * Contact official via capwiz */ $app->post('/capwiz-api/', function () use ($app) { $req = $app->request(); $data = $req->post(); $address = Util::array_whitelist($data, array('addr1','city','state','zip')); // Query for representative $capwiz = new Capwiz(CAPWIZ_KEY, CAPWIZ_PASSWORD); $data['officials'] = $capwiz->requestAndParseRepresentatives($address); // Prepare email $userOptions = Util::validateUserOptions($data); $stats = Util::calculateStats('../data/data.json', $userOptions); $userOptions = array_merge($stats, $userOptions); $emailBody = Util::makeEmailBodyPlain($userOptions); $emailSubject = Util::makeEmailSubject($userOptions); $data['body'] = $emailBody; $data['subject'] = $emailSubject; $data['template_id'] = CAPWIZ_MESSAGE_TEMPLATE_ID; $data['alert_id'] = CAPWIZ_ALERT_ID; $response = Util::prepareStateMessageObject($data); $response = $capwiz->sendMessage($response, CAPWIZ_CLIENT_ID); $app->contentType('application/json'); echo $response; // Prepare data for Springboard/Drupal API $node_id = '50062'; $campaign_id = '701a0000000j1Gg'; $ref = $req->getReferrer(); $ms_path = 'https://theuncovery.org/act/'; $ms_title = 'Theuncovery Signup Form'; $faf = 'NAT'; $market = 'web_theuncovery'; $timestamp = time(); $datagen = array(); $datagen['resource'] = 'submit'; $datagen['timestamp'] = (int) trim($timestamp); $datagen['pubkey'] = trim(ACLU_PUBLIC_KEY); $datagen['privkey'] = trim(ACLU_PRIVATE_KEY); $gen_checksum = sha1(json_encode($datagen)); $fields = array( 'key' => $datagen['pubkey'], 'time' => $timestamp, 'checksum' => $gen_checksum, 'webform_id' => $node_id, 'mail' => $data['email'], 'profile_zip' => $data['zip'], 'fight_for_freedom' => '1', 'cid' => $campaign_id, 'ms' => $market, 'referrer' => $ref, 'ms_title' => $ms_title, 'ms_nid' => $node_id, 'ms_path' => $ms_path, 'Form_Affiliation' => $faf, 'profile_first_name' => $data['first_name'], 'profile_last_name' => $data['last_name'], 'campaign_member_status' => 'Sent' ); $fields_string = array(); $fields_string = http_build_query($fields); $response2 = $capwiz->sendToDrupal($fields_string); echo $response2; })->name('capwiz-api'); /* * Share with friend via sendgrid */ $app->post('/share-email/', function () use ($app) { $req = $app->request(); $data = $req->post(); $userOptions = Util::validateUserOptions($data); $stats = Util::calculateStats('../data/data.json', $userOptions); $userOptions = array_merge($stats, $userOptions); $userOptions['url'] = 'http://aclu.dev'; $data['body'] = Util::makeEmailBody($userOptions); $data['subject'] = Util::makeEmailSubject($userOptions, array('WA','CO')); $sendgrid = new SendGrid(SENDGRID_USER, SENDGRID_PASSWORD); $email = new SendGrid\Email(); $fullName = $data['first_name'] . ' ' . $data['last_name']; $attachment = POSTCARD_PATH.'/'.Util::dataToFilename($userOptions); $email->addTo($data['to_email']) ->setFrom($data['from_email']) ->setFromName($fullName) ->setSubject($data['subject']) ->setHtml($data['body']) ->addAttachment($attachment); if (isset($data['copy_to_self']) && $data['copy_to_self'] == 'yes') { $email->addTo($data['from_email']); } if(SENDGRID_DELIVERY_METHOD == "smtp") { $res = $sendgrid->smtp->send($email); } else if(SENDGRID_DELIVERY_METHOD == "web") { $res = $sendgrid->web->send($email); } else { throw new Exception('Sendgrid Delivery Method Missing or not Valid'); } $app->contentType('application/json'); echo json_encode($userOptions); })->name('share-email'); /* * Info about the data */ $app->get('/the-data/', function () use ($app) { $app->render('the-data.html'); })->name('data'); /* * Dynamic image endpoint */ $app->get('/:img/', function ($fileName) use ($app) { $webPath = WEB_PATH . 'postcard/combine/'.$fileName.'.png'; $filePath = './postcard/combine/'.$fileName.'.png'; if (!file_exists($filePath)) { return $app->notFound(); } $app->render('static-statement.html', array('image' => $webPath)); }); $app->notFound(function () use ($app) { $app->render('404.html'); }); $app->run();