<?php
// src/OgBootBundle/Service/CurlRequestService.php

namespace App\OgBootBundle\Service;
require_once __DIR__ . '/../constants.php';
use Exception;
include_once (__DIR__ . '/../../../etc/php-vars.php');
$conf_file = parse_ini_file(__DIR__ . '/../../../etc/ogAdmServer.cfg');
define('OG_REST_API_TOKEN', 'Authorization: ' . $conf_file['APITOKEN']);

class CurlRequestService
{
    function common_request($command, $type, $data = null) {

        $json = json_encode($data);
    
        $service_url = OG_REST_URL.$command;
    
        $curl = curl_init($service_url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            OG_REST_API_TOKEN,
         ));
	echo __DIR__ . '/../../../etc/ssl/WebConsole.crt.pem';
        curl_setopt ($curl, CURLOPT_SSLCERT, __DIR__ . '/../../../etc/ssl/WebConsole.crt.pem');
	curl_setopt ($curl, CURLOPT_SSLKEY,  __DIR__ . '/../../../etc/ssl/WebConsole.key.pem');
	if (curl_error($curl)) {
		    echo 'Error setting key: ' . curl_error($curl);
	}
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);  
        switch ($type) {
            default:
            case GET:
                break;
            case POST:
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
        }
    
        $curl_response = curl_exec($curl);
	$info = curl_getinfo($curl);
	var_dump($info);
    
        if ($curl_response === false || $info['http_code'] != 200) {
            syslog(LOG_ERR, 'error occured during curl exec. Additioanl info: ' . print_r($info, TRUE));
            return 0;
        }
    
        curl_close($curl);
    
        syslog(LOG_INFO, 'response '.$command.' ok!');
    
        return json_decode($curl_response, true);
    }

    public function convertMaskToCIDR($mask)
    {
        $bits = 0;
        $mask = explode(".", $mask);

        foreach ($mask as $octect)
            $bits += strlen(str_replace("0", "", decbin($octect)));

        return $bits;
    }


}
