<?php
function send_push_notification($registatoin_ids, $message) {
    $api = 'AIzaSyDt9lIvDkjwyhSg1uzbcVzF5_FX7J8v_80';
    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array('registration_ids' => $registatoin_ids, 'data' => $message);
    $headers = array('Authorization: key=' . $api, 'Content-Type: application/json');
    //print_r($headers);
    // Open connection
    $ch = curl_init();

    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);
    return $result;
}

function openurl($url, $postvars = "") {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, '3');
    $content = trim(curl_exec($ch));
    curl_close($ch);
    return $content;
}

function openurlget($url, $getvars = "") {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_GET, 1);
    curl_setopt($ch, CURLOPT_GETFIELDS, $getvars);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, '30');
    $content = trim(curl_exec($ch));
    curl_close($ch);
    return $content;
}

function Mod_addslashes($string) {
    if (get_magic_quotes_gpc() == 1) {
        return ($string);
    } else {
        return (addslashes($string));
    }
}

function sanitize($str, $remove_nl = true) {
    $str = StripSlashes($str);
    if ($remove_nl) {
        $injections = array('/(\n+)/i',
            '/(\r+)/i',
            '/(\t+)/i',
            '/(%0A+)/i',
            '/(%0D+)/i',
            '/(%08+)/i',
            '/(%09+)/i'
        );
        $str = preg_replace($injections, '', $str);
    }
    return $str;
}

function validate_phone($phone) {
    if (eregi("([0-9]{10})", $phone) !== FALSE && strlen($phone) === 10) {
        $temp = substr($phone, 0, -9);
        if ($temp < 1) {
            return FALSE;
        } else {
            return TRUE;
        }
    } else {
        return FALSE;
    }
}

function validate_mobile($mobile) {
    if (eregi("([0-9]{10})", $mobile) !== FALSE && strlen($mobile) === 10) {
        $temp = substr($mobile, 0, -9);
        if ($temp < 7) {
            return FALSE;
        } else {
            return TRUE;
        }
    } else {
        return FALSE;
    }
}

function validate_email($email) {
    return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
}

function validateJSON() {
    $args = func_get_args(); // gets the parameters passed to this func as an array;
    $json = $args[0];
    $count = func_num_args() - 1; // 0 is the json, so total count of arguments - 1;
    $error = array(); // contains the list of keys not found in $json;
    for ($i = 1; $i <= $count; $i++) {
        if (!isset($json->$args[$i]))
            $error[] = $args[$i];
    }
    return $error;
}

function makeJSON() {
    $args = func_get_args();
    $num = func_num_args();
    $jsonArray = array();
    for ($i = 0; $i < $num; $i = $i + 2) {
        $jsonArray[$args[$i]] = $args[$i + 1];
    }
    $json = json_encode($jsonArray);
    return $json;
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
function activity_log($d, $user_id, $activity, $date, $flag, $ip_address,$role) {
    $query = 'INSERT INTO activity_log(user_id,activity,date,flag, ip_address,role)VALUES("' . $user_id . '","' . $activity . '","' . $date . '","' . $flag . '","' . $ip_address . '","' . $role . '")';
    return $d->insertQuery($query);
}
function mailAttachments($to, $from, $subject, $message, $attachments = array(), $headers = array(), $additional_parameters = '') {
    $headers['From'] = $from;

    // Define the boundray we're going to use to separate our data with.
    $mime_boundary = '==MIME_BOUNDARY_' . md5(time());

    // Define attachment-specific headers
    $headers['MIME-Version'] = '1.0';
    $headers['Content-Type'] = 'multipart/mixed; boundary="' . $mime_boundary . '"';

    // Convert the array of header data into a single string.
    $headers_string = '';
    foreach ($headers as $header_name => $header_value) {
        if (!empty($headers_string)) {
            $headers_string .= "\r\n";
        }
        $headers_string .= $header_name . ': ' . $header_value;
    }

    // Message Body
    $message_string = '--' . $mime_boundary;
    $message_string .= "\r\n";
    $message_string .= 'Content-Type: text/plain; charset="iso-8859-1"';
    $message_string .= "\r\n";
    $message_string .= 'Content-Transfer-Encoding: 7bit';
    $message_string .= "\r\n";
    $message_string .= "\r\n";
    $message_string .= $message;
    $message_string .= "\r\n";
    $message_string .= "\r\n";

    // Add attachments to message body
    foreach ($attachments as $local_filename => $attachment_filename) {
        if (is_file($local_filename)) {
            $message_string .= '--' . $mime_boundary;
            $message_string .= "\r\n";
            $message_string .= 'Content-Type: application/octet-stream; name="' . $attachment_filename . '"';
            $message_string .= "\r\n";
            $message_string .= 'Content-Description: ' . $attachment_filename;
            $message_string .= "\r\n";

            $fp = @fopen($local_filename, 'rb'); // Create pointer to file
            $file_size = filesize($local_filename); // Read size of file
            $data = @fread($fp, $file_size); // Read file contents
            $data = chunk_split(base64_encode($data)); // Encode file contents for plain text sending

            $message_string .= 'Content-Disposition: attachment; filename="' . $attachment_filename . '"; size=' . $file_size . ';';
            $message_string .= "\r\n";
            $message_string .= 'Content-Transfer-Encoding: base64';
            $message_string .= "\r\n\r\n";
            $message_string .= $data;
            $message_string .= "\r\n\r\n";
        }
    }

    // Signal end of message
    $message_string .= '--' . $mime_boundary . '--';

    // Send the e-mail.
    return mail($to, $subject, $message_string, $headers_string, $additional_parameters);
}

function encrypt_decrypt($action, $string) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key = 'DIT<7E4MHH^U7WKt5yc,*-4?2NSBT"';
    $secret_iv = "z8Q66M)=8H'*BS*ZA74i}';rP*S8?P";

    // hash
    $key = hash('sha256', $secret_key);

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);

    if ($action == 'encrypt') {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    } else if ($action == 'decrypt') {
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }

    return $output;
}

function RandomString($length) {
    $keys = array_merge(range(0, 9), range('a', 'z'));

    $key = "";
    for ($i = 0; $i < $length; $i++) {
        $key .= $keys[mt_rand(0, count($keys) - 1)];
    }
    return $key;
}

function getResponse($status,$message){
    return '{"status":"'.$status.'","message":"'.$message.'"}';
}

function getUserDetails($id,$field)
{
    global $d;
    $q = 'SELECT '.$field.' FROM user WHERE id='.$id;
    $r = $d->selectQueryInArray($q);
    return $r[0][$field];
}

function isInWishlist($id)
{
    global $d;
    if(isset($_SESSION['id']))
    {
        $q = 'SELECT COUNT(id) AS Total FROM favorite WHERE (product_id = "'.$id.'" AND user_id="'.$_SESSION['id'].'")';
        $r = $d->selectQueryInArray($q);
        if($r[0]['Total'] > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
?>