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

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

Encrypting credentials

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