source: admin-center/trunk/fuentes/admin-center.install/usr/share/admin-center/n4d.php @ 2185

Last change on this file since 2185 was 2185, checked in by joamuran, 3 years ago

Added aes encryption for n4d answers

File size: 4.1 KB
Line 
1<?php
2include("./libphp/cryptojs-aes.php");
3
4//require_once("logServer.php"); -> No s'usa!
5//require_once('php_lib/wsclient/vendor/autoload.php'); -> No s'usa !
6//use WebSocket\Client; -> No s'usa!
7
8
9// require_once('php_lib/logListener.php');  -> S'utilitza?
10// use logListener;  -> S'utilitza?
11
12
13// Class for make n4d calls asyncronous
14/*class n4dAsync extends Thread {
15    public function run($method, $args) {
16        n4d($method, $args);
17    }
18}*/
19// End async
20
21
22if(!session_id()) session_start();
23//$logServer = new logServer();
24
25
26function writeHeader(){
27  header('Content-type: application/json');
28  header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
29  header("Cache-Control: post-check=0, pre-check=0", false);
30  header("Pragma: no-cache");
31}
32
33
34function n4dRSADecrypt($crypttext){
35  $priv_key = openssl_pkey_get_private("file:///etc/admin-center/private_key.pem");
36  openssl_private_decrypt(base64_decode($crypttext), $newsource, $priv_key );
37 
38  return $newsource;
39}
40
41function AESEncrypt($plain){ 
42  if (isset($_POST['pass'])) $pass=$_POST['pass'];
43  else $pass=""; 
44  return cryptoJsAesEncrypt($pass, $plain);
45}
46
47function n4d($method, $args, $timeout){
48  writeHeader();
49 
50  $url='https://127.0.0.1:9779';
51  $request=xmlrpc_encode_request($method, $args);
52  //error_log($request);
53    $header[] = "Content-type: text/xml";
54    $header[] = "Content-length: ".strlen($request);
55 
56    $ch = curl_init();
57    curl_setopt($ch, CURLOPT_URL, $url);
58    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
59    curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
60    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
61    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
62   
63   
64    if ($timeout!="0"){
65      error_log("**************************************************************");
66      curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);  // async
67      curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);       // async
68    }
69 
70    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
71    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
72    curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/n4dcert.pem");
73 
74 
75    $data = curl_exec($ch);
76    curl_close($ch);
77   
78    //error_log($data);
79    //error_log(curl_errno($ch));
80    if (curl_errno($ch)) {
81      $xml_snippet=simplexml_load_string($data);
82      $json=json_encode($xml_snippet);
83      echo (AESEncrypt($json));
84    } else {
85        $xmlobj=xmlrpc_decode($data);
86       
87      error_log($xmlobj);
88       
89        $json=json_encode($xmlobj);
90        if ($json=="") {
91          $json=$xmlobj;}
92        echo (AESEncrypt($json));
93    }
94 
95}
96
97
98function var_error_log( $object=null ){
99    ob_start();                    // start buffer capture
100    var_dump( $object );           // dump the values
101    $contents = ob_get_contents(); // put the buffer into a variable
102    ob_end_clean();                // end capture
103    error_log( $contents );        // log contents of the result of var_dump( $object )
104}
105
106
107
108try{
109// Getting $_POST parameters
110  $method=$_POST["method"];
111 
112  //error_log($_POST["args"]);
113  //error_log($_POST["args"]);
114 
115  $args_coded=n4dRSADecrypt($_POST["args"]);
116 
117  $args=json_decode($args_coded);
118  if (gettype($args)=="NULL") $args=array();
119 
120  $timeout=$_POST["timeout"];
121  /*$myfile = fopen("/tmp/n4dlog","a");
122  fwrite($myfile,"\nMETHOD:\n");
123  fwrite($myfile,$method);
124  fwrite($myfile,"\nARGS IN POST:\n");
125  fwrite($myfile,$_POST["args"]);
126 
127  fwrite($myfile,"\nTYPE ARGS IN POST:\n");
128  fwrite($myfile, gettype($_POST["args"]));
129 
130  fwrite($myfile,"\nARGS CODED:\n");
131  fwrite($myfile, $args_coded);
132 
133  fwrite($myfile,"\nTYPE ARGS CODED:\n");
134  fwrite($myfile, gettype($args_coded));
135 
136  fwrite($myfile,"\nARGS DECODED:\n");
137  fwrite($myfile, $args);
138  fwrite($myfile,"\nARGS DECODED TYPE:\n");
139  fwrite($myfile, gettype($args));
140 
141  fwrite($myfile,"******\n\n");
142  fclose($myfile);
143  error_log($method);
144  */
145 
146  if (isset($_POST['log'])) $log=$_POST["log"];
147    else $log="false";
148   
149  n4d($method, $args, $timeout);
150  //$n4d_async = new n4dAsync();
151  //$n4d_async ->start($method, $args);
152 
153} catch(Exception $e){
154  error_log("Exception in n4d.php: ".$e);
155}
156?>
Note: See TracBrowser for help on using the repository browser.