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

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

new release. read changelog

File size: 4.6 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  try{
49    writeHeader();
50   
51    $url='https://127.0.0.1:9779';
52    $request=xmlrpc_encode_request($method, $args);
53    error_log($request);
54      $header[] = "Content-type: text/xml";
55      $header[] = "Content-length: ".strlen($request);
56
57      $ch = curl_init();
58      curl_setopt($ch, CURLOPT_URL, $url);
59      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
60      curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
61      curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
62      curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
63     
64      if ($timeout!="0"){
65        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);  // async
66        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);       // async
67      }
68   
69      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
70      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
71      curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/n4dcert.pem");
72       
73      $data = curl_exec($ch);
74     
75      /*
76      $myfile = fopen("/tmp/n4dlog","a");
77      fwrite($myfile,"\nDATA:::\n");
78      fwrite($myfile,$data);
79      fwrite($myfile,"\nCH:\n");
80      fwrite($myfile,$ch);
81      fclose($myfile);
82     
83       */ 
84     
85     
86      //error_log($data); /////////////////////
87      //error_log(curl_errno($ch)); ////////////////////////////////
88     
89      curl_close($ch);
90     
91      if ((gettype($ch)!="unknown type")&&curl_errno($ch)) {
92        $xml_snippet=simplexml_load_string($data);
93        $json=json_encode($xml_snippet);
94        echo (AESEncrypt($json));
95      } else {
96          $xmlobj=xmlrpc_decode($data);
97          if (gettype($xmlobj)=="string") error_log($xmlobj);
98         
99          $json=json_encode($xmlobj);
100          if ($json=="") {
101            $json=$xmlobj;}
102          echo (AESEncrypt($json));
103      }
104  } catch (Exception $e){
105    error_log("$e->getMessage()");
106    echo 'FATAL exception: ',  $e->getMessage(), "\n";
107    } 
108}
109
110
111function var_error_log( $object=null ){
112    ob_start();                    // start buffer capture
113    var_dump( $object );           // dump the values
114    $contents = ob_get_contents(); // put the buffer into a variable
115    ob_end_clean();                // end capture
116    error_log( $contents );        // log contents of the result of var_dump( $object )
117}
118
119
120
121try{
122// Getting $_POST parameters
123  $method=$_POST["method"];
124 
125  //error_log($_POST["args"]);
126  //error_log($_POST["args"]);
127 
128  $args_coded=n4dRSADecrypt($_POST["args"]);
129 
130  $args=json_decode($args_coded);
131  if (gettype($args)=="NULL") $args=array();
132 
133  $timeout=$_POST["timeout"];
134  /*
135   $myfile = fopen("/tmp/n4dlog","a");
136  fwrite($myfile,"\nMETHOD:\n");
137  fwrite($myfile,$method);
138  fwrite($myfile,"\nARGS IN POST:\n");
139  fwrite($myfile,$_POST["args"]);
140 
141  fwrite($myfile,"\nTYPE ARGS IN POST:\n");
142  fwrite($myfile, gettype($_POST["args"]));
143 
144  fwrite($myfile,"\nARGS CODED:\n");
145  fwrite($myfile, $args_coded);
146 
147  fwrite($myfile,"\nTYPE ARGS CODED:\n");
148  fwrite($myfile, gettype($args_coded));
149 
150  fwrite($myfile,"\nARGS DECODED:\n");
151  fwrite($myfile, print_r($args, true));
152  //fwrite($myfile, $args);
153  fwrite($myfile,"\nARGS DECODED TYPE:\n");
154  fwrite($myfile, gettype($args));
155 
156  fwrite($myfile,"******\n\n");
157  fclose($myfile);
158  error_log($method);
159   */
160 
161  if (isset($_POST['log'])) $log=$_POST["log"];
162    else $log="false";
163   
164  n4d($method, $args, $timeout);
165  //$n4d_async = new n4dAsync();
166  //$n4d_async ->start($method, $args);
167 
168} catch(Exception $e){
169  error_log("Exception in n4d.php: ".$e);
170}
171?>
Note: See TracBrowser for help on using the repository browser.