<?php
namespace App\Utils;
use App\Utils\Ticketing;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
class Functions extends AbstractController
{
protected $requestStack;
/**
* @var Ticketing
*/
private $ticketing;
public function __construct(RequestStack $requestStack, Ticketing $ticketing)
{
$this->requestStack = $requestStack;
$this->ticketing = $ticketing;
}
public function checkCaptcha($captchaResponse)
{
$secret = $this->getParameter('captcha_secret');
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captchaResponse."&remoteip=".$_SERVER['REMOTE_ADDR']);
$obj = json_decode($response);
return $obj->success;
}
public function getUserLogged(){
$session = $this->requestStack->getCurrentRequest()->getSession();
if($session->get('userId')){
return $session->get('userId');
}
return false;
}
public function rsa_sha1_sign($policy, $private_key_filename) {
$signature = "";
// load the private key
$fp = fopen($private_key_filename, "r");
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key);
// compute signature
openssl_sign($policy, $signature, $pkeyid);
// free the key from memory
openssl_free_key($pkeyid);
return $signature;
}
public function url_safe_base64_encode($value) {
$encoded = base64_encode($value);
// replace unsafe characters +, = and / with the safe characters -, _ and ~
return str_replace(
array('+', '=', '/'),
array('-', '_', '~'),
$encoded);
}
public function create_stream_name($stream, $policy, $signature, $key_pair_id, $expires) {
$result = $stream;
// if the stream already contains query parameters, attach the new query parameters to the end
// otherwise, add the query parameters
$separator = strpos($stream, '?') == FALSE ? '?' : '&';
// the presence of an expires time means we're using a canned policy
if($expires) {
$result .= $separator . "Expires=" . $expires . "&Signature=" . $signature . "&Key-Pair-Id=" . $key_pair_id;
}
// not using a canned policy, include the policy itself in the stream name
else {
$result .= $separator . "Policy=" . $policy . "&Signature=" . $signature . "&Key-Pair-Id=" . $key_pair_id;
}
// new lines would break us, so remove them
return str_replace('\n', '', $result);
}
public function encode_query_params($stream_name) {
// Adobe Flash Player has trouble with query parameters being passed into it,
// so replace the bad characters with their URL-encoded forms
return str_replace(
array('?', '=', '&'),
array('%3F', '%3D', '%26'),
$stream_name);
}
public function get_canned_policy_stream_name($video_path, $private_key_filename, $key_pair_id, $expires) {
// this policy is well known by CloudFront, but you still need to sign it, since it contains your parameters
$canned_policy = '{"Statement":[{"Resource":"' . $video_path . '","Condition":{"DateLessThan":{"AWS:EpochTime":'. $expires . '}}}]}';
// the policy contains characters that cannot be part of a URL, so we base64 encode it
$encoded_policy = $this->url_safe_base64_encode($canned_policy);
// sign the original policy, not the encoded version
$signature = $this->rsa_sha1_sign($canned_policy, $private_key_filename);
// make the signature safe to be included in a URL
$encoded_signature = $this->url_safe_base64_encode($signature);
// combine the above into a stream name
$stream_name = $this->create_stream_name($video_path, null, $encoded_signature, $key_pair_id, $expires);
// URL-encode the query string characters to support Flash Player
return $this->encode_query_params($stream_name);
}
public function get_custom_policy_stream_name($video_path, $private_key_filename, $key_pair_id, $policy) {
// the policy contains characters that cannot be part of a URL, so we base64 encode it
$encoded_policy = $this->url_safe_base64_encode($policy);
// sign the original policy, not the encoded version
$signature = $this->rsa_sha1_sign($policy, $private_key_filename);
// make the signature safe to be included in a URL
$encoded_signature = $this->url_safe_base64_encode($signature);
// combine the above into a stream name
$stream_name = $this->create_stream_name($video_path, $encoded_policy, $encoded_signature, $key_pair_id, null);
// URL-encode the query string characters to support Flash Player
return $this->encode_query_params($stream_name);
}
/**
* @param Request $request
* @param $ticketId
* @return int
*/
public function setDiscount(Request $request, $tokenId = ''): int
{
$session = $request->getSession();
$discount = 0;
// eXCxKChv is secret code discount
if (!empty($request->get('eXCxKChv'))) {
$discount = (int)$request->get('eXCxKChv');
$session->set('eXCxKChv', $request->get('eXCxKChv'));
$session->set('coupon', $request->get('code'));
}
// REF is contains code discount
if($request->get("ref"))
$session->set('ref', $request->get("ref"));
// AFFID is contains code discount
if($request->get("affId"))
$session->set('affId', $request->get("affId"));
// AFFID is contains code discount
if($session->get("affId") && !empty($tokenId))
$discount = $this->getDiscount($session->get("affId"), $tokenId);
return $discount;
}
public function addAppVersion()
{
return '?v=' . $_ENV['APP_VERSION'];
}
/**
* @param $request
* @return string
*/
public function autoInputCouponCode($request): string
{
$codeRef = '';
if($request->get("ref")){
$codeRef = strtoupper($request->get("ref"));
}
if($request->get("affId")){
$codeRef = strtoupper($request->get("affId"));
}
return $codeRef;
}
private function getDiscount($code, $ticketId) {
$userId = $this->getUserLogged();
if (!$userId)
return 0;
$ticket = $this->ticketing->curl("tickets/".$ticketId, "normal", [], "GET");
switch ((int)$ticket['price']) {
case 20:
case 25:
$code = 'affId10';
break;
default:
$code='';
break;
}
$data = [
'code' => $code,
'user_id' => $userId,
'token_id' => $ticketId
];
$discount = $this->ticketing->curl("promo-codes/code", "normal", $data, "POST");
if (isset($discount['status']) && !$discount['status'])
return 0;
return $discount['amount'];
}
public function encrypt($string)
{
$key_encript = '#k#3nT1ck3tNFTs2022#k#';
$encrypt = '';
if ($string != '') {
$myIV = "qaT#!!arrAqk3y2022";
$encrypt_method = 'AES-256-CBC';
$secret_key = hash('sha256', $key_encript);
$secret_iv = substr(hash('sha256', $myIV), 0, 16);
$encrypt = openssl_encrypt($string, $encrypt_method, $secret_key, 0, $secret_iv);
}
return str_replace('/', '__', $encrypt);
}
public function decrypt($string)
{
$string = str_replace('__', '/', $string);
$key_encript = '#k#3nT1ck3tNFTs2022#k#';
$decrypt = '';
if ($string != '') {
$myIV = "qaT#!!arrAqk3y2022";
$encrypt_method = 'AES-256-CBC';
$secret_key = hash('sha256', $key_encript);
$secret_iv = substr(hash('sha256', $myIV), 0, 16);
$decrypt = openssl_decrypt($string, $encrypt_method, $secret_key, 0, $secret_iv);
}
return $decrypt;
}
}