Class Json

Description

The Json class provides useful functions for servicing AJAX requests.

In the simplest form, this class is used in the following manner:


 postResults($results);
</code> To run a arbitrary system command, the following represents the simplest form:


 exec('ls -al /tmp 2>&1');
 $json->postResults($results);
</code> See: wui/php/wireless/wireless-json.php for a more advanced example.

Located in /lib/Json.php (line 47)


	
			
Direct descendents
Class Description
NstFileStorage This class provides a file based implementation remote storage support for the NstRemoteStorage JavaScript class.
JsonAdnsresfilter Extend the base Json class with andsresfilter specific methods.
Method Summary
static void setHeader ([allowCache $allowCache = false], [contentType $contentType = 'application/javascript'])
An exec ($cmd $cmd)
Associative execResults ($cmd $cmd, $rc $rc, $output $output)
void getCommand ($name $name)
void getValue ( $key,  $def)
void postResults ($results $results)
Methods
static method setHeader (line 64)

Set header information for JSON response.

static void setHeader ([allowCache $allowCache = false], [contentType $contentType = 'application/javascript'])
  • allowCache $allowCache: Should the client cache the results (defaults to false).
  • contentType $contentType: You can override if you need to return something other than JavaScript (it omitted, we default to returning JavaScript to client).
exec (line 173)

Run a system command and return results in a associative array.

This method runs a arbitrary system command and returns the results in a associative array (which can then be posted).

The following example shows how one might use this to allow a directory listing on a directory which is specified in a POST/GET request (or defaults to "/tmp" if not in request).


 getCommand('ls');
 $dir = escapeshellcmd($json->getValue('dir', '/tmp');
 $results = json->exec("{$ls} -al {$dir} 2>&1");
 $json->postResults($results);

 ?>
</code>

  • return: associative array, with the 'command' key associated with the command which was run. The 'rc' key associated with the integer return code from running the command (typically 0 on success), and the 'output' key associated with an array of output lines produced by running the command).
An exec ($cmd $cmd)
  • $cmd $cmd: The system command to run (you will typically want to include '2>&1' or '2>/dev/null' on the end to deal with stderr).
execResults (line 136)

Build standard looking 'exec' results.

  • return: array.
Associative execResults ($cmd $cmd, $rc $rc, $output $output)
  • $cmd $cmd: The command line to 'exec'.
  • $rc $rc: The return code from invocation.
  • $output $output: An array of output lines to return.
getCommand (line 120)

Get full path to a program to run.

void getCommand ($name $name)
  • $name $name: The name of the program to find (like: "ls").
getCommandLine (line 105)

Get access to a CommandLine instance for execing commands.

  • return: CommandLine instance
A getCommandLine ()
getValue (line 183)

Get request parameter value (or default).

void getValue ( $key,  $def)
  • $key
  • $def
postResults (line 81)

Post PHP entity as JSON encoded object.

void postResults ($results $results)
  • $results $results: The PHP object to returns as a JSON encoded object.

Documentation generated on Sun, 11 Feb 2018 07:41:02 -0500 by phpDocumentor 1.4.4