Files
2020-07-26 12:39:11 -04:00

191 lines
6.5 KiB
PHP

<?php
class COR_Controller extends CI_Controller {
var $template = array(
'table_open' => "<table class='table-responsive table-striped table-hover table-bordered table-condensed'>",
'thead_open' => '<thead class=\'bg-indigo\'>',
'thead_close' => '</thead>',
'heading_row_start' => '<tr style=\'padding:1px;\'>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'tbody_open' => '<tbody>',
'tbody_close' => '</tbody>',
'row_start' => '<tr style=\'padding:0px;\'>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>',
'table_close' => '</table>'
);
var $template_nohead = array(
'table_open' => "<table class='table-responsive table-striped table-hover table-bordered table-condensed'>",
'thead_open' => '<thead>',
'thead_close' => '</thead>',
'heading_row_start' => '<tr style=\'padding:3px;\'>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'tbody_open' => '<tbody>',
'tbody_close' => '</tbody>',
'row_start' => '<tr style=\'padding:3px;\'>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>',
'table_close' => '</table>'
);
public $data = array();
function __construct() {
parent::__construct();
}
protected function smart_htmlspecialchars($str) {
if (substr($str, 0, 1) == '<')
return $str;
return htmlspecialchars($str);
}
protected function sql_escape_func($inp) {
if (is_array($inp)) {
return array_map(__METHOD__, $inp);
}
if (!empty($inp) && is_string($inp)) {
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
}
return $inp;
}
protected function savvy_api($in, &$out) {
global $coregrade;
$ret = -1;
$in['pid'] = 115;
$in['backoffice'] = 1;
error_log(json_encode($in));
$out = $coregrade->coregrade_api($in);
$ret = $out["retval"];
error_log("ret = $ret");
error_log(json_encode($out));
return $ret;
}
protected function main_api_post($endpoint,$payload) {
global $coregrade;
$httpAuthToken = $coregrade->cfgReadChar('system.oauth2_token');
$encryptionAlg = $coregrade->cfgReadChar('encryption.algorithm');
$encryptionKey = $coregrade->cfgReadChar('encryption.key');
$encryptionIV = $coregrade->cfgReadChar('encryption.iv');
$encrypted_payload = bin2hex(
openssl_encrypt(
$payload,
$encryptionAlg,
$encryptionKey,
OPENSSL_RAW_DATA,
$encryptionIV
));
$postdata = "{\"encrypted_payload\": \"${encrypted_payload}\"}";
$url = $coregrade->cfgReadChar('system.api_url').$endpoint;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($postdata),
'Authorization: Server-Token ' . $httpAuthToken)
);
$body = curl_exec($ch);
$result = json_decode($body,true);
if (is_array($result) && array_key_exists('payload',$result)) {
$decrypted = openssl_decrypt(
hex2bin(
$result['payload']
),
$encryptionAlg,
$encryptionKey,
OPENSSL_RAW_DATA,
$encryptionIV
);
} else {
$decrypted = $body; // Attempt without encryption
}
$payload = json_decode($decrypted, true);
return [$payload,$decrypted,$result,$body];
}
protected function main_api_get($endpoint,$payload) {
global $coregrade;
$httpAuthToken = $coregrade->cfgReadChar('system.oauth2_token');
$encryptionAlg = $coregrade->cfgReadChar('encryption.algorithm');
$encryptionKey = $coregrade->cfgReadChar('encryption.key');
$encryptionIV = $coregrade->cfgReadChar('encryption.iv');
$url = $coregrade->cfgReadChar('system.api_url').$endpoint.$payload;
//echo $url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Server-Token ' . $httpAuthToken)
);
$body = curl_exec($ch);
$result = json_decode($body,true);
if (is_array($result) && array_key_exists('payload',$result)) {
$decrypted = openssl_decrypt(
hex2bin(
$result['payload']
),
$encryptionAlg,
$encryptionKey,
OPENSSL_RAW_DATA,
$encryptionIV
);
} else {
$decrypted = $body; // Attempt without encryption
}
$payload = json_decode($decrypted, true);
return [$payload,$decrypted,$result,$body];
}
function formatedMesage($msgType, $theMessage) {
return "<div class=\"text-left\"><div class=\"alert alert-danger no-border\">" . $theMessage . "</div></div>";
}
protected function renderAdminPage($page_type, $page_name, $data) {
$this->load->view('admin_template/view_admin_header', $data);
$this->load->view($page_type.'/' . $page_name, $data);
$this->load->view('admin_template/view_admin_footer', $data);
}
}