<?php
namespace App\Utils;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
class Functions extends AbstractController
{
const CONVERSION = 200;
protected $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack= $requestStack;
}
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);
}
public function get_user_ip()
{
$http_headers = apache_request_headers();
if(array_key_exists("X-Forwarded-For", $http_headers)){
$original_ip = $http_headers["X-Forwarded-For"];
$ips = explode(",", $original_ip);
$ip=trim($ips[0]);
}
else{
// Get real visitor IP behind CloudFlare network
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
}
return $ip;
}
public function requestPaypal($data)
{
$PAYPAL_CLIENT_ID = $_ENV['PAYPAL_CLIENT_ID'];
$PAYPAL_SECRET = $_ENV['PAYPAL_SECRET'];
$PAYPAL_URL = $_ENV['PAYPAL_URL'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $PAYPAL_URL . $data['uri']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
if(count($data['post']))
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data['post']));
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Basic ' . base64_encode($PAYPAL_CLIENT_ID . ':' . $PAYPAL_SECRET);
$headers[] = 'Paypal-Request-Id: ' . $data['id'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
return [
'status' => false,
'message' => 'We were unable to complete the transaction, please try again in a few minutes.',
'data' => []
];
}
curl_close($ch);
return [
'status' => true,
'env' => $PAYPAL_URL,
'data' => json_decode($result, true)
];
}
public function addAppVersion()
{
return '?v=' . $_ENV['APP_VERSION'];
}
}