5 # Part of the Collection Workflow Integration System (CWIS) 6 # Copyright 2002-2013 Edward Almasy and Internet Scout Research Group 7 # http://scout.wisc.edu/cwis/ 22 $this->Data = array();
23 $this->Warnings = array();
33 $this->Data[$Key] = $Value;
45 $this->Warnings[] = strval($Message);
57 $this->SendResult($this->GenerateResult(
"ERROR", $Message));
67 $this->SendResult($this->GenerateResult(
"OK", $Message));
78 private function SendResult(array $Result)
80 header(
"Content-Type: application/json; charset=" 81 .$GLOBALS[
"G_SysConfig"]->DefaultCharacterSet(), TRUE);
82 $this->PrintArrayToJson($Result);
93 private function GenerateResult($State, $Message)
96 "data" => $this->Data,
98 "state" => strval($State),
99 "message" => strval($Message),
100 "numWarnings" => count($this->Warnings),
101 "warnings" => $this->Warnings));
108 private function PrintArrayToJson(array $Array)
110 # variables needed for printing commas if necessary 112 $Count = count($Array);
114 # determine whether or not we have a true array or a hash map 116 $ArrayCount = count($Array);
117 for ($i = 0, reset($Array); $i < $ArrayCount; $i++, next($Array))
119 if (key($Array) !== $i)
127 print ($TrueArray) ?
"[" :
"{";
130 foreach ($Array as $key => $value)
132 # replacements so we can escape strings and replace smart quotes 133 static $Replace = array(
134 array(
"\\",
"/",
"\n",
"\t",
"\r",
"\b",
"\f",
'"',
"",
136 array(
'\\\\',
'\\/',
'\\n',
'\\t',
'\\r',
'\\b',
'\\f',
'\"',
137 "'",
"'",
'\"',
'\"',
'-'));
139 # print key if a hash map 142 # escape, remove smart quotes, and print the key 143 print
'"'.str_replace($Replace[0], $Replace[1], $key).
'":';
146 # scalar values (int, float, string, or boolean) 147 if (is_scalar($value))
149 # numeric (i.e., float, int, or float/int string) 150 if (is_numeric($value))
156 else if (is_string($value))
158 # escape, remove smart quotes, and print the value 159 print
'"'.str_replace($Replace[0], $Replace[1], $value).
'"';
163 else if ($value === TRUE)
169 else if ($value === FALSE)
175 # recur if the value is an array 176 else if (is_array($value))
178 $this->PrintArrayToJson($value);
182 else if (is_null($value))
187 # object, just print the name and don't possibly expose secret details 188 else if (is_object($value))
190 print
'"object('.get_class($value).
')"';
193 # resource, just print the name and don't possibly expose secret details 196 print
'"resource('.get_resource_type($value).
')"';
199 # print comma if necessary 200 if (++$Offset < $Count) { print
","; }
204 print ($TrueArray) ?
"]" :
"}";
AddWarning($Message)
Add a warning message to export in the JSON response.
Success($Message="")
Signal that the callback was successful and optionally set a message.
AddDatum($Key, $Value)
Add a datum identified by a key to export in the JSON response.
Convenience class for standardizing JSON responses, making it easier to export primitive data types t...
__construct()
Object constructor.
Error($Message)
Add an error message to export in the JSON response and then send the response.