Example #1
0
 public function mainAction()
 {
     // inicialize supporting classes
     $connection = new Connection();
     $email = new Email();
     $service = new Service();
     $service->showAds = false;
     $render = new Render();
     $response = new Response();
     $utils = new Utils();
     $wwwroot = $this->di->get('path')['root'];
     // get valid people
     $people = $connection->deepQuery("\n\t\t\tSELECT email, username, first_name, last_access\n\t\t\tFROM person\n\t\t\tWHERE active=1\n\t\t\tAND email not in (SELECT DISTINCT email FROM delivery_dropped)\n\t\t\tAND DATE(last_access) > DATE('2016-05-01')\n\t\t\tAND email like '%.cu'\n\t\t\tAND email not like '*****@*****.**'");
     // send the remarketing
     $log = "";
     foreach ($people as $person) {
         // get the email address
         $newEmail = "apretaste+{$person->username}@gmail.com";
         // create the variabels to pass to the template
         $content = array("newemail" => $newEmail, "name" => $person->first_name);
         // create html response
         $response->setEmailLayout("email_simple.tpl");
         $response->createFromTemplate('newEmail.tpl', $content);
         $response->internal = true;
         $html = $render->renderHTML($service, $response);
         // send the email
         $email->sendEmail($person->email, "Sorteando las dificultades, un email lleno de alegria", $html);
         $log .= $person->email . "\n";
     }
     // saving the log
     $logger = new \Phalcon\Logger\Adapter\File("{$wwwroot}/logs/newemail.log");
     $logger->log($log);
     $logger->close();
 }
Example #2
0
 /**
  * Process the page when its submitted
  *
  * @author kuma, salvipascual
  * @version 1.0
  * */
 public function processAction()
 {
     // get the values from the post
     $captcha = trim($this->request->getPost('captcha'));
     $name = trim($this->request->getPost('name'));
     $inviter = trim($this->request->getPost('email'));
     $guest = trim($this->request->getPost('guest'));
     if (!isset($_SESSION['phrase'])) {
         $_SESSION['phrase'] = uniqid();
     }
     // throw a die()
     // check all values passed are valid
     if (strtoupper($captcha) != strtoupper($_SESSION['phrase']) || $name == "" || !filter_var($inviter, FILTER_VALIDATE_EMAIL) || !filter_var($guest, FILTER_VALIDATE_EMAIL)) {
         die("Error procesando, por favor valla atras y comience nuevamente.");
     }
     // params for the response
     $this->view->name = $name;
     $this->view->email = $inviter;
     // create classes needed
     $connection = new Connection();
     $email = new Email();
     $utils = new Utils();
     $render = new Render();
     // do not invite people who are already using Apretaste
     if ($utils->personExist($guest)) {
         $this->view->already = true;
         return $this->dispatcher->forward(array("controller" => "invitar", "action" => "index"));
     }
     // send notification to the inviter
     $response = new Response();
     $response->setResponseSubject("Gracias por darle internet a un Cubano");
     $response->setEmailLayout("email_simple.tpl");
     $response->createFromTemplate("invitationThankYou.tpl", array('num_notifications' => 0));
     $response->internal = true;
     $html = $render->renderHTML(new Service(), $response);
     $email->sendEmail($inviter, $response->subject, $html);
     // send invitations to the guest
     $response = new Response();
     $response->setResponseSubject("{$name} le ha invitado a revisar internet desde su email");
     $responseContent = array("host" => $name, "guest" => $guest, 'num_notifications' => 0);
     $response->createFromTemplate("invitation.tpl", $responseContent);
     $response->internal = true;
     $html = $render->renderHTML(new Service(), $response);
     $email->sendEmail($guest, $response->subject, $html);
     // save all the invitations into the database at the same time
     $connection->deepQuery("INSERT INTO invitations (email_inviter,email_invited,source) VALUES ('{$inviter}','{$guest}','abroad')");
     // redirect to the invite page
     $this->view->message = true;
     return $this->dispatcher->forward(array("controller" => "invitar", "action" => "index"));
 }
Example #3
0
 /**
  * Recovers a pin and create a pin for users with blank pins
  *
  * @author salvipascual
  * @param GET email
  * @return JSON
  * */
 public function recoverAction()
 {
     $email = trim($this->request->get('email'));
     $utils = new Utils();
     $connection = new Connection();
     // check if the email exist
     if (!$utils->personExist($email)) {
         die('{"code":"error","message":"invalid user"}');
     }
     // get pin from the user
     $pin = $connection->deepQuery("SELECT pin FROM person WHERE email='{$email}'");
     $pin = $pin[0]->pin;
     // if pin is blank, create it
     if (empty($pin)) {
         $pin = mt_rand(1000, 9999);
         $connection->deepQuery("UPDATE person SET pin='{$pin}' WHERE email='{$email}'");
     }
     // create response to email the new code
     $subject = "Su codigo de Apretaste";
     $response = new Response();
     $response->setEmailLayout("email_simple.tpl");
     $response->setResponseSubject($subject);
     $response->createFromTemplate("pinrecover.tpl", array("pin" => $pin));
     $response->internal = true;
     // render the template as html
     $render = new Render();
     $body = $render->renderHTML(new Service(), $response);
     // email the code to the user
     $emailSender = new Email();
     $emailSender->sendEmail($email, $subject, $body);
     // return ok response
     die('{"code":"ok"}');
 }
Example #4
0
 /**
  * Subservice PUBLICAR
  *
  * @param Request $request        	
  */
 public function _publicar($request)
 {
     $connection = new Connection();
     $title = substr(trim($request->query), 0, 100);
     $body = substr(trim($request->body), 0, 1000);
     if ($title == '') {
         $title = substr($body, 0, 100);
     }
     $title = $connection->escape($title);
     $body = $connection->escape($body);
     $title = str_replace("'", '\\' . "'", $title);
     $body = str_replace("'", '\\' . "'", $body);
     $hash = $this->utils->generateRandomHash();
     $di = \Phalcon\DI\FactoryDefault::getDefault();
     $wwwroot = $di->get('path')['root'];
     // insert new ad with a year of life
     $connection->deepQuery("INSERT INTO ads (title,description,owner,expiration_date) VALUES ('{$title}','{$body}','{$request->email}',DATE_ADD(CURRENT_DATE, INTERVAL 1 YEAR));");
     // get id of the new ad inserted
     $id = $connection->deepQuery("SELECT id FROM ads WHERE owner = '{$request->email}' ORDER BY time_inserted DESC LIMIT 100;");
     $id = $id[0]->id;
     // insert one image for the ad
     foreach ($request->attachments as $at) {
         if (isset($at->type) && strpos("jpg,jpeg,image/jpg,image/jpeg,image/png,png,image/gif,gif", $at->type) !== false && isset($at->path)) {
             // save the image
             $img = file_get_contents($at->path);
             $filePath = "{$wwwroot}/public/ads/" . md5($id) . ".jpg";
             file_put_contents($filePath, $img);
             // optimize the image
             $this->utils->optimizeImage($filePath);
             // only first image
             break;
         }
     }
     // respond to the owner of the ad
     $response = new Response();
     $response->setResponseSubject("Su anuncio ha sido agregado");
     $response->createFromTemplate('publish.tpl', array('id' => $id, 'userEmail' => $request->email));
     // alert us about the new ad
     $alert = new Response();
     $alert->setResponseEmail("*****@*****.**");
     $alert->setEmailLayout("email_simple.tpl");
     $alert->setResponseSubject('Nueva publicidad en Apretaste');
     $alert->createFromTemplate('notify.tpl', array('owner' => $request->email, 'title' => $title, 'body' => $body));
     return array($response, $alert);
 }
Example #5
0
 /**
  * Respond to a request based on the parameters passed
  *
  * @author salvipascual
  * @param String, email
  * @param String
  * @param String, email
  * @param String
  * @param Array of Objects {type,content,path}
  * @param Enum: html,json,email
  * @param String, email
  * @param String $messageID
  * */
 private function renderResponse($email, $fromEmail, $subject, $sender = "", $body = "", $attachments = array(), $format = "html", $messageID = NULL)
 {
     // get the time when the service started executing
     $execStartTime = date("Y-m-d H:i:s");
     // remove double spaces and apostrophes from the subject
     // sorry apostrophes break the SQL code :-(
     $subject = trim(preg_replace('/\\s{2,}/', " ", preg_replace('/\'|`/', "", $subject)));
     // get the name of the service based on the subject line
     $subjectPieces = explode(" ", $subject);
     $serviceName = strtolower($subjectPieces[0]);
     unset($subjectPieces[0]);
     // check the service requested actually exists
     $utils = new Utils();
     $connection = new Connection();
     // select the default service if service does not exist
     $alias = $serviceName;
     if (!$utils->serviceExist($serviceName)) {
         $serviceName = $utils->getDefaultService($fromEmail);
     } else {
         if ($serviceName !== $alias) {
             // increase the counter for alias
             $connection->deepQuery("UPDATE service_alias SET used = used + 1 WHERE alias = '{$alias}';");
         }
     }
     // update topics if you are contacting via the secure API
     if ($serviceName == "secured") {
         // disregard any footer message and decript new subject
         $message = trim(explode("--", $body)[0]);
         $subject = $utils->decript($email, $message);
         // get the name of the service based on the subject line
         $subjectPieces = explode(" ", $subject);
         $serviceName = strtolower($subjectPieces[0]);
         unset($subjectPieces[0]);
         // if the service don't exist, throw an error and exit
         if (!$utils->serviceExist($serviceName)) {
             error_log("Service {$serviceName} do not exist");
             exit;
         }
     }
     // include the service code
     $wwwroot = $this->di->get('path')['root'];
     include "{$wwwroot}/services/{$serviceName}/service.php";
     // check if a subservice is been invoked
     $subServiceName = "";
     if (isset($subjectPieces[1]) && !preg_match('/\\?|\\(|\\)|\\\\|\\/|\\.|\\$|\\^|\\{|\\}|\\||\\!/', $subjectPieces[1])) {
         $serviceClassMethods = get_class_methods($serviceName);
         if (preg_grep("/^_{$subjectPieces[1]}\$/i", $serviceClassMethods)) {
             $subServiceName = strtolower($subjectPieces[1]);
             unset($subjectPieces[1]);
         }
     }
     // get the service query
     $query = implode(" ", $subjectPieces);
     // create a new Request object
     $request = new Request();
     $request->email = $email;
     $request->name = $sender;
     $request->subject = $subject;
     $request->body = $body;
     $request->attachments = $attachments;
     $request->service = $serviceName;
     $request->subservice = trim($subServiceName);
     $request->query = trim($query);
     // get the path to the service
     $servicePath = $utils->getPathToService($serviceName);
     // get details of the service
     if ($this->di->get('environment') == "sandbox") {
         // get details of the service from the XML file
         $xml = simplexml_load_file("{$servicePath}/config.xml");
         $serviceCreatorEmail = trim((string) $xml->creatorEmail);
         $serviceDescription = trim((string) $xml->serviceDescription);
         $serviceCategory = trim((string) $xml->serviceCategory);
         $serviceUsageText = trim((string) $xml->serviceUsage);
         $showAds = isset($xml->showAds) && $xml->showAds == 0 ? 0 : 1;
         $serviceInsertionDate = date("Y/m/d H:m:s");
     } else {
         // get details of the service from the database
         $sql = "SELECT * FROM service WHERE name = '{$serviceName}'";
         $result = $connection->deepQuery($sql);
         $serviceCreatorEmail = $result[0]->creator_email;
         $serviceDescription = $result[0]->description;
         $serviceCategory = $result[0]->category;
         $serviceUsageText = $result[0]->usage_text;
         $serviceInsertionDate = $result[0]->insertion_date;
         $showAds = $result[0]->ads == 1;
     }
     // create a new service Object of the user type
     $userService = new $serviceName();
     $userService->serviceName = $serviceName;
     $userService->serviceDescription = $serviceDescription;
     $userService->creatorEmail = $serviceCreatorEmail;
     $userService->serviceCategory = $serviceCategory;
     $userService->serviceUsage = $serviceUsageText;
     $userService->insertionDate = $serviceInsertionDate;
     $userService->pathToService = $servicePath;
     $userService->showAds = $showAds;
     $userService->utils = $utils;
     // run the service and get a response
     if (empty($subServiceName)) {
         $response = $userService->_main($request);
     } else {
         $subserviceFunction = "_{$subServiceName}";
         $response = $userService->{$subserviceFunction}($request);
     }
     // a service can return an array of Response or only one.
     // we always treat the response as an array
     $responses = is_array($response) ? $response : array($response);
     // adding extra responses from Utils
     $extraResponses = Utils::getExtraResponses();
     $responses = array_merge($responses, $extraResponses);
     Utils::clearExtraResponses();
     // clean the empty fields in the response
     foreach ($responses as $rs) {
         $rs->email = empty($rs->email) ? $email : $rs->email;
         // check if is first request of the day
         $requestsToday = $utils->getTotalRequestsTodayOf($rs->email);
         $stars = 0;
         if ($requestsToday == 0) {
             // run the tickets's game
             // @note: este chequeo se hace despues de verificar si es el primer
             // correo del dia, para no preguntar chequear mas veces
             // innecesariamente en el resto del dia
             $stars = $utils->getRaffleStarsOf($rs->email, false);
             if ($stars === 4) {
                 // insert 10 tickets for user
                 $sqlValues = "('{$email}', 'GAME')";
                 $sql = "INSERT INTO ticket(email, origin) VALUES " . str_repeat($sqlValues . ",", 9) . "{$sqlValues};";
                 $connection->deepQuery($sql);
                 // add notification to user
                 $utils->addNotification($rs->email, "GAME", "Haz ganado 10 tickets para Rifa por utilizar Apretaste durante 5 días seguidos", "RIFA", "IMPORTANT");
             }
             $stars++;
         }
         $rs->subject = empty($rs->subject) ? "Respuesta del servicio {$serviceName}" : $rs->subject;
         $rs->content['num_notifications'] = $utils->getNumberOfNotifications($rs->email);
         $rs->content['raffle_stars'] = $stars;
         $rs->content['requests_today'] = $requestsToday;
     }
     // create a new render
     $render = new Render();
     // render the template and echo on the screen
     if ($format == "html") {
         $html = "";
         for ($i = 0; $i < count($responses); $i++) {
             $html .= "<br/><center><small><b>To:</b> " . $responses[$i]->email . ". <b>Subject:</b> " . $responses[$i]->subject . "</small></center><br/>";
             $html .= $render->renderHTML($userService, $responses[$i]);
             if ($i < count($responses) - 1) {
                 $html .= "<br/><hr/><br/>";
             }
         }
         $usage = nl2br(str_replace('{APRETASTE_EMAIL}', $utils->getValidEmailAddress(), $serviceUsageText));
         $html .= "<br/><hr><center><p><b>XML DEBUG</b></p><small>";
         $html .= "<p><b>Owner: </b>{$serviceCreatorEmail}</p>";
         $html .= "<p><b>Category: </b>{$serviceCategory}</p>";
         $html .= "<p><b>Description: </b>{$serviceDescription}</p>";
         $html .= "<p><b>Usage: </b><br/>{$usage}</p></small></center>";
         return $html;
     }
     // echo the json on the screen
     if ($format == "json") {
         return $render->renderJSON($response);
     }
     // render the template email it to the user
     // only save stadistics for email requests
     if ($format == "email") {
         // get the person, false if the person does not exist
         $person = $utils->getPerson($email);
         // if the person exist in Apretaste
         if ($person !== false) {
             // update last access time to current and make person active
             $connection->deepQuery("UPDATE person SET active=1, last_access=CURRENT_TIMESTAMP WHERE email='{$email}'");
         } else {
             $inviteSource = 'alone';
             // alone if the user came by himself, no invitation
             $sql = "START TRANSACTION;";
             // start the long query
             // check if the person was invited to Apretaste
             $invites = $connection->deepQuery("SELECT * FROM invitations WHERE email_invited='{$email}' AND used=0 ORDER BY invitation_time DESC");
             if (count($invites) > 0) {
                 // check how this user came to know Apretaste, for stadistics
                 $inviteSource = $invites[0]->source;
                 // give prizes to the invitations via service invitar
                 // if more than one person invites X, they all get prizes
                 foreach ($invites as $invite) {
                     switch ($invite->source) {
                         case "internal":
                             // assign tickets and credits
                             $sql .= "INSERT INTO ticket (email, origin) VALUES ('{$invite->email_inviter}', 'RAFFLE');";
                             $sql .= "UPDATE person SET credit=credit+0.25 WHERE email='{$invite->email_inviter}';";
                             // email the invitor
                             $newTicket = new Response();
                             $newTicket->setResponseEmail($invite->email_inviter);
                             $newTicket->setEmailLayout("email_simple.tpl");
                             $newTicket->setResponseSubject("Ha ganado un ticket para nuestra Rifa");
                             $newTicket->createFromTemplate("invitationWonTicket.tpl", array("guest" => $email));
                             $newTicket->internal = true;
                             $responses[] = $newTicket;
                             break;
                         case "abroad":
                             $newGuest = new Response();
                             $newGuest->setResponseEmail($invite->email_inviter);
                             $newGuest->setResponseSubject("Tu amigo ha atendido tu invitacion");
                             $inviter = $utils->usernameFromEmail($invite->email_inviter);
                             $pInviter = $utils->getPerson($invite->email_inviter);
                             if (!isset($pInviter->name)) {
                                 $pInviter->name = '';
                             }
                             if ($pInviter !== false) {
                                 if (trim($pInviter->name) !== '') {
                                     $inviter = $pInviter->name;
                                 }
                             }
                             $pGuest = $utils->getPerson($email);
                             $guest = $email;
                             if ($pGuest !== false) {
                                 $guest = $pGuest->username;
                             }
                             $newGuest->createFromTemplate("invitationNewGuest.tpl", array("inviter" => $inviter, "guest" => $guest, "guest_email" => $email));
                             $newGuest->internal = true;
                             $responses[] = $newGuest;
                             break;
                     }
                 }
                 // mark all opened invitations to that email as used
                 $sql .= "UPDATE invitations SET used=1, used_time=CURRENT_TIMESTAMP WHERE email_invited='{$email}' AND used=0;";
             }
             // create a unique username and save the new person
             $username = $utils->usernameFromEmail($email);
             $sql .= "INSERT INTO person (email, username, last_access, source) VALUES ('{$email}', '{$username}', CURRENT_TIMESTAMP, '{$inviteSource}');";
             // save details of first visit
             $sql .= "INSERT INTO first_timers (email, source) VALUES ('{$email}', '{$fromEmail}');";
             // check list of promotor's emails
             $promoters = $connection->deepQuery("SELECT email FROM promoters WHERE email='{$fromEmail}' AND active=1;");
             $prize = count($promoters) > 0;
             if ($prize) {
                 // update the promotor
                 $sql .= "UPDATE promoters SET `usage`=`usage`+1, last_usage=CURRENT_TIMESTAMP WHERE email='{$fromEmail}';";
                 // add credit and tickets
                 $sql .= "UPDATE person SET credit=credit+5, source='promoter' WHERE email='{$email}';";
                 $sqlValues = "('{$email}', 'PROMOTER')";
                 $sql .= "INSERT INTO ticket(email, origin) VALUES " . str_repeat($sqlValues . ",", 9) . "{$sqlValues};";
             }
             // run the long query all at the same time
             $connection->deepQuery($sql . "COMMIT;");
             // send the welcome email
             $welcome = new Response();
             $welcome->setResponseEmail($email);
             $welcome->setEmailLayout("email_simple.tpl");
             $welcome->setResponseSubject("Bienvenido a Apretaste!");
             $welcome->createFromTemplate("welcome.tpl", array("email" => $email, "prize" => $prize, "source" => $fromEmail));
             $welcome->internal = true;
             $responses[] = $welcome;
         }
         // create and configure to send email
         $emailSender = new Email();
         $emailSender->setRespondEmailID($messageID);
         $emailSender->setEmailGroup($fromEmail);
         // get params for the email and send the response emails
         foreach ($responses as $rs) {
             if ($rs->render) {
                 // save impressions in the database
                 $ads = $rs->getAds();
                 if ($userService->showAds && !empty($ads)) {
                     $sql = "";
                     if (!empty($ads[0])) {
                         $sql .= "UPDATE ads SET impresions=impresions+1 WHERE id='{$ads[0]->id}';";
                     }
                     if (!empty($ads[1])) {
                         $sql .= "UPDATE ads SET impresions=impresions+1 WHERE id='{$ads[1]->id}';";
                     }
                     $connection->deepQuery($sql);
                 }
                 // prepare the email variable
                 $emailTo = $rs->email;
                 $subject = $rs->subject;
                 $images = $rs->images;
                 $attachments = $rs->attachments;
                 $body = $render->renderHTML($userService, $rs);
                 // remove dangerous characters that may break the SQL code
                 $subject = trim(preg_replace('/\'|`/', "", $subject));
                 // send the response email
                 $emailSender->sendEmail($emailTo, $subject, $body, $images, $attachments);
             }
         }
         // saves the openning date if the person comes from remarketing
         $connection->deepQuery("UPDATE remarketing SET opened=CURRENT_TIMESTAMP WHERE opened IS NULL AND email='{$email}'");
         // calculate execution time when the service stopped executing
         $currentTime = new DateTime();
         $startedTime = new DateTime($execStartTime);
         $executionTime = $currentTime->diff($startedTime)->format('%H:%I:%S');
         // get the user email domainEmail
         $emailPieces = explode("@", $email);
         $domain = $emailPieces[1];
         // get the top and bottom Ads
         $ads = isset($responses[0]->ads) ? $responses[0]->ads : array();
         $adTop = isset($ads[0]) ? $ads[0]->id : "NULL";
         $adBottom = isset($ads[1]) ? $ads[1]->id : "NULL";
         // save the logs on the utilization table
         $safeQuery = $connection->escape($query);
         $sql = "INSERT INTO utilization\t(service, subservice, query, requestor, request_time, response_time, domain, ad_top, ad_bottom) VALUES ('{$serviceName}','{$subServiceName}','{$safeQuery}','{$email}','{$execStartTime}','{$executionTime}','{$domain}',{$adTop},{$adBottom})";
         $connection->deepQuery($sql);
         // return positive answer to prove the email was quequed
         return true;
     }
     // false if no action could be taken
     return false;
 }
Example #6
0
 /**
  * Insert a notification in the database
  *
  * @author kuma
  * @param string $email
  * @param string $origin
  * @param string $text
  * @param string $link
  * @param string $tag
  * @return integer
  */
 public function addNotification($email, $origin, $text, $link = '', $tag = 'INFO')
 {
     $connection = new Connection();
     $notifications = $this->getNumberOfNotifications($email);
     // insert notification
     $sql = "INSERT INTO notifications (email, origin, text, link, tag) VALUES ('{$email}','{$origin}','{$text}','{$link}','{$tag}');";
     $connection->deepQuery($sql);
     // get notification id
     $id = false;
     $r = $connection->deepQuery("SELECT LAST_INSERT_ID() as id;");
     if (isset($r[0]->id)) {
         $id = intval($r[0]->id);
     }
     // increase number of notifications
     $sql = "UPDATE person SET notifications = notifications + 1 WHERE email = '{$email}';";
     $connection->deepQuery($sql);
     // If more than 50 notifications, send the notifications to the user
     if ($notifications + 1 >= 50) {
         // getting notifications
         $sql = "SELECT * FROM notifications WHERE email ='{$email}' AND viewed = 0 ORDER BY inserted_date DESC;";
         $notificationsList = $connection->deepQuery($sql);
         if (!is_array($notificationsList)) {
             $notificationsList = array();
         }
         // create extra response
         $response = new Response();
         $response->setEmailLayout('email_default.tpl');
         $response->setResponseSubject("Tienes {$notifications} notificaciones sin leer");
         $response->createFromTemplate("notifications.tpl", array('notificactions' => $notificationsList));
         $response->internal = true;
         self::addExtraResponse($response);
         // Mark as seen
         $connection->deepQuery("UPDATE notifications SET viewed = 1, viewed_date = CURRENT_TIMESTAMP WHERE email ='{$email}'");
         // down to zero
         $connection->deepQuery("UPDATE person SET notifications = 0 WHERE email = '{$email}';");
     }
     return $id;
 }