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

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

new release

File size: 4.3 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      //error_log($data); /////////////////////
76      //error_log(curl_errno($ch)); ////////////////////////////////
77     
78      curl_close($ch);
79     
80      if ((gettype($ch)!="unknown type")&&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          if (gettype($xmlobj)=="string") error_log($xmlobj);
87         
88          $json=json_encode($xmlobj);
89          if ($json=="") {
90            $json=$xmlobj;}
91          echo (AESEncrypt($json));
92      }
93  } catch (Exception $e){
94    error_log("$e->getMessage()");
95    echo 'FATAL exception: ',  $e->getMessage(), "\n";
96    } 
97}
98
99
100function var_error_log( $object=null ){
101    ob_start();                    // start buffer capture
102    var_dump( $object );           // dump the values
103    $contents = ob_get_contents(); // put the buffer into a variable
104    ob_end_clean();                // end capture
105    error_log( $contents );        // log contents of the result of var_dump( $object )
106}
107
108
109
110try{
111// Getting $_POST parameters
112  $method=$_POST["method"];
113 
114  //error_log($_POST["args"]);
115  //error_log($_POST["args"]);
116 
117  $args_coded=n4dRSADecrypt($_POST["args"]);
118 
119  $args=json_decode($args_coded);
120  if (gettype($args)=="NULL") $args=array();
121 
122  $timeout=$_POST["timeout"];
123  /*
124   $myfile = fopen("/tmp/n4dlog","a");
125  fwrite($myfile,"\nMETHOD:\n");
126  fwrite($myfile,$method);
127  fwrite($myfile,"\nARGS IN POST:\n");
128  fwrite($myfile,$_POST["args"]);
129 
130  fwrite($myfile,"\nTYPE ARGS IN POST:\n");
131  fwrite($myfile, gettype($_POST["args"]));
132 
133  fwrite($myfile,"\nARGS CODED:\n");
134  fwrite($myfile, $args_coded);
135 
136  fwrite($myfile,"\nTYPE ARGS CODED:\n");
137  fwrite($myfile, gettype($args_coded));
138 
139  fwrite($myfile,"\nARGS DECODED:\n");
140  fwrite($myfile, $args);
141  fwrite($myfile,"\nARGS DECODED TYPE:\n");
142  fwrite($myfile, gettype($args));
143 
144  fwrite($myfile,"******\n\n");
145  fclose($myfile);
146  error_log($method);
147  */
148 
149  if (isset($_POST['log'])) $log=$_POST["log"];
150    else $log="false";
151   
152  n4d($method, $args, $timeout);
153  //$n4d_async = new n4dAsync();
154  //$n4d_async ->start($method, $args);
155 
156} catch(Exception $e){
157  error_log("Exception in n4d.php: ".$e);
158}
159?>
Note: See TracBrowser for help on using the repository browser.