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 | |
---|
20 | if(!session_id()) session_start(); |
---|
21 | //$logServer = new logServer(); |
---|
22 | |
---|
23 | |
---|
24 | function 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 | function n4d($method, $args, $timeout){ |
---|
32 | |
---|
33 | writeHeader(); |
---|
34 | $url='https://127.0.0.1:9779'; |
---|
35 | $request=xmlrpc_encode_request($method, $args); |
---|
36 | //error_log($request); |
---|
37 | $header[] = "Content-type: text/xml"; |
---|
38 | $header[] = "Content-length: ".strlen($request); |
---|
39 | |
---|
40 | $ch = curl_init(); |
---|
41 | curl_setopt($ch, CURLOPT_URL, $url); |
---|
42 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
43 | curl_setopt($ch, CURLOPT_TIMEOUT, 10000); |
---|
44 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
---|
45 | curl_setopt($ch, CURLOPT_POSTFIELDS, $request); |
---|
46 | |
---|
47 | |
---|
48 | if ($timeout!="0"){ |
---|
49 | error_log("**************************************************************"); |
---|
50 | curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); // async |
---|
51 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // async |
---|
52 | } |
---|
53 | |
---|
54 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
---|
55 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
---|
56 | curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/n4dcert.pem"); |
---|
57 | |
---|
58 | |
---|
59 | $data = curl_exec($ch); |
---|
60 | curl_close($ch); |
---|
61 | |
---|
62 | //error_log($data); |
---|
63 | //error_log(curl_errno($ch)); |
---|
64 | if (curl_errno($ch)) { |
---|
65 | $xml_snippet=simplexml_load_string($data); |
---|
66 | $json=json_encode($xml_snippet); |
---|
67 | echo ($json); |
---|
68 | } else { |
---|
69 | $xmlobj=xmlrpc_decode($data); |
---|
70 | |
---|
71 | error_log($xmlobj); |
---|
72 | |
---|
73 | $json=json_encode($xmlobj); |
---|
74 | var_error_log($data); |
---|
75 | var_error_log($xmlobj); |
---|
76 | var_error_log($json); |
---|
77 | if ($json=="") { |
---|
78 | $json=$xmlobj;} |
---|
79 | //error_log($json); |
---|
80 | var_error_log($json); |
---|
81 | echo ($json); |
---|
82 | } |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | function var_error_log( $object=null ){ |
---|
88 | ob_start(); // start buffer capture |
---|
89 | var_dump( $object ); // dump the values |
---|
90 | $contents = ob_get_contents(); // put the buffer into a variable |
---|
91 | ob_end_clean(); // end capture |
---|
92 | error_log( $contents ); // log contents of the result of var_dump( $object ) |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | try{ |
---|
98 | // Getting $_POST parameters |
---|
99 | $method=$_POST["method"]; |
---|
100 | $args=json_decode($_POST["args"]); |
---|
101 | $timeout=$_POST["timeout"]; |
---|
102 | $myfile = fopen("/tmp/n4dlog","a"); |
---|
103 | fwrite($myfile,$method); |
---|
104 | fwrite($myfile,$_POST["args"]); |
---|
105 | fwrite($myfile,"******\n"); |
---|
106 | fclose($myfile); |
---|
107 | error_log($method); |
---|
108 | |
---|
109 | if (isset($_POST['log'])) $log=$_POST["log"]; |
---|
110 | else $log="false"; |
---|
111 | |
---|
112 | n4d($method, $args, $timeout); |
---|
113 | //$n4d_async = new n4dAsync(); |
---|
114 | //$n4d_async ->start($method, $args); |
---|
115 | |
---|
116 | } catch(Exception $e){ |
---|
117 | error_log("Exception in n4d.php: ".$e); |
---|
118 | } |
---|
119 | ?> |
---|