<?php

set_time_limit(0);
//ignore_user_abort(true);    //bypass browser abort
ini_set('memory_limit', '1024M');

require_once dirname(__DIR__) . '/autoload.smart.php';
require_once LIBS_PATH . 'Common/db_standalone_class.php';
/** @property db_standalone $db */
$db = new db_standalone(null, DB_HOST, DB_USER, DB_PASS, DB_NAME);


function sync_get_contents($url, $post_arr = array()) {
    $postdata = '';
    if (!empty($post_arr))
        $postdata = http_build_query($post_arr);
    //
    $header_arr = array(
        'Content-type: application/x-www-form-urlencoded',
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng',
        'Accept-Language: en-US,en;q=0.9,ar;q=0.8',
        'Cache-Control: max-age=0',
        'Connection: keep-alive',
        'Host: ' . parse_url($url, PHP_URL_HOST),
        'Origin: ' . parse_url($url, PHP_URL_HOST),
        'Upgrade-Insecure-Requests: 1',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    );
    //some times in some local server when try file get contents on large data or time it return failed for that if curl is activated we use it first
    if (extension_loaded('curl')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (!empty($postdata)) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_arr);
        $contents = curl_exec($ch);
        curl_close($ch);
    } else { //ini_get('allow_url_fopen')
        $opts = array('http' =>
            array(
                'method' => (empty($postdata) ? 'GET' : 'POST'),
                'header' => implode("\r\n", $header_arr),
                'content' => $postdata
            )
        );
        $context = stream_context_create($opts);
        $contents = file_get_contents($url, false, $context);
    }
    return $contents;
}
