alecpl
2010-06-24 2011bef155aacdfa8461a4d5c2cd3988d946d135
commit | author | age
4ca10b 1 <?php
T 2 /**
3  * $Id: rpc.php 822 2008-04-28 13:45:03Z spocke $
4  *
5  * @author Moxiecode
6  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
7  */
8
9 require_once("./includes/general.php");
10
11 // Set RPC response headers
12 header('Content-Type: text/plain');
13 header('Content-Encoding: UTF-8');
14 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
15 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
16 header("Cache-Control: no-store, no-cache, must-revalidate");
17 header("Cache-Control: post-check=0, pre-check=0", false);
18 header("Pragma: no-cache");
19
20 $raw = "";
21
22 // Try param
23 if (isset($_POST["json_data"]))
24     $raw = getRequestParam("json_data");
25
26 // Try globals array
27 if (!$raw && isset($_GLOBALS) && isset($_GLOBALS["HTTP_RAW_POST_DATA"]))
28     $raw = $_GLOBALS["HTTP_RAW_POST_DATA"];
29
30 // Try globals variable
31 if (!$raw && isset($HTTP_RAW_POST_DATA))
32     $raw = $HTTP_RAW_POST_DATA;
33
34 // Try stream
35 if (!$raw) {
36     if (!function_exists('file_get_contents')) {
37         $fp = fopen("php://input", "r");
38         if ($fp) {
39             $raw = "";
40
41             while (!feof($fp))
42                 $raw = fread($fp, 1024);
43
44             fclose($fp);
45         }
46     } else
47         $raw = "" . file_get_contents("php://input");
48 }
49
50 // No input data
51 if (!$raw)
52     die('{"result":null,"id":null,"error":{"errstr":"Could not get raw post data.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}');
53
2011be 54
4ca10b 55 // Passthrough request to remote server
T 56 if (isset($config['general.remote_rpc_url'])) {
57     $url = parse_url($config['general.remote_rpc_url']);
58
59     // Setup request
60     $req = "POST " . $url["path"] . " HTTP/1.0\r\n";
61     $req .= "Connection: close\r\n";
62     $req .= "Host: " . $url['host'] . "\r\n";
63     $req .= "Content-Length: " . strlen($raw) . "\r\n";
64     $req .= "\r\n" . $raw;
65
66     if (!isset($url['port']) || !$url['port'])
67         $url['port'] = 80;
68
69     $errno = $errstr = "";
70
71     $socket = fsockopen($url['host'], intval($url['port']), $errno, $errstr, 30);
72     if ($socket) {
73         // Send request headers
74         fputs($socket, $req);
75
76         // Read response headers and data
77         $resp = "";
78         while (!feof($socket))
79                 $resp .= fgets($socket, 4096);
80
81         fclose($socket);
82
83         // Split response header/data
84         $resp = explode("\r\n\r\n", $resp);
85         echo $resp[1]; // Output body
86     }
87
88     die();
89 }
90
2011be 91 console($raw);
4ca10b 92 // Get JSON data
T 93 $json = new Moxiecode_JSON();
94 $input = $json->decode($raw);
95
96 // Execute RPC
97 if (isset($config['general.engine'])) {
98     $spellchecker = new $config['general.engine']($config);
99     $result = call_user_func_array(array($spellchecker, $input['method']), $input['params']);
100 } else
101     die('{"result":null,"id":null,"error":{"errstr":"You must choose an spellchecker engine in the config.php file.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}');
102
103 // Request and response id should always be the same
104 $output = array(
105     "id" => $input->id,
106     "result" => $result,
107     "error" => null
108 );
109
2011be 110 console($json->encode($output));
4ca10b 111 // Return JSON encoded string
T 112 echo $json->encode($output);
113
114 ?>