X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=php%2Fplc_api.php;h=38aa8961a8eec8b5973980c407c779264f589dd5;hb=bc7bd41556e1fd137acf4df415e5dc0f6d5e02c4;hp=0c2187e1901394c7371854433c630abe13344a30;hpb=1e4558f1e8eaec5f69f3832a8599d0dfa1531f00;p=plcapi.git diff --git a/php/plc_api.php b/php/plc_api.php index 0c2187e..38aa896 100644 --- a/php/plc_api.php +++ b/php/plc_api.php @@ -22,6 +22,7 @@ * If the package is not found the php module XML-RPC is used if available * */ + if (file_exists(__DIR__ . '/phpxmlrpc/src/Autoloader.php')) { include_once __DIR__ . '/phpxmlrpc/src/Autoloader.php'; PhpXmlRpc\Autoloader::register(); @@ -29,163 +30,148 @@ if (file_exists(__DIR__ . '/phpxmlrpc/src/Autoloader.php')) { require_once 'plc_config.php'; -class PLCAPI -{ - var $auth; - var $server; - var $port; - var $path; - var $errors; - var $trace; - var $calls; - var $multicall; - - function PLCAPI($auth = NULL, - $server = PLC_API_HOST, - $port = PLC_API_PORT, - $path = PLC_API_PATH, - $cainfo = NULL) - { - $this->auth = $auth; - $this->server = $server; - $this->port = $port; - $this->path = $path; - $this->cainfo = $cainfo; - $this->errors = array(); - $this->trace = array(); - $this->calls = array(); - $this->multicall = false; - } - - function rec_join ($arg) { - if ( is_array($arg) ) { - $ret = ""; - foreach ( $arg as $i ) { - $l = $this->rec_join($i); - # ignore html code. - if ( $l[0] != "<" ) { $ret .= $l . ", "; } +class PLCAPI { + + var $auth; + var $server; + var $port; + var $path; + var $errors; + var $trace; + var $calls; + var $multicall; + + function __construct($auth = NULL, + $server = PLC_API_HOST, + $port = PLC_API_PORT, + $path = PLC_API_PATH, + $cainfo = NULL) { + $this->auth = $auth; + $this->server = $server; + $this->port = $port; + $this->path = $path; + $this->cainfo = $cainfo; + $this->errors = array(); + $this->trace = array(); + $this->calls = array(); + $this->multicall = false; + } + + function rec_join ($arg) { + if ( is_array($arg) ) { + $ret = ""; + foreach ( $arg as $i ) { + $l = $this->rec_join($i); + # ignore html code. + if ( $l[0] != "<" ) { $ret .= $l . ", "; } + } + return $ret; + } else { + settype($arg, "string"); + return $arg; } - return $ret; - } else { - settype($arg, "string"); - return $arg; } - } - - function backtrace_php () { - $backtrace = debug_backtrace(); - $msg = ""; - $len = count($backtrace); - $cnt = 1; - foreach( array_reverse($backtrace) as $line ) { - $msg .= "File '". $line['file'] . "' line " . $line['line'] . "\n"; - $msg .= " " . $line['function'] . "( " . $this->rec_join($line['args']) . ")\n"; - $cnt += 1; - if ( $cnt == $len ) { break; } + + function backtrace_php () { + $backtrace = debug_backtrace(); + $msg = ""; + $len = count($backtrace); + $cnt = 1; + foreach( array_reverse($backtrace) as $line ) { + $msg .= "File '". $line['file'] . "' line " . $line['line'] . "\n"; + $msg .= " " . $line['function'] . "( " . $this->rec_join($line['args']) . ")\n"; + $cnt += 1; + if ($cnt == $len) + break; + } + return $msg; } - return $msg; - } - - function error_log($error_msg, $backtrace_level = 1) - { - $backtrace = debug_backtrace(); - $file = $backtrace[$backtrace_level]['file']; - $line = $backtrace[$backtrace_level]['line']; - - $error_line='PLCAPI error: ' . $error_msg ; - if ($file) $error_line .= ' in file ' . $file; - if ($line) $error_line .= ' on line ' . $line; - $this->errors[] = $error_line; - # TODO: setup a config variable for more detailed stack traces, for API errors. - if ( TRUE ){ - error_log($error_line); - } else { - error_log($this->backtrace_php()); + + function error_log($error_msg, $backtrace_level = 1) { + $backtrace = debug_backtrace(); + $file = $backtrace[$backtrace_level]['file']; + $line = $backtrace[$backtrace_level]['line']; + + $error_line='PLCAPI error: ' . $error_msg ; + if ($file) + $error_line .= ' in file ' . $file; + if ($line) + $error_line .= ' on line ' . $line; + $this->errors[] = $error_line; + # TODO: setup a config variable for more detailed stack traces, for API errors. + if (TRUE) { + error_log($error_line); + } else { + error_log($this->backtrace_php()); + } } - } - - function error() - { - if (empty($this->trace)) { - return NULL; - } else { - $last_trace = end($this->trace); - return implode("\\n", $last_trace['errors']); + + function error() { + if (empty($this->trace)) { + return NULL; + } else { + $last_trace = end($this->trace); + return implode("\\n", $last_trace['errors']); + } + } + + function trace() { + return $this->trace; + } + + function microtime_float() { + list($usec, $sec) = explode(" ", microtime()); + return ((float) $usec + (float) $sec); } - } - - function trace() - { - return $this->trace; - } - - function microtime_float() - { - list($usec, $sec) = explode(" ", microtime()); - return ((float) $usec + (float) $sec); - } - - function call($method, $args = NULL) - { - if ($this->multicall) { - $this->calls[] = array ('methodName' => $method, - 'params' => $args); - return NULL; - } else { - return $this->internal_call($method, $args, 3); + + function call($method, $args = NULL) { + if ($this->multicall) { + $this->calls[] = array ('methodName' => $method, + 'params' => $args); + return NULL; + } else { + return $this->internal_call($method, $args, 3); + } } - } - - /* - * Use PhpXmlRpc\Value before encoding the request - */ - function xmlrpcValue($value) { - switch(gettype($value)) { - case 'array': - $members = array(); - foreach($value as $vk => $vv) { - $members[$vk] = $this->xmlrpcValue($vv); - } - - if (array_key_exists(0, $value)) { - return new PhpXmlRpc\Value( - $members, - 'array' - ); - } else { - return new PhpXmlRpc\Value( - $members, - 'struct' - ); - } - - break; - case 'double': - return new PhpXmlRpc\Value($value, 'double'); - break; - case 'boolean': - return new PhpXmlRpc\Value($value, 'boolean'); - break; - case 'NULL': - case 'null': - return new PhpXmlRpc\Value(null, 'null'); - break; - case 'integer': - return new PhpXmlRpc\Value($value, 'int'); - break; - default: - if (empty($value)) { + + /* + * Use PhpXmlRpc\Value before encoding the request + */ + function xmlrpcValue($value) { + switch(gettype($value)) { + case 'array': + $members = array(); + foreach($value as $vk => $vv) { + $members[$vk] = $this->xmlrpcValue($vv); + } + + if ((array_key_exists(0, $value)) || (empty($value))) { + return new PhpXmlRpc\Value($members, 'array'); + } else { + return new PhpXmlRpc\Value($members, 'struct'); + } + + break; + case 'double': + return new PhpXmlRpc\Value($value, 'double'); + break; + case 'boolean': + return new PhpXmlRpc\Value($value, 'boolean'); + break; + case 'NULL': + case 'null': return new PhpXmlRpc\Value(null, 'null'); - } else { + break; + case 'integer': + return new PhpXmlRpc\Value($value, 'int'); + break; + default: return new PhpXmlRpc\Value($value); - } - - break; - } - } + break; + } + } - function internal_call($method, $args = NULL, $backtrace_level = 2) - { + function internal_call($method, $args = NULL, $backtrace_level = 2) { if (class_exists('PhpXmlRpc\\PhpXmlRpc')) { return $this->internal_call_phpxmlrpc($method, $args, $backtrace_level); } else { @@ -193,184 +179,192 @@ class PLCAPI } } - /* - * the new internal call, will use PhpXmlRpc - */ - function internal_call_phpxmlrpc($method, $args = NULL, $backtrace_level = 2) - { + /* + * the new internal call, will use PhpXmlRpc + */ + function internal_call_phpxmlrpc($method, $args = NULL, $backtrace_level = 2) { +// echo '
method and args:'; - PhpXmlRpc\PhpXmlRpc::$xmlrpc_null_extension = true; + PhpXmlRpc\PhpXmlRpc::$xmlrpc_null_extension = true; - if ($this->port == 443) { - $url = 'https://'; - } else { - $url = 'http://'; - } + if ($this->port == 443) { + $url = 'https://'; + } else { + $url = 'http://'; + } - // Set the URL for the request - $url .= $this->server . ':' . $this->port . '/' . $this->path; + // Set the URL for the request + $url .= $this->server . ':' . $this->port . '/' . $this->path; - $client = new PhpXmlRpc\Client($url); - $client->setSSLVerifyPeer(false); - /* - * 1 -> not verify CN - * 2 -> verify CN (default) - */ - $client->setSSLVerifyHost(1); + $client = new PhpXmlRpc\Client($url); + $client->setSSLVerifyPeer(false); + /* + * 1 -> not verify CN + * 2 -> verify CN (default) + */ + $client->setSSLVerifyHost(1); - $values = $this->xmlrpcValue($args); + $values = $this->xmlrpcValue($args); - $response = $client->send(new PhpXmlRpc\Request($method, $values)); + $response = $client->send(new PhpXmlRpc\Request($method, $values)); +// echo '
'; +// var_dump($method); +// var_dump($args); +// echo '
response:'; +// echo '
'; +// var_dump($response); +// echo '
response->value():'; - if (!$response->faultCode()) { - $encoder = new PhpXmlRpc\Encoder(); - $v = $encoder->decode($response->value()); + if (!$response->faultCode()) { + $encoder = new PhpXmlRpc\Encoder(); + $v = $encoder->decode($response->value()); - return $v; - } else { - $this->error_log("An error occurred [" . $response->faultCode() . "] ". - $response->faultString()); - return NULL; - } - } - - /* - * The original internal call that uses php XML-RPC - */ - function internal_call_xmlrpc($method, $args = NULL, $backtrace_level = 2) - { - $curl = curl_init(); - - // Verify peer certificate if talking over SSL - if ($this->port == 443) { - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2); - if (!empty($this->cainfo)) { - curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo); - } elseif (defined('PLC_API_CA_SSL_CRT')) { - curl_setopt($curl, CURLOPT_CAINFO, PLC_API_CA_SSL_CRT); - } - $url = 'https://'; - } else { - $url = 'http://'; + return $v; + } else { + $this->error_log( + "An error occurred [" . $response->faultCode() . "] " + . $response->faultString()); + return NULL; + } } - // Set the URL for the request - $url .= $this->server . ':' . $this->port . '/' . $this->path; - curl_setopt($curl, CURLOPT_URL, $url); - - // Marshal the XML-RPC request as a POST variable.
'; +// var_dump($response->value()); +// echo '