Commit 66bdb10252c31cccb23ad129234c9126a3903ed0

Authored by Conrad Vermeulen
1 parent 22270a18

WSA-70

"Provide a mechanism to allow for JSON responses from web service requests"
Implemented.

Committed By: Conrad Vermeulen
Reviewed By: Megan Watson

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7727 c91229c3-7414-0410-bfa2-8a42b809f60b
ktwebservice/json.php 0 → 100644
  1 +<?php
  2 +
  3 +define('JSON_WEBSERVICE',1);
  4 +require_once('webservice.php');
  5 +
  6 +?>
0 7 \ No newline at end of file
... ...
ktwebservice/webservice.php
... ... @@ -230,7 +230,6 @@ class KTWebService
230 230 'folder_id' => 'int',
231 231 'workflow' => 'string',
232 232 'workflow_state' => 'string',
233   - //'checkout_by' => 'string',
234 233 'full_path' => 'string',
235 234 'owner'=>'string',
236 235 'is_immutable'=>'boolean',
... ... @@ -3985,6 +3984,109 @@ class KTWebService
3985 3984 return new SOAP_Value('return',"{urn:$this->namespace}kt_search_response", $response);
3986 3985 }
3987 3986  
  3987 +
  3988 + /**
  3989 + * The main json request processing function.
  3990 + *
  3991 + * The method name of the method to be called must be $_POST['_method'].
  3992 + * All parameters may be set in any order, as long as the names correspond with the wdsl names.
  3993 + *
  3994 + */
  3995 + function runJSON()
  3996 + {
  3997 + // Let us check that all the POST variables that are expected are set
  3998 + if ($_SERVER['REQUEST_METHOD'] != 'POST')
  3999 + {
  4000 + print json_encode(array('msg'=>'Post request must be made!'));
  4001 + return;
  4002 + }
  4003 + $method = '';
  4004 + if (array_key_exists('_method',$_POST))
  4005 + {
  4006 + $method=$_POST['_method'];
  4007 + }
  4008 + if ($method == '' || !array_key_exists($method,$this->__dispatch_map))
  4009 + {
  4010 + print json_encode(array('msg'=>'Method must exist!'));
  4011 + return;
  4012 + }
  4013 +
  4014 + // let us check that we have all the variables for the method to be called.
  4015 + $dispatcher = $this->__dispatch_map[$method];
  4016 + $params = array();
  4017 + foreach($dispatcher['in'] as $var=>$type)
  4018 + {
  4019 + $param = array('var'=>$var, 'set'=>0);
  4020 +
  4021 + if (array_key_exists($var, $_POST))
  4022 + {
  4023 + $param['set'] = 1;
  4024 +
  4025 + // if it looks json like we should decode it
  4026 + if (substr($_POST[$var],0,1) == '{' && substr($_POST[$var],-1) == '}')
  4027 + {
  4028 + $original = $_POST[$var];
  4029 + $decoded = json_decode($original);
  4030 +
  4031 + $_POST[$var] = is_null($decoded)?$original:$decoded;
  4032 + unset($original);
  4033 + unset($decoded);
  4034 + }
  4035 + }
  4036 +
  4037 + $params[] = $param;
  4038 + }
  4039 +
  4040 + // prepare the parameters and call the method
  4041 + // by passing references to $POST we hopefully save some memory
  4042 +
  4043 + $paramstr = '';
  4044 + foreach($params as $param)
  4045 + {
  4046 + $var = $param['var'];
  4047 + if ($param['set'] == 0)
  4048 + {
  4049 + print json_encode(array('msg'=>"'$var' is not set!"));
  4050 + return;
  4051 + }
  4052 + if ($paramstr != '') $paramstr .= ',';
  4053 +
  4054 + $paramstr .= "\$_POST['$var']";
  4055 + }
  4056 +
  4057 + $result = eval("return \$this->$method($paramstr);");
  4058 +
  4059 + // return the json encoded result
  4060 + print json_encode(KTWebService::decodeSOAPValue($result));
  4061 + }
  4062 +
  4063 + /**
  4064 + * Returns a decoded soap value structure.
  4065 + *
  4066 + * @param SOAP_Value $value
  4067 + * @return mixed
  4068 + */
  4069 + function decodeSOAPValue($value)
  4070 + {
  4071 + if ($value instanceof SOAP_Value)
  4072 + {
  4073 + $x = new stdClass();
  4074 + $v = & $value->value;
  4075 + $vars = array_keys($v);
  4076 +
  4077 + foreach($vars as $var)
  4078 + {
  4079 + $x->$var = KTWebService::decodeSOAPValue($v[$var]);
  4080 + }
  4081 +
  4082 + return $x;
  4083 + }
  4084 + else
  4085 + {
  4086 + return $value;
  4087 + }
  4088 + }
  4089 +
3988 4090 /**
3989 4091 * This runs the web service
3990 4092 *
... ... @@ -3993,6 +4095,12 @@ class KTWebService
3993 4095 */
3994 4096 function run()
3995 4097 {
  4098 + if (defined('JSON_WEBSERVICE'))
  4099 + {
  4100 + $this->runJSON();
  4101 + return;
  4102 + }
  4103 +
3996 4104 ob_start();
3997 4105 $server = new SOAP_Server();
3998 4106  
... ...