src/Utils/Functions.php line 150

Open in your IDE?
  1. <?php
  2. namespace App\Utils;
  3. use App\Utils\Ticketing;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class Functions  extends AbstractController
  8. {
  9.     protected $requestStack;
  10.     /**
  11.      * @var Ticketing
  12.      */
  13.     private $ticketing;
  14.     public function __construct(RequestStack $requestStackTicketing $ticketing)
  15.     {
  16.         $this->requestStack $requestStack;
  17.         $this->ticketing $ticketing;
  18.     }
  19.     public function checkCaptcha($captchaResponse)
  20.     {
  21.         $secret $this->getParameter('captcha_secret');
  22.         $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captchaResponse."&remoteip=".$_SERVER['REMOTE_ADDR']);
  23.         $obj json_decode($response);
  24.         return $obj->success;
  25.     }
  26.     public function getUserLogged(){
  27.         $session $this->requestStack->getCurrentRequest()->getSession();
  28.         if($session->get('userId')){
  29.             return $session->get('userId');
  30.         }
  31.         return false;
  32.     }
  33.     public function rsa_sha1_sign($policy$private_key_filename) {
  34.         $signature "";
  35.         // load the private key
  36.         $fp fopen($private_key_filename"r");
  37.         $priv_key fread($fp8192);
  38.         fclose($fp);
  39.         $pkeyid openssl_get_privatekey($priv_key);
  40.         // compute signature
  41.         openssl_sign($policy$signature$pkeyid);
  42.         // free the key from memory
  43.         openssl_free_key($pkeyid);
  44.         return $signature;
  45.     }
  46.     public function url_safe_base64_encode($value) {
  47.         $encoded base64_encode($value);
  48.         // replace unsafe characters +, = and / with the safe characters -, _ and ~
  49.         return str_replace(
  50.             array('+''=''/'),
  51.             array('-''_''~'),
  52.             $encoded);
  53.     }
  54.     public function create_stream_name($stream$policy$signature$key_pair_id$expires) {
  55.         $result $stream;
  56.         // if the stream already contains query parameters, attach the new query parameters to the end
  57.         // otherwise, add the query parameters
  58.         $separator strpos($stream'?') == FALSE '?' '&';
  59.         // the presence of an expires time means we're using a canned policy
  60.         if($expires) {
  61.             $result .= $separator "Expires=" $expires "&Signature=" $signature "&Key-Pair-Id=" $key_pair_id;
  62.         }
  63.         // not using a canned policy, include the policy itself in the stream name
  64.         else {
  65.             $result .= $separator "Policy=" $policy "&Signature=" $signature "&Key-Pair-Id=" $key_pair_id;
  66.         }
  67.         // new lines would break us, so remove them
  68.         return str_replace('\n'''$result);
  69.     }
  70.     public function encode_query_params($stream_name) {
  71.         // Adobe Flash Player has trouble with query parameters being passed into it,
  72.         // so replace the bad characters with their URL-encoded forms
  73.         return str_replace(
  74.             array('?''=''&'),
  75.             array('%3F''%3D''%26'),
  76.             $stream_name);
  77.     }
  78.     public function get_canned_policy_stream_name($video_path$private_key_filename$key_pair_id$expires) {
  79.         // this policy is well known by CloudFront, but you still need to sign it, since it contains your parameters
  80.         $canned_policy '{"Statement":[{"Resource":"' $video_path '","Condition":{"DateLessThan":{"AWS:EpochTime":'$expires '}}}]}';
  81.         // the policy contains characters that cannot be part of a URL, so we base64 encode it
  82.         $encoded_policy $this->url_safe_base64_encode($canned_policy);
  83.         // sign the original policy, not the encoded version
  84.         $signature $this->rsa_sha1_sign($canned_policy$private_key_filename);
  85.         // make the signature safe to be included in a URL
  86.         $encoded_signature $this->url_safe_base64_encode($signature);
  87.         // combine the above into a stream name
  88.         $stream_name $this->create_stream_name($video_pathnull$encoded_signature$key_pair_id$expires);
  89.         // URL-encode the query string characters to support Flash Player
  90.         return $this->encode_query_params($stream_name);
  91.     }
  92.     public function get_custom_policy_stream_name($video_path$private_key_filename$key_pair_id$policy) {
  93.         // the policy contains characters that cannot be part of a URL, so we base64 encode it
  94.         $encoded_policy $this->url_safe_base64_encode($policy);
  95.         // sign the original policy, not the encoded version
  96.         $signature $this->rsa_sha1_sign($policy$private_key_filename);
  97.         // make the signature safe to be included in a URL
  98.         $encoded_signature $this->url_safe_base64_encode($signature);
  99.         // combine the above into a stream name
  100.         $stream_name $this->create_stream_name($video_path$encoded_policy$encoded_signature$key_pair_idnull);
  101.         // URL-encode the query string characters to support Flash Player
  102.         return $this->encode_query_params($stream_name);
  103.     }
  104.     /**
  105.      * @param Request $request
  106.      * @param $ticketId
  107.      * @return int
  108.      */
  109.     public function setDiscount(Request $request$tokenId ''): int
  110.     {
  111.         $session $request->getSession();
  112.         $discount 0;
  113.         // eXCxKChv is secret code discount
  114.         if (!empty($request->get('eXCxKChv'))) {
  115.             $discount = (int)$request->get('eXCxKChv');
  116.             $session->set('eXCxKChv'$request->get('eXCxKChv'));
  117.             $session->set('coupon'$request->get('code'));
  118.         }
  119.         // REF is contains code discount
  120.         if($request->get("ref"))
  121.             $session->set('ref'$request->get("ref"));
  122.         // AFFID is contains code discount
  123.         if($request->get("affId"))
  124.             $session->set('affId'$request->get("affId"));
  125.         // AFFID is contains code discount
  126.         if($session->get("affId") && !empty($tokenId))
  127.             $discount $this->getDiscount($session->get("affId"), $tokenId);
  128.         return $discount;
  129.     }
  130.     public function addAppVersion()
  131.     {
  132.         return '?v=' $_ENV['APP_VERSION'];
  133.     }
  134.     /**
  135.      * @param $request
  136.      * @return string
  137.      */
  138.     public function autoInputCouponCode($request): string
  139.     {
  140.         $codeRef '';
  141.         if($request->get("ref")){
  142.             $codeRef strtoupper($request->get("ref"));
  143.         }
  144.         if($request->get("affId")){
  145.             $codeRef strtoupper($request->get("affId"));
  146.         }
  147.         return $codeRef;
  148.     }
  149.     private function getDiscount($code$ticketId) {
  150.         $userId $this->getUserLogged();
  151.         if (!$userId)
  152.             return 0;
  153.         $ticket $this->ticketing->curl("tickets/".$ticketId"normal", [], "GET");
  154.         switch ((int)$ticket['price']) {
  155.             case 20:
  156.             case 25:
  157.                 $code 'affId10';
  158.                 break;
  159.             default:
  160.                 $code='';
  161.                 break;
  162.         }
  163.         $data = [
  164.             'code' => $code,
  165.             'user_id' => $userId,
  166.             'token_id' => $ticketId
  167.         ];
  168.         $discount $this->ticketing->curl("promo-codes/code""normal"$data"POST");
  169.         if (isset($discount['status']) && !$discount['status'])
  170.             return 0;
  171.         return $discount['amount'];
  172.     }
  173.     public function encrypt($string)
  174.     {
  175.         $key_encript '#k#3nT1ck3tNFTs2022#k#';
  176.         $encrypt '';
  177.         if ($string != '') {
  178.             $myIV "qaT#!!arrAqk3y2022";
  179.             $encrypt_method 'AES-256-CBC';
  180.             $secret_key hash('sha256'$key_encript);
  181.             $secret_iv substr(hash('sha256'$myIV), 016);
  182.             $encrypt openssl_encrypt($string$encrypt_method$secret_key0$secret_iv);
  183.         }
  184.         return str_replace('/''__'$encrypt);
  185.     }
  186.     public function decrypt($string)
  187.     {
  188.         $string str_replace('__''/'$string);
  189.         $key_encript '#k#3nT1ck3tNFTs2022#k#';
  190.         $decrypt '';
  191.         if ($string != '') {
  192.             $myIV "qaT#!!arrAqk3y2022";
  193.             $encrypt_method 'AES-256-CBC';
  194.             $secret_key hash('sha256'$key_encript);
  195.             $secret_iv substr(hash('sha256'$myIV), 016);
  196.             $decrypt openssl_decrypt($string$encrypt_method$secret_key0$secret_iv);
  197.         }
  198.         return $decrypt;
  199.     }
  200. }