src/Utils/Functions.php line 124

Open in your IDE?
  1. <?php
  2. namespace App\Utils;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. class Functions  extends AbstractController
  6. {
  7.     const CONVERSION 200;
  8.     protected $requestStack;
  9.     public function __construct(RequestStack $requestStack)
  10.     {
  11.         $this->requestStack$requestStack;
  12.     }
  13.     public function getUserLogged(){
  14.         $session $this->requestStack->getCurrentRequest()->getSession();
  15.         if($session->get('userId')){
  16.             return $session->get('userId');
  17.         }
  18.         return false;
  19.     }
  20.     public function rsa_sha1_sign($policy$private_key_filename) {
  21.         $signature "";
  22.         // load the private key
  23.         $fp fopen($private_key_filename"r");
  24.         $priv_key fread($fp8192);
  25.         fclose($fp);
  26.         $pkeyid openssl_get_privatekey($priv_key);
  27.         // compute signature
  28.         openssl_sign($policy$signature$pkeyid);
  29.         // free the key from memory
  30.         openssl_free_key($pkeyid);
  31.         return $signature;
  32.     }
  33.     public function url_safe_base64_encode($value) {
  34.         $encoded base64_encode($value);
  35.         // replace unsafe characters +, = and / with the safe characters -, _ and ~
  36.         return str_replace(
  37.             array('+''=''/'),
  38.             array('-''_''~'),
  39.             $encoded);
  40.     }
  41.     public function create_stream_name($stream$policy$signature$key_pair_id$expires) {
  42.         $result $stream;
  43.         // if the stream already contains query parameters, attach the new query parameters to the end
  44.         // otherwise, add the query parameters
  45.         $separator strpos($stream'?') == FALSE '?' '&';
  46.         // the presence of an expires time means we're using a canned policy
  47.         if($expires) {
  48.             $result .= $separator "Expires=" $expires "&Signature=" $signature "&Key-Pair-Id=" $key_pair_id;
  49.         }
  50.         // not using a canned policy, include the policy itself in the stream name
  51.         else {
  52.             $result .= $separator "Policy=" $policy "&Signature=" $signature "&Key-Pair-Id=" $key_pair_id;
  53.         }
  54.         // new lines would break us, so remove them
  55.         return str_replace('\n'''$result);
  56.     }
  57.     public function encode_query_params($stream_name) {
  58.         // Adobe Flash Player has trouble with query parameters being passed into it,
  59.         // so replace the bad characters with their URL-encoded forms
  60.         return str_replace(
  61.             array('?''=''&'),
  62.             array('%3F''%3D''%26'),
  63.             $stream_name);
  64.     }
  65.     public function get_canned_policy_stream_name($video_path$private_key_filename$key_pair_id$expires) {
  66.         // this policy is well known by CloudFront, but you still need to sign it, since it contains your parameters
  67.         $canned_policy '{"Statement":[{"Resource":"' $video_path '","Condition":{"DateLessThan":{"AWS:EpochTime":'$expires '}}}]}';
  68.         // the policy contains characters that cannot be part of a URL, so we base64 encode it
  69.         $encoded_policy $this->url_safe_base64_encode($canned_policy);
  70.         // sign the original policy, not the encoded version
  71.         $signature $this->rsa_sha1_sign($canned_policy$private_key_filename);
  72.         // make the signature safe to be included in a URL
  73.         $encoded_signature $this->url_safe_base64_encode($signature);
  74.         // combine the above into a stream name
  75.         $stream_name $this->create_stream_name($video_pathnull$encoded_signature$key_pair_id$expires);
  76.         // URL-encode the query string characters to support Flash Player
  77.         return $this->encode_query_params($stream_name);
  78.     }
  79.     public function get_custom_policy_stream_name($video_path$private_key_filename$key_pair_id$policy) {
  80.         // the policy contains characters that cannot be part of a URL, so we base64 encode it
  81.         $encoded_policy $this->url_safe_base64_encode($policy);
  82.         // sign the original policy, not the encoded version
  83.         $signature $this->rsa_sha1_sign($policy$private_key_filename);
  84.         // make the signature safe to be included in a URL
  85.         $encoded_signature $this->url_safe_base64_encode($signature);
  86.         // combine the above into a stream name
  87.         $stream_name $this->create_stream_name($video_path$encoded_policy$encoded_signature$key_pair_idnull);
  88.         // URL-encode the query string characters to support Flash Player
  89.         return $this->encode_query_params($stream_name);
  90.     }
  91.     public function get_user_ip()
  92.     {
  93.         $http_headers apache_request_headers(); 
  94.         if(array_key_exists("X-Forwarded-For"$http_headers)){
  95.             $original_ip $http_headers["X-Forwarded-For"];
  96.             $ips explode(","$original_ip);
  97.             $ip=trim($ips[0]);
  98.         }
  99.         else{
  100.             // Get real visitor IP behind CloudFlare network
  101.             if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  102.                       $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
  103.                       $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
  104.             }
  105.             $client  = @$_SERVER['HTTP_CLIENT_IP'];
  106.             $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  107.             $remote  $_SERVER['REMOTE_ADDR'];
  108.             if(filter_var($clientFILTER_VALIDATE_IP))
  109.             {
  110.                 $ip $client;
  111.             }
  112.             elseif(filter_var($forwardFILTER_VALIDATE_IP))
  113.             {
  114.                 $ip $forward;
  115.             }
  116.             else
  117.             {
  118.                 $ip $remote;
  119.             }
  120.         }
  121.         return $ip;
  122.     }
  123.     public function requestPaypal($data)
  124.     {
  125.         $PAYPAL_CLIENT_ID $_ENV['PAYPAL_CLIENT_ID'];
  126.         $PAYPAL_SECRET $_ENV['PAYPAL_SECRET'];
  127.         $PAYPAL_URL $_ENV['PAYPAL_URL'];
  128.         $ch curl_init();
  129.         curl_setopt($chCURLOPT_URL$PAYPAL_URL $data['uri']);
  130.         curl_setopt($chCURLOPT_RETURNTRANSFER1);
  131.         curl_setopt($chCURLOPT_POST1);
  132.         if(count($data['post']))
  133.             curl_setopt($chCURLOPT_POSTFIELDSjson_encode($data['post']));
  134.         $headers = array();
  135.         $headers[] = 'Content-Type: application/json';
  136.         $headers[] = 'Authorization: Basic ' base64_encode($PAYPAL_CLIENT_ID ':' $PAYPAL_SECRET);
  137.         $headers[] = 'Paypal-Request-Id: ' $data['id'];
  138.         curl_setopt($chCURLOPT_HTTPHEADER$headers);
  139.         $result curl_exec($ch);
  140.         if (curl_errno($ch)) {
  141.             return [
  142.                 'status' => false,
  143.                 'message' => 'We were unable to complete the transaction, please try again in a few minutes.',
  144.                 'data' => []
  145.             ];
  146.         }
  147.         curl_close($ch);
  148.         return [
  149.             'status' => true,
  150.             'env' => $PAYPAL_URL,
  151.             'data' => json_decode($resulttrue)
  152.         ];
  153.     }
  154.     public function addAppVersion()
  155.     {
  156.         return '?v=' $_ENV['APP_VERSION'];
  157.     }
  158. }