PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/js/tinymce/plugins/spellchecker/rpc.php

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