<?php
// src/Helper/Helpers.php
namespace App\Helper;
use App\Entity\Ticket;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use DateInterval;
/**
*
*/
class Helpers
{
public static $password = '3sc3RLrpd17';
public static $method = 'aes-256-cbc';
public static function encrypt($value = '')
{
// Must be exact 32 chars (256 bit)
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
$password = substr(hash('sha256', self::$password, true), 0, 32);
// IV must be exact 16 chars (128 bit)
// av3DYGLkwBsErphcyYp+imUW4QKs19hUnFyyYcXwURU=
$encrypted = urlencode(base64_encode(openssl_encrypt($value, self::$method, $password, OPENSSL_RAW_DATA, $iv)));
return $encrypted;
}
public static function decrypt($value = '')
{
// Must be exact 32 chars (256 bit)
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
$password = substr(hash('sha256', self::$password, true), 0, 32);
// My secret message 1234
$decrypted = urldecode(openssl_decrypt(base64_decode($value), self::$method, $password, OPENSSL_RAW_DATA, $iv));
return $decrypted;
}
public static function checkCrendentials($role, $event = '', $user, AuthorizationChecker $security)
{
$ret = false;
if ($security->isGranted($role) && ($user->getEvents()->contains($event) || $event == '')) {
$ret = true;
}
return $ret;
}
public static function checkTicket(Ticket $ticket)
{
date_default_timezone_set('Europe/Madrid');
$last_used = $ticket->getUsed();
$entrada = $ticket->getEntradaType();
$result = "ok";
$now = new \DateTime();
$startToday = new \DateTime();
$startToday->setTime(0, 0);
$beforeStartDate = clone $entrada->getStartDate();
$interval = new DateInterval("PT30M");
$beforeStartDate->sub($interval);
if ($last_used) {
$result = "used";
if ($last_used < $startToday && $entrada->getDaily()) {
if ($beforeStartDate < $now && $beforeStartDate > $now) {
$result = 'ok';
} else {
$result = 'outatime';
}
}
} else {
if ($beforeStartDate < $now) {
if ($entrada->getEndDate() > $now) {
$result = 'ok';
} else {
$result = 'outatime';
}
} else {
$result = 'time_early';
}
}
return $result;
}
public static function getServerTime()
{
return (new \DateTime())->format(\DateTimeInterface::ISO8601);
}
public static function sendInvoice($tickets = [], $bundle = '', $doctrine = null)
{
$entityManager = $doctrine->getManager();
$repository = $doctrine->getRepository(Ticket::class);
$tickets = $repository->findByBundle($bundle);
$ticket = $tickets[0];
$buyer = $ticket->getBuyer();
$email = $buyer->getEmail();
$name = $buyer->getName();
$surname = $buyer->getSurname();
$buydate = $ticket->getBuyDate()->format('d/m/Y');
$total = 0;
$pdf = Helpers::createInvoicePDF($tickets);
$pdfdoc = $pdf->Output($bundle . ".pdf", "S");
$separator = md5(time());
$eol = PHP_EOL;
$filename = "./pdf/" . $bundle . ".pdf";
$to = $email;
$from = 'admin@entradasytickets.com';
$subject = 'Recibo de EntradasyTickets';
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: " . $from . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"";
$message = file_get_contents('./partials/invoice1.html');
$message .= "<br/>" . $name . " " . $surname;
$message .= "<br/>" . "<p style='color: #777777'>" . $email . "<p>";
$message .= "<br/>";
$message .= file_get_contents('./partials/invoice2.html');
$message .= "<br/>" . $buydate;
$message .= "<br/>";
$message .= "<br/>";
$message .= '<span class="header-sm">Orden</span><br/>' . $bundle;
$message .= file_get_contents('./partials/invoice3.html');
foreach ($tickets as $ticket) {
$nombreEvento = $ticket->getEntradaType()->getEvent()->getName();
$nombreEntrada = $ticket->getEntradaType()->getName();
$identifier = $ticket->getIdentifier();
$total += $ticket->getEntradaType()->getPrice();
$message .= '<tr>
<td class="item-col item">
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td class="mobile-hide-img">
<a href=""><img width="200" height="200" src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=' . $identifier . '&choe=UTF-8" alt="item1"></a>
</td>
<td class="product">
<span style="color: #4d4d4d; font-weight:bold;">' . $nombreEntrada . '</span>
<br /> <p style="color: #777777">' . $nombreEvento . '<p>
<br /> ' . $identifier . '
</td>
</tr>
</table>
</td>
<td class="item-col quantity">
1
</td>
<td class="item-col">
' . $ticket->getEntradaType()->getPrice() . '€
</td>
</tr>';
}
$message .= file_get_contents('./partials/invoice4.html');
$message .= '<tr>
<td class="item-col item">
</td>
<td class="item-col quantity" style="text-align:right; padding-right: 10px; border-top: 1px solid #cccccc;">
<br />
<span class="total-space" style="font-weight: bold; color: #4d4d4d">Total</span>
</td>
<td class="item-col price" style="text-align: left; border-top: 1px solid #cccccc;">
<br />
<span class="total-space" style="font-weight:bold; color: #4d4d4d">' . $total . '€</span>
<br />
</td>
</tr>';
$message .= file_get_contents('./partials/invoice5.html');
$body = "";
// message
$body .= "--" . $separator . $eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $message . $eol;
// message
$body .= "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= "*Este mensaje se envia con HTML si no lo ves bien por favor configure su navegador" . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol . $eol;
$body .= $attachment . $eol;
$body .= "--" . $separator . "--";
mail($to, $subject, $body, $headers);
// unlink($filename);
}
public static function createInvoicePDF($tickets = [])
{
$user = $tickets[0]->getBuyer();
$bundle = $tickets[0]->getBundle();
$pdf = new TCPDF();
foreach ($tickets as $ticket) {
$entrada = $ticket->getEntradaType();
$event = $entrada->getEvent();
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$html = $this->renderView('buy_ticket/pdf_invoice.html.twig', [
'ticket' => $ticket,
'entrada' => $entrada,
'event' => $event,
]);
$ticketURL = '<img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=' . $ticket->getIdentifier() . '&choe=UTF-8" width="300" height="300">';
$html = str_replace('%qr%', $ticketURL, $html);
$pdf->writeHTML($html, true, false, true, false, '');
}
return $pdf;
}
}