Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  
  10  require_once ("./includes/general.php");
  11  
  12  // Set RPC response headers

  13  header('Content-Type: text/plain');
  14  header('Content-Encoding: UTF-8');
  15  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  16  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  17  header("Cache-Control: no-store, no-cache, must-revalidate");
  18  header("Cache-Control: post-check=0, pre-check=0", false);
  19  header("Pragma: no-cache");
  20  
  21  $raw = "";
  22  
  23  // Try param

  24  if (isset($_POST["json_data"]))
  25  	 $raw = getRequestParam("json_data");
  26  
  27  // Try globals array

  28  if (!$raw && isset($_GLOBALS) && isset($_GLOBALS["HTTP_RAW_POST_DATA"]))
  29  	 $raw = $_GLOBALS["HTTP_RAW_POST_DATA"];
  30  
  31  // Try globals variable

  32  if (!$raw && isset($HTTP_RAW_POST_DATA))
  33  	 $raw = $HTTP_RAW_POST_DATA;
  34  
  35  // Try stream

  36  if (!$raw) {
  37  	 if (!function_exists('file_get_contents')) {
  38  	 	 $fp = fopen("php://input", "r");
  39  	 	 if ($fp) {
  40  	 	 	 $raw = "";
  41  
  42  	 	 	 while (!feof($fp))
  43  	 	 	 	 $raw = fread($fp, 1024);
  44  
  45  	 	 	 fclose($fp);
  46  	 	 }
  47  	 } else
  48  	 	 $raw = "" . file_get_contents("php://input");
  49  }
  50  
  51  // No input data

  52  if (!$raw)
  53  	 die('{"result":null,"id":null,"error":{"errstr":"Could not get raw post data.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}');
  54  
  55  // Passthrough request to remote server

  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  
  91  // Get JSON data

  92  $json = new Moxiecode_JSON();
  93  $input = $json->decode($raw);
  94  
  95  // Execute RPC

  96  if (isset($config['general.engine'])) {
  97  	 $spellchecker = new $config['general.engine']($config);
  98  	 $result = call_user_func_array(array($spellchecker, $input['method']), $input['params']);
  99  } else
 100  	 die('{"result":null,"id":null,"error":{"errstr":"You must choose an spellchecker engine in the config.php file.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}');
 101  
 102  // Request and response id should always be the same

 103  $output = array(
 104  	 "id" => $input->id,
 105  	 "result" => $result,
 106  	 "error" => null
 107  );
 108  
 109  // Return JSON encoded string

 110  echo $json->encode($output);
 111  
 112  ?>