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